带有nginx负载均衡器问题的Socket.IO集群

时间:2014-10-22 17:26:52

标签: node.js nginx socket.io

我正在运行NGINX作为Socket.IO服务器的反向代理,该服务器负载平衡多个集群进程的请求。每个集群进程都被告知要侦听不同的端口。

nginx服务器配置为基于IP哈希进行负载均衡,但我收到消息:

ws://{domain}/socket.io/?EIO=3&transport=websocket&sid=KaU3C8caGVK4gU1LAAAB failed: WebSocket is closed before the connection is established.

我的nginx配置有:

http {
    {+ default configs}

    upstream io_nodes {
        ip_hash;
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
        server 127.0.0.1:3003;
    }

}

默认vhost:

server {
#listen   80; ## listen for ipv4; this line is default and implied
#listen   [::]:80 default ipv6only=on; ## listen for ipv6

root /usr/share/nginx/www/static/web;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name {domain};

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://io_nodes;
     }
}

任何帮助将不胜感激。如果我在没有集群的情况下运行nodejs服务器,它的行为正确,我在此设置中使用的引用位于:http://socket.io/docs/using-multiple-nodes/

由于

1 个答案:

答案 0 :(得分:0)

我发现问题是安装的nginx版本是< 1.3并没有支持websockets。

对于其他任何人,您可以查看如何在ubuntu上安装较新版本的nginx:

http://usefulmix.com/install-upgrade-to-latest-nginx-without-compiling-from-source/