Search Our Database
How to set HAProxy load balancing using Session Cookie
Introduction
Prerequisites
- Access to your server via SSH.
- HAProxy installed on your server.
- Root or sudo privileges.
Step-by-Step Guide
Step 1: Locate and Open the HAProxy Configuration File
Using your SSH terminal, navigate to the location of the HAProxy configuration file and open it for editing.
vi /etc/haproxy/haproxy.cfg
Step 2: Add the Load Balancing Configuration
Edit the configuration file by adding the following code to set up HTTP load balancing for two web servers:
listen http_proxy 192.168.0.1:80 mode http balance roundrobin cookie SERVERID insert indirect nocache server web1 192.168.0.2:80 check cookie server web2 192.168.0.3:80 check cookie
- listen http_proxy 192.168.0.1:80: This line defines a listener on the HAProxy server for HTTP traffic on IP 192.168.0.1 and port 80.
- mode http: Specifies that this listener operates in HTTP mode.
- balance roundrobin: This instructs HAProxy to distribute incoming requests evenly across both servers (web1 and web2).
- cookie SERVERID insert indirect nocache: Adds a session cookie to track the server assigned to each user.
- server web1 192.168.0.2:80 check cookie: Defines the first backend server (web1) located at 192.168.0.2.
- server web2 192.168.0.3:80 check cookie: Defines the second backend server (web2) located at 192.168.0.3.
Step 3: Save and Exit
After editing, save the file and exit the editor. In vi, this can be done by typing:
:wq
Step 4: Restart HAProxy
To apply the changes, restart the HAProxy service:
sudo systemctl restart haproxy
Step 5: Test the Configuration
Verify the load balancing by sending multiple requests to http://192.168.0.1 and check that traffic is being distributed evenly between web1 and web2.
Conclusion
By following these steps, you can successfully configure HAProxy for HTTP load balancing between two web servers. This setup improves the distribution of traffic, ensuring better performance and reliability.
For additional assistance or if you encounter any issues, please contact our support team at support@ipserverone.com.