“网络套接字连接无效";在node.js服务器上使用nginx时 [英] "websocket connection invalid" when using nginx on node.js server
问题描述
我正在使用Express.js创建可以通过Web套接字连接到的服务器.
I'm using Express.js to create a server to which I can connect using web sockets.
即使它最终似乎可以工作(即连接并向客户端传递事件),我最初还是会在Chrome的控制台中遇到错误:
Even though it eventually seems to work (that, is connects and passes an event to the client), I initially get an error in Chrome's console:
意外的响应代码:502
Unexpected response code: 502
在后端,socket.io仅记录warn - websocket connection invalid
.
On the backend, the socket.io only logs warn - websocket connection invalid
.
但是,nginx会记录以下内容:
However, nginx logs this:
2012/02/12 23:30:03 [错误] 25061#0:* 81上游提前关闭 从客户端读取响应标头时进行连接: 71.122.117.15,服务器:www.example.com,请求:"GET/socket.io/1/websocket/1378920683898138448 HTTP/1.1",上游: " http://127.0.0.1:8090/socket.io/1/websocket /1378920683898138448 ", 主持人:"www.example.com"
2012/02/12 23:30:03 [error] 25061#0: *81 upstream prematurely closed connection while reading response header from upstream, client: 71.122.117.15, server: www.example.com, request: "GET /socket.io/1/websocket/1378920683898138448 HTTP/1.1", upstream: "http://127.0.0.1:8090/socket.io/1/websocket/1378920683898138448", host: "www.example.com"
注意:我正在运行nginx dev:nginx version: nginx/1.1.14
,因此它应该支持HTTP/1.1.
Note: I have nginx dev running: nginx version: nginx/1.1.14
so it should support HTTP/1.1.
还请注意,如果我只使用不带nginx的node.js服务器,它将在没有任何警告的情况下正常工作.
Also note that if I just use the node.js server without the nginx it works without any warnings.
最后,这是我的nginx配置文件:
Finally, here is my nginx config file:
server {
listen 0.0.0.0:80;
server_name www.example.com;
access_log /var/log/nginx/example.com.log;
location / {
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 true;
proxy_pass http://node;
proxy_redirect off;
}
}
upstream node {
server 127.0.0.1:8090;
}
任何帮助将不胜感激.我尝试了此问题中建议的修复程序,但是没有成功.也不行.
Any help would be greatly appreciated. I tried the fix suggested in this question but that didn't work either.
推荐答案
nginx仅在不稳定的1.1分支中具有某种Web套接字支持.请参阅Socket.IO wiki .
nginx has some kind of Web Socket support in unstable 1.1 branch only. See Socket.IO wiki.
Afaik当前只有少数稳定的基于Node.js的http代理能够正确支持Web套接字.
Afaik there are currently only few stable Node.js based http proxies that support Web Sockets properly.
查看node-http-proxy(我们使用它):
Check out node-http-proxy (we use this):
https://github.com/nodejitsu/node-http-proxy
和有弹性:
https://github.com/substack/bouncy
或者您可以使用纯TCP代理,例如 HAproxy
Or you can use pure TCP proxy such as HAproxy
nginx(1.3.13> =)开箱即用支持websockets!
nginx (1.3.13>=) supports websockets out of the box!
http://nginx.org/en/docs/http/websocket.html
这篇关于“网络套接字连接无效";在node.js服务器上使用nginx时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!