Ale*_*eks 19 nginx cors 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;
}
Run Code Online (Sandbox Code Playgroud)
它在浏览器和Node.js之间直接完美地工作,但是当使用nginx 1.6代理时,socket.io需要太长时间.握手协议需要花费太多时间,但如果保持不间断,它最终会在几分钟后开始工作.
nginx提供的静态文件完美运行.
可能是什么问题呢?
更新:
我分析了一下网络流量,并确定以下请求大约持续一分钟(这正是请求升级时):
Sec-WebSocket-Key: LhZ1frRdl+myuwyR/T03lQ==
Cookie: io=1-A7tpvwmoGbrSvTAAA5
Connection: keep-alive, Upgrade
Upgrade: websocket
....
Run Code Online (Sandbox Code Playgroud)
预期的响应是代码101和:
Connection: upgrade
Sec-WebSocket-Accept: HXx3KKJadQYjDa11lpK5y1nENMM=
Upgrade: websocket
...
Run Code Online (Sandbox Code Playgroud)
相反,浏览器接收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
Run Code Online (Sandbox Code Playgroud)
更新2:
我确定相同的配置在我的办公室计算机上完美运行,这意味着它是我的家用电脑问题.无论如何,确定到底出了什么问题会非常好.
小智 55
在正在运行的服务器中,这里使用的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";
}
Run Code Online (Sandbox Code Playgroud)
Del*_*con 20
我仍然有问题,就像我一样,这是您需要做的:对齐 3 个配置。假设您想通过“mysocket”进行通信:
宁克斯:
location /mysocket/ {
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;
proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)
节点:
const io = require('socket.io')(server, {
path: '/mysocket'
});
Run Code Online (Sandbox Code Playgroud)
客户:
var io = require('socket.io-client');
const socket = io('http://myserver.com, {
path: '/mysocket'
});
Run Code Online (Sandbox Code Playgroud)
一探究竟!这里最重要的部分是这两个迹象^~已经失去了3天,直到我设法让nginx与socketio工作..
location ^~ /socket {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
Run Code Online (Sandbox Code Playgroud)
小智 2
我见过的每个示例(Nginx 文档、Nginx 博客)都使用:
proxy_set_header Connection "upgrade";
Run Code Online (Sandbox Code Playgroud)
请注意所有小写的“升级”。你的例子有一个大写的“U”。也许值得尝试。
| 归档时间: |
|
| 查看次数: |
25991 次 |
| 最近记录: |