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

前端 未结 7 2515
甜味超标
甜味超标 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条回答
  •  萌比男神i
    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
    

提交回复
热议问题