gpt4 book ai didi

nginx - 带有 nginx 的 Socket.io

转载 作者:行者123 更新时间:2023-12-03 12:24:21 25 4
gpt4 key购买 nike

我正在尝试通过 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 之间完美运行,但是 socket.io 在使用 nginx 1.6 代理时花费的时间太长。握手协议(protocol)需要太多时间,但如果不中断,它最终会在几分钟后开始工作。

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 - 带有 nginx 的 Socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29043879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com