nginx 1.2.0-socket.io-HTTP/1.1-代理websocket连接 [英] nginx 1.2.0 - socket.io - HTTP/1.1 - Proxy websocket connections

查看:159
本文介绍了nginx 1.2.0-socket.io-HTTP/1.1-代理websocket连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用nginx proxy_pass模块替换我的node-http-proxy模块.如我所读,新发布的nginx版本是否有可能开箱即用支持HTTP/1.1.我看到一些线程在为这个问题而苦苦挣扎,nginx不支持websocket.

i would like to replace my node-http-proxy module with nginx proxy_pass module. Is it possible with new released nginx version, as i have read, that it supports HTTP/1.1 out of the box. I saw some threads struggeling with that problem, that websockets are not supported by nginx.

在我的情况下,我在后台运行多个节点项目,并希望根据域将我的Websocket连接从端口80路由到8000-8100.有没有一种不使用tcp_module插件即可进行websocket代理/反向代理的本地方法?

In my case im running several node projects in background and want to route my websocket connections from port 80 to 8000-8100, depending on domain. Is there a native way to do websocket proxy/reverse proxy without using the tcp_module addon?

我尝试通过proxy_passing在nginx.conf中设置上游,但是如果我尝试通过websocket连接到端口80,则会收到502网关错误.

I tried to setup an upstream in nginx.conf with proxy_passing to it, but if i try to connect to port 80 over websocket, i get an 502 Gateway error.

有人遇到同样的问题吗? 是否有人通过端口80代理nginx + spcket.io的有效示例?

Anyone facing the same problem? Does anyone have a working example for nginx + spcket.io, proxying over port 80?

推荐答案

否,目前尚不可能; nginx 1.2合并了来自1.1.x开发分支的内容,该分支确实包括HTTP/1.1反向代理. Websocket连接是使用HTTP/1.1升级"标头建立的,但是nginx现在支持这种标头的事实并不意味着它支持websockets(websockets是一种不同的协议,不是HTTP). (我自己使用1.1.x分支进行了尝试(我发现该分支足够稳定以达到我的目的),没有tcp_module它将无法正常工作)

No, this is not yet possible; nginx 1.2 incorporates stuff from the 1.1.x development branch which indeed includes HTTP/1.1 reverse proxying. Websocket connections are established using the HTTP/1.1 "Upgrade" header, but the fact that nginx now supports this kind of headers does not mean it supports websockets (websockets are a different protocol, not HTTP). (I tried this myself using the 1.1.x branch (which I found to be stable enough for my purpose) and it doesn't work without the tcp_module)

Websocket可能在1.3.x中受支持( http://trac.nginx.org/nginx/roadmap ).

Websockets will probably be supported in 1.3.x ( http://trac.nginx.org/nginx/roadmap ).

您的替代方法是:

  • 继续使用node-http-proxy
  • 在不带tcp模块的情况下使用nginx; socket.io不会使用websocket,而是使用其他方法(例如长轮询)
  • 带有tcp模块的nginx:在这种情况下,我认为您需要为此模块提供一个额外的端口(请勿自己尝试使用此端口)
  • 将其他内容作为反向代理放在前面:我在nginx和node前面使用HAProxy(支持websocket). Nginx现在仅充当静态文件服务器,而不是代理.如果您想要额外的缓存,则可以使用Varnish.

这篇关于nginx 1.2.0-socket.io-HTTP/1.1-代理websocket连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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