HAProxy is an example of a free and reliable load balancer that also works as a reverse proxy. You can configure HAProxy for WebSocket connections to better utilize WebSockets’ features, especially on real-time data transfer, while minimizing the server load using HAProxy. This post presents all the required steps to configure HAProxy for WebSocket connections.
Step-by-Step Guide on How to Configure HAProxy for WebSocket Connections
With WebSocket connections, the server and client communication is long-lived. It persists until the server or client decides to close it. As such, having a way to work with a load balancer ensures that the traffic can be distributed to another server if the server is overloaded. That way, the client and server can enjoy an uninterrupted and long-lived connection at low latency.
1. Install HAProxy
The first step to configure HAProxy for WebSocket connections is to ensure that you have HAProxy installed. If you have it already installed, skip this step. Otherwise, run the following command to install it:
2. Edit the HAProxy Configuration
To configure HAProxy for WebSocket connections, we must edit the HAProxy configuration to support the WebSocket connections. We must modify the “Defaults” section and create the “Frontend” and “Backend” sections.
Open the HAProxy config using a text editor as follows:
In the “Defaults” section, modify it as presented in the following image. First, we specify that we are working with HTTP connections and define where to send the log files. Next, we set the timeouts for different activities. For instance, the timeout connect is the maximum time a connection attempt to the server should take. If the maximum time is reached, the connection is concluded to have failed and can be retried.
For this case, we set the timeout connect to 5000 milliseconds. You can adjust the timeout on how you see them best for your case. Adjust them based on your application, and remember the WebSocket sessions’ duration and how long-lived you expect them to be.
Next, we must create the “Frontend” section where we bind what ports to use for the WebSocket connections and what backend server to reference. For this case, we specify port 80 and give the URL to access the “Stats” page to monitor our HAProxy. Lastly, we define what backend section to reference in load balancing.
In the “Backend” section, we distribute the load using round robin. The “option forward for” specifies to HAProxy to include an “X-Forward-For” header that contains the client’s IP address when sending the connection request. That way, the backend server will get the correct client IP.
The “option http-server-close” restricts the resources by eliminating any idle connections from taking up the server resources. Although the WebSocket connections are long-lived, we must check for idle long-lived connections and close them to avoid unnecessary resource wastage.
Lastly, add the backend servers that you will use to distribute the traffic. Ensure that you give the correct IP address for the servers and replace the ones in the following image:
Your HAProxy is now configured for WebSocket connections. Save the changes and close the file.
3. Test for Validity
Before we restart HAProxy, we can run a quick command to check if the file is valid and has no error. For that, run the following command:
4. Restart and Test the WebSocket Connection
Once the file is valid, restart the HAProxy service.
That’s it. You configured HAProxy for WebSocket connections. You can test the connection to ensure that all the WebSocket traffic is forwarded correctly.
Conclusion
Configuring HAProxy for WebSocket connections is a sought-after solution, especially by web administrators. This post elaborated the process in detail, giving the steps and an example to ensure that you comfortably follow along and manage to implement the same in your case. Go through each step and ensure that you understand the concept and the process to implement it.