Can nginx be used as a reverse proxy for a backend websocket server?

前端 未结 7 2482
甜味超标
甜味超标 2020-11-30 19:51

We\'re working on a Ruby on Rails app that needs to take advantage of html5 websockets. At the moment, we have two separate \"servers\" so to speak: our main app running on

相关标签:
7条回答
  • 2020-11-30 19:52

    I use nginx to reverse proxy to a comet style server with long polling connections and it works great. Make sure you configure proxy_send_timeout and proxy_read_timeout to appropriate values. Also make sure your back-end server that nginx is proxying to supports http 1.0 because I don't think nginx's proxy module does http 1.1 yet.

    Just to clear up some confusion in a few of the answers: Keepalive allows a client to reuse a connection to send another HTTP request. It does not have anything to do with long polling or holding connections open until an event occurs which is what the original question was asking about. So it doesn't matter than nginx's proxy module only supports HTTP 1.0 which does not have keepalive.

    0 讨论(0)
  • 2020-11-30 19:54

    Out of the box (i.e. official sources) Nginx can establish only HTTP 1.0 connections to an upstream (=backend), which means no keepalive is possibe: Nginx will select an upstream server, open connection to it, proxy, cache (if you want) and close the connection. That's it.

    This is the fundamental reason frameworks requiring persistent connections to the backend would not work through Nginx (no HTTP/1.1 = no keepalive and no websockets I guess). Despite having this disadvantage there is an evident benefit: Nginx can choose out of several upstreams (load balance) and failover to alive one in case some of them failed.

    Edit: Nginx supports HTTP 1.1 to backends & keepalive since version 1.1.4. "fastcgi" and "proxy" upstreams are supported. Here it is the docs

    0 讨论(0)
  • 2020-11-30 20:00

    How about use my nginx_tcp_proxy_module module?

    This module is designed for general TCP proxy with Nginx. I think it's also suitable for websocket. And I just add tcp_ssl_module in the development branch.

    0 讨论(0)
  • 2020-11-30 20:08

    nginx (>= 1.3.13) now supports reverse proxying websockets.

    # the upstream server doesn't need a prefix! 
    # no need for wss:// or http:// because nginx will upgrade to http1.1 in the config below
    upstream app_server {
        server localhost:3000;
    }
    
    server {
        # ...
    
        location / {
            proxy_pass http://app_server;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
    
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
            proxy_redirect off;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 20:09

    For anyone that wondering about the same problem, nginx now officially supports HTTP 1.1 upstream. See nginx documentation for "keepalive" and "proxy_http_version 1.1".

    0 讨论(0)
  • 2020-11-30 20:11

    You can't use nginx for this currently[it's not true anymore], but I would suggest looking at HAProxy. I have used it for exactly this purpose.

    The trick is to set long timeouts so that the socket connections are not closed. Something like:

    timeout client  86400000 # In the frontend
    timeout server  86400000 # In the backend
    

    If you want to serve say a rails and cramp application on the same port you can use ACL rules to detect a websocket connection and use a different backend. So your haproxy frontend config would look something like

    frontend all 0.0.0.0:80
      timeout client    86400000
      default_backend   rails_backend
      acl websocket hdr(Upgrade)    -i WebSocket
      use_backend   cramp_backend   if websocket
    

    For completeness the backend would look like

    backend cramp_backend
      timeout server  86400000
      server cramp1 localhost:8090 maxconn 200 check
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题