Nginx + Express + Socket.IO workarounds

As we know at this time nginx stable version can't proxy tcp connections. So if express and socket.io will work on the same port we need to use some other proxy solution.

But there are other ways to bypass this problem:

  1. What if we will set socket.io to listen different port, then express listens. For example: nginx proxies 80 port to 8000 port, express listens on 8000 port, socket.io listens to 8001 port and client connects to socket.io directly to 8001 port.
  2. Using nginx_tcp_proxy_module we can proxy tcp connections, but we can't use http on the same port. So we use such solution: nginx proxies 80 port to 8000 port and 81 port (for websockets) to 8001, express listens on 8000 port, socket.io listens on 8001 port and client connects to socket.io to 81 port.

What advantages and disadvantages do these approaches have?

I prefer to use Haproxy in front and have only one public open port. The "rooting" is done per path.

The config looks like that (you can find easily many tutorials/resources)

frontend all 0.0.0.0:80

    acl is_websocket path_beg /websocket/
    use_backend nodejs if is_websocket
    default_backend nginx

backend nodejs
   server srv_node 127.0.0.1:16852

backend nginx
   balance roundrobin
   server srv_static 127.0.0.1:8080

You can do it with Varnish and Nginx - http://blog.dealspotapp.com/post/40923117591/websockets-with-varnish-and-multiple-nginx-backends

or with Haproxy - http://blog.dealspotapp.com/post/41226162147/websockets-with-haproxy-ssl-and-multiple-backends

If you use Haproxy, make sure to set the tunnel timeout to something long, such as 1 day. otherwise you'll see new sockets being created every few seconds.