“websocket连接无效"在 node.js 服务器上使用 nginx 时 [英] "websocket connection invalid" when using nginx on node.js server

查看:62
本文介绍了“websocket连接无效"在 node.js 服务器上使用 nginx 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Express.js 创建一个服务器,我可以使用网络套接字连接到该服务器.

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

在后端,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 支持.请参阅 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

这篇关于“websocket连接无效"在 node.js 服务器上使用 nginx 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆