Error during WebSocket handshake, I use nginx



  • I've searched for a whole day, and of course I searched in nodeBB first. But I cannot find a post that solves my problem. So I ask for help here. Please do not mark my post as spam again.

    I've read nginx config document, and I've done most of the configs. But it still doesn't work . The queer thing is that it works well in my mac, but it does not work in my server.
    My server is CentOS release 6.6 (Final).

    And I cannot understand these things:
    /path/to/nginx/sites-available/* -- files here must be aliased to ../sites-enabled(my nginx does not have such directory)
    /path/to/nginx/conf.d/*.conf -- filenames must end in .conf
    /path/to/nginx/httpd.conf -- if all else fails (What does it mean?)

    Here is my error informations:
    WebSocket connection to 'ws://test1-forum-acp.ezsport.cc/socket.io/?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

    Here is my config:

    user              nginx;
    worker_processes  2;
    
    error_log  /mnt/logs/nginx/error.log;
    
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /mnt/logs/nginx/access.log  main;
        server_names_hash_bucket_size   64;
    
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
       
        include /etc/nginx1.8/conf/conf.d/*.conf; 
    }
    
    

    conf.d/ includes config file blow

    map $http_upgrade $connection_upgrade {
    	default upgrade;
    	''      close;
    }
    
    server {
            listen 80;
            client_max_body_size 5m;
    
            server_name  test1-forum-acp.ezsport.cc;
            location / {
    		proxy_set_header X-Forwarded-Proto $scheme;
    		proxy_set_header X-NginX-Proxy true;
    
    		proxy_pass http://acp_io_nodes;
    		proxy_redirect off;
    
    		# Socket.IO Support
    	        proxy_set_header Host $host;
    	        proxy_set_header X-Real-IP $remote_addr;
    	        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	        proxy_http_version 1.1;
    	        proxy_set_header Upgrade $http_upgrade;
    	        proxy_set_header Connection $connection_upgrade;
            }
    }
    upstream acp_io_nodes {
        ip_hash;
        server 127.0.0.1:5568;
    }
    


  • well, though nodebb websocket cannot be connected, a simple websocket server I wrote can be connected. I use the same config :

      upstream acp_io_nodes { 
        #ip_hash; 
        server localhost:3434;   
      } 
     server { 
        listen       80; 
        server_name  test1-forum-acp.ezsport.cc; 
        location / { 
          proxy_pass http://acp_io_nodes; 
          proxy_read_timeout 300s; 
          proxy_set_header Host $host; 
          proxy_set_header X-Real-IP $remote_addr; 
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
          proxy_http_version 1.1; 
          proxy_set_header Upgrade $http_upgrade; 
          proxy_set_header Connection $connection_upgrade; 
                   } 
           } 
    

    here is my server's code:

    console.log("Server started"); 
    var Msg = ''; 
    var WebSocketServer = require('ws').Server 
        , wss = new WebSocketServer({port: 3434}); 
        wss.on('connection', function(ws) { 
            ws.on('message', function(message) { 
            console.log('Received from client: %s', message); 
            ws.send('Server received from client: ' + message); 
        }); 
     }); 
    

    Hope someone will rescue me soon.


  • Swedes

    Works fine 🙂



  • @jenkler works fine?


  • Swedes

    Strange, i responded to the "try to reply" message but its gone now. Nevermind!



  • @jenkler well, that's because I edit it. 😀


  • Global Moderator

    The normal way to configure nginx is to create an nginx config file in /path/to/nginx/sites-available/. You can copy one of the templates from docs.nodebb.org.

    You then need to create a symlink from the file (I'll call it forum.example.com) in sites-available to sites-enabled

    ln -s /path/to/nginx/sites-available/forum.example.com /path/to/nginx/sites-enabled/forum.example.com
    

    You don't seem to be using our nginx templates at all. Any reason why?



  • @pitaj because my nginx has other services. I have to follow previous configs. Anyway, I'll try what you said.



  • @pitaj
    My nginx dir only has these dirs:



  • @pitaj My nginx directory only has these sub dirs:

     client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
    

    And even I do what you said, nginx still not working. I seems that my nginx can only set config by conf/nginx.conf file. sites-available/ and sites-enabled/ seems useless to me.


  • Admin

    @pitaj Depending on the OS nginx is set up differently, you can't push sites_enabled/ and sites_available/ on people. It's mostly a Debian/Ubuntu thing 😃

    @lqzerogg Can you show us your config.json?



  • @julian thx, I've solved this problem. But I can't post my solution because the stupid robot marked my post as spam.



  • After a hard time debugging, I found out that it's not because of nginx setting. It is because I set a "cookieDomain" config. After I remove this one, it works properly. I modify source code and fix this problem. Maybe it's not a good solution, but it works for me.



  • file: src/socket.io/index, line:45

    // other code
    // added code, force to resign variable
    		domain = parsedUrl.hostname;
    // comment these codes
    //		if (!domain) {
    //			domain = parsedUrl.hostname;	// cookies don't provide isolation by port: http://stackoverflow.com/a/16328399/122353
    //		}
    // other code
    

  • Admin

    ... do you need to set cookieDomain? You're probably better off unsetting that config instead of commenting out code.


 

| |