Nginx socket io websockets代理传递

时间:2016-02-12 02:03:13

标签: node.js sockets nginx proxypass

我一直试图弄清楚如何在nginx中使用proxy_pass socket io / websocket数据。

当尝试使用socket io连接时,我在nginx中不断收到此错误。

2016/02/12 03:57:42 [info] 1047#0: *15 client closed connection while waiting for request, client: 192.168.1.1, server: 0.0.0.0:4444
2016/02/12 03:57:42 [info] 1047#0: *13 client closed connection while waiting for request, client: 192.168.1.1, server: 0.0.0.0:4444
2016/02/12 03:57:42 [info] 1047#0: *14 client closed connection while waiting for request, client: 192.168.1.1, server: 0.0.0.0:4444
2016/02/12 05:11:19 [info] 1047#0: *18 client closed connection while waiting for request, client: 192.168.1.1, server: 0.0.0.0:4444
2016/02/12 05:11:19 [info] 1047#0: *20 client closed connection while waiting for request, client: 192.168.1.1, server: 0.0.0.0:4444
2016/02/12 05:11:19 [info] 1047#0: *22 client closed connection while waiting for request, client: 192.168.1.1, server: 0.0.0.0:4444
2016/02/12 05:11:19 [info] 1047#0: *24 client closed connection while waiting for request, client: 192.168.1.1, server: 0.0.0.0:4444

这是我的配置:

#inside http block
upstream socket_nodes {
    ip_hash;
    server 127.0.0.1:6969;
    #socket io app is running on port 6969
} 

我的服务器块

server {
    listen       4444;

    server_name  url.com;

    #charset koi8-r;

    location /io/ {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://localhost:6969;
        proxy_buffering off;
        proxy_buffer_size 8k;
        proxy_buffers 2048 8k;
    }

    location / {
        root /path/to/my/static/files/;
        index  index.html index.htm;
    }

    location /path/to/http/api/ {
            proxy_pass    http://localhost:2814/;
            #api app location
    }
}

在前端,每​​次socket io尝试轮询时都会得到404未找到的响应,这不是由NGINX生成的

我的连接字符串是:

var socket = io.connect('http://url.com',{'force new connection': true, path: '/io/socket.io'});

1 个答案:

答案 0 :(得分:1)

服务器似乎以某种方式与静态路径文件冲突。

我设法通过将应用程序放在子域上来实现它。