+-
带有nginx的Socket.io
我正在尝试通过nginx 1.6提供静态文件,并使用socket.io代理来自Node.js Web服务器的套接字流量.

这是nginx.conf的相关部分:

location /socket.io/ {
            proxy_pass http://localhost:3000;       
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
        }

它在浏览器和Node.js之间直接完美地工作,但是当使用nginx 1.6代理时,socket.io需要太长时间.握手协议需要花费太多时间,但如果保持不间断,它最终会在几分钟后开始工作.

nginx提供的静态文件完美运行.

可能是什么问题呢?

更新:

我分析了一下网络流量,并确定以下请求大约持续一分钟(这正是请求升级时):

Sec-WebSocket-Key: LhZ1frRdl+myuwyR/T03lQ==
Cookie: io=1-A7tpvwmoGbrSvTAAA5
Connection: keep-alive, Upgrade
Upgrade: websocket
....

预期的响应是代码101和:

Connection: upgrade
Sec-WebSocket-Accept: HXx3KKJadQYjDa11lpK5y1nENMM=
Upgrade: websocket
...

相反,浏览器接收400并且:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:8888
Connection: keep-alive
Content-Type: application/json
Server: nginx/1.6.2
Transfer-Encoding: chunked

更新2:

我确定相同的配置在我的办公室计算机上完美运行,这意味着它是我的家用电脑问题.无论如何,确定究竟出了什么问题会非常好.

最佳答案
在正在运行的服务器中,这里使用的nginx配置是:

  # Requests for socket.io are passed on to Node on port 3000
  location ~* \.io {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy false;

      proxy_pass http://localhost:3000;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
点击查看更多相关文章

转载注明原文:带有nginx的Socket.io - 乐贴网