nginx 1.2.0 - socket.io - HTTP/1.1 - Proxy websock

2019-03-08 13:04发布

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.

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?

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.

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

2条回答
我命由我不由天
2楼-- · 2019-03-08 13:06

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)

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

Your alternatives are:

  • keep using node-http-proxy
  • use nginx without tcp module; socket.io won't use websockets but something else (e.g. long polling)
  • nginx with tcp module: in this case I think you need an additional port for this module (never tried this myself)
  • put something else in front as a reverse proxy: I use HAProxy (which supports websockets) in front of nginx and node. Nginx now simply acts as a static fileserver, not a proxy. Varnish is another option, if you want additional caching.
查看更多
地球回转人心会变
3楼-- · 2019-03-08 13:06

In relation to NginX with TCP module there are few problems I have encountered. But the most tricky one is trying to run your websockets with nginx on port 80 on EC2 instance.

I described whole configuration here

查看更多
登录 后发表回答