Use the Haproxy load balance to connect the WebSocket connection with the Tornado application?

I am using a Tornado application that uses a websocket handler. I am using Supervisord to run multiple instances of the application, but I cannot balance websocket connections.

I know that nginx does not support handling websockets out of the box, but I followed the instructions in http://www.letseehere.com/reverse-proxy-web-sockets to use the nginx tcp_proxy module to reverse proxy websocket connections. However, this does not work because the module cannot route websocket urls (for example: ws://localhost:80/something). So it does not work for the URL routing I defined in the Tornado application.

From my research on the network, it seems that HAProxy is the way to load balance my websocket connections. However, I can hardly find any decent guidance to set up HAProxy to load balance websocket connections and also be able to handle websocket URL routing.

I really appreciate some detailed instructions on how to achieve this goal. I also fully accept other solutions.

< /div>

It is not difficult to implement WebSocket in haproxy, but I admit that it is not easy to find the doc in this regard (hope this response will become an example). If you are using haproxy 1.4 (I think You are so), then it is like any other HTTP request, no action is required, because haproxy can recognize HTTP upgrades.

If you want to direct WebSocket traffic to a different server than the rest of HTTP Field, content switching rules should be used, in short:

 frontend pub-srv
bind :80
use_backend websocket if {hdr(Upgrade ) -i WebSocket }
default_backend http

backend websocket
timeout server 600s
server no de1 1.1.1.1:8080 check
server node2 2.2.2.2:8080 check

backend http
timeout server 30s
server www1 1.1.1.1:80 check
server www2 2.2.2.2:80 check

If you are using 1.5-dev, you can even specify “timeout tunnel” to make the WS connection timeout more than normal HTTP connection Long, so you can avoid using too long timeout on the client side.

You can also combine Upgrade: WebSocket into a specific URL:

 frontend pub-srv
bind :80
acl is_websocket hdr(Upgrade) -i WebSocket
acl is_ws_url path /something1 /something2 /something3
use_backend websocket if is_websocket is_ws_url
default_backend http

Finally, please don’t use the stupid 24-hour idle timeout that we sometimes see, it definitely doesn’t feel like
Establish a meeting now and wait for the customer for 24 hours. Network more< br>Mobile is very short-lived than in the 80s and connections. You will end up with many FIN_WAIT sockets
in vain. The current Internet has been around for 10 minutes.

Hope this helps!

I am using a Tornado application that uses a websocket handler. I am using Supervisord to run multiple instances of the application, but I cannot balance websocket connections.

I know that nginx does not support handling websockets out of the box, but I follow the instructions in http://www.letseehere.com/reverse-proxy-web-sockets to use the nginx tcp_proxy module to reverse Proxy websocket connection. However, this does not work because the module cannot route the websocket url (for example: ws://localhost:80/something). So it does not work with the URL routing I defined in the Tornado application.

From my research on the network, it seems that HAProxy is the way to load balance my websocket connections. However, it is difficult for me to find any decent guidance to set up HAProxy to load balance websocket connections and still be able to handle websocket URL routing.

I really appreciate some detailed instructions on how to achieve this. I also fully accept other solutions.

It is not difficult to implement WebSocket in haproxy, but I admit it is not easy to find the doc in this regard (hope this response will be an example). If you are using haproxy 1.4 (I think you are), then it is Like any other HTTP request, no action is required because haproxy can recognize HTTP upgrades.

If you want to direct WebSocket traffic to a different server farm than the rest of HTTP, you should use content switching rules , In short:

 frontend pub-srv
bind :80
use_backend websocket if {hdr(Upgrade) -i WebSocket }
default_backend http

backend websocket
timeout server 600s
server node1 1.1.1.1:8080 check
server node2 2.2.2.2:8080 check

backend http
timeout server 30s
server www1 1.1.1.1:80 check
server www2 2.2.2.2:80 check

If you are using 1.5-dev, you can even specify “timeout tunnel” to make the timeout of WS connection longer than normal HTTP connection, which can avoid using too long timeout on the client side.

You can also combine Upgrade: WebSocket into a specific URL:

 frontend pub-srv
bind :80
acl is_websocket hdr(Upgrade)- i WebSocket
acl is_ws_url path /something1 /something2 /something3
use_backend websocket if is_websocket is_ws_url
default_backend http

Finally, please do not use what we sometimes see The stupid 24-hour idle timeout, it definitely does not feel like
establishing meetings now and waiting for customers for 24 hours. The network is more
mobile than in the 80s and connections are very short-lived. You will end up with many FIN_WAIT sockets
Futility. The current Internet has been around for 10 minutes.

Hope this helps!

Leave a Comment

Your email address will not be published.