简体   繁体   中英

Error with Loadbalancing, Websockets and NodeJS

I have two NodeJS application with socket.io, which sits behind a local (docker) NGINX loadbalancer. Wenn only one application is up everything works fine. When I add the second I get the following error on every second reload or so.

WebSocket connection to 'wss://[HOST]/socket.io/?EIO=3&transport=websocket&sid=9CmO27cJsdmqMwwXAAAK' failed: Error during WebSocket handshake: Unexpected response code: 400

here my NGINX config:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    upstream myhost {
        least_conn;
        server 192.168.99.1:3333;
        server 192.168.99.1:4444;
    }

    server {
        listen 80;
        listen 443 ssl;
        server_name         [HOST];
        ssl_certificate     server.crt;
        ssl_certificate_key server.key;

        location / {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_http_version 1.1;
            proxy_pass http://myhost;
        }
    }
}

You probably don't have socket.io set up to support multiple nodes, in which case the server on port 3333 might get requests/frames related to connections set up by the server on port 4444 (and vice versa) which will cause problems (because a connection set up by 3333 is unknown to 4444).

There are various solutions outlined here: https://socket.io/docs/using-multiple-nodes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM