Error during WebSocket handshake, I use nginx

Technical Support
  • 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.

  • Works fine 🙂

  • @jenkler works fine?

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

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

  • 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.

  • @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
    
  • ... do you need to set cookieDomain? You're probably better off unsetting that config instead of commenting out code.


Suggested Topics


  • 0 Votes
    2 Posts
    206 Views

    Looking for solutions for this problem I tried nodebb-plugin-session-sharing plugin, which is supposed to generate JWT token if I understand correctly, I didn't manage yet to generate it although I think I correctly configured it.

    Anyways, what I see in the cookie storage is cookie named express.sid which itself seems that it's not JWT cookie, but of some other kind, what I would want to know is it possible to decode that cookie on different subdoman of the same site, what I have is forum.domain.com (where NodeBB is installed) and app.domain.com which is running Node.js application.

    I need a simplest solution I can get, I don't want to mess with SSO systems like FusionAuth of Keycloak, I want to use NodeBB's user registration system.

  • 0 Votes
    4 Posts
    190 Views
  • 0 Votes
    2 Posts
    2k Views

    Ah, yes, that is potentially a stumbling block... port property is optional, if not specified, NodeBB will listen on port 4567. However the url property is the one that is referenced by NodeBB, and is used as the canonical reference to itself... so when somebody asks your forum what it is to be called, NodeBB will respond back with the value in url, so the port is required if you are accessing your forum with port number in url.

  • Redis Error

    Technical Support
    0 Votes
    4 Posts
    1k Views

    Yeah, it was a RAM issue. Upgraded the server and all is well now.

  • 0 Votes
    2 Posts
    1k Views

    Your config file looks good, paste output of this command to rule out any user error?

    cat /home/javier/proyectos/foros/config.json