Back to articles
Technology Insight

A Guide to High Availability: How to Configure HAProxy Load Balancing for Web Server Clusters on a VPS

May 29, 2026

Introduction to Modern Web Architecture and High Availability

In the digital business landscape, application uptime and responsiveness are directly tied to revenue and user satisfaction. When a web application relies on a single server, it inherently possesses a Single Point of Failure (SPOF). If that server experiences a hardware malfunction, a sudden traffic spike, or resource exhaustion, your entire business goes offline. To mitigate this risk, modern enterprise infrastructure utilizes server clustering managed by a dedicated Load Balancer.

Among the tools available for traffic distribution, HAProxy (High Availability Proxy) stands out as an industry standard. It is a free, very fast, and reliable reverse proxy and load balancer tailored for TCP and HTTP-based applications. By placing HAProxy in front of a cluster of Virtual Private Servers (VPS), you can distribute incoming user requests evenly across multiple backend web servers. This not only enhances performance but also guarantees seamless high availability.

Understanding the Architecture: The HAProxy Ecosystem

Before diving into the configuration, it is essential to visualize how a load-balanced architecture functions. Instead of users connecting directly to your Apache, Nginx, or Node.js web servers, all incoming traffic is directed to the HAProxy instance. HAProxy then evaluates the traffic and routes it to an available backend server based on predefined algorithms and health checks.

Core Components of HAProxy

  • Frontend: This section defines how HAProxy listens for connections. It specifies the IP addresses and ports (such as port 80 for HTTP or 443 for HTTPS) that clients connect to.
  • Backend: A pool of target web servers that receive the forwarded requests. HAProxy monitors these servers to ensure they are healthy before routing traffic to them.
  • Load Balancing Algorithms: The logic used to determine which backend server handles a specific request. Common options include Round Robin (sequential distribution) and Least Connections (routing to the server with the fewest active sessions).

Prerequisites for Implementation

To successfully implement this setup, you will require a minimum of three separate VPS instances residing within the same network or accessible via public IPs:

  1. HAProxy Node: Operating System: Ubuntu 22.04 / 24.04 LTS (IP: 192.168.1.10)
  2. Web Server 01: Nginx or Apache installed (IP: 192.168.1.11)
  3. Web Server 02: Nginx or Apache installed (IP: 192.168.1.12)
Note: Ensure that all servers have active firewalls configured to allow communication between the load balancer and the web nodes on ports 80 and 443.

Step-by-Step Guide to Configuring HAProxy

Step 1: Installing HAProxy on the Load Balancer VPS

First, access your dedicated HAProxy VPS via SSH. It is critical to update your local package index before installing the software to ensure you receive the latest stable version available in the repository.

sudo apt update
sudo apt install haproxy -y

Once the installation process completes, verify that the HAProxy service is active and running smoothly using the systemctl utility:

sudo systemctl status haproxy

Step 2: Configuring the Web Server Nodes

For testing purposes, ensure that your two web servers are running and serve a slightly different index page so you can visually confirm that the load balancer is toggling between them. For instance, modify the index.html file on Web Server 01 to display "Welcome to Web Server 01" and on Web Server 02 to display "Welcome to Web Server 02".

Step 3: Modifying the HAProxy Configuration File

The primary configuration file for HAProxy is located at /etc/haproxy/haproxy.cfg. Before making any modifications, it is highly recommended to create a backup copy of the original file:

sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

Next, open the file using a text editor like nano:

sudo nano /etc/haproxy/haproxy.cfg

Scroll to the bottom of the file and append the following configuration blocks to define your frontend and backend infrastructure:

frontend http_front
    bind *:80
    stats uri /haproxy?stats
    default_backend http_back

backend http_back
    balance roundrobin
    server web1 192.168.1.11:80 check
    server web2 192.168.1.12:80 check

Let us break down what this configuration accomplishes:

  • bind *:80: Instructs HAProxy to listen for all incoming HTTP traffic on port 80.
  • stats uri: Enables a built-in monitoring dashboard accessible via your browser.
  • balance roundrobin: Specifies that traffic should be distributed evenly and sequentially between the backend servers.
  • check: Directs HAProxy to perform regular health checks on the backend servers. If a server stops responding, HAProxy automatically routes traffic away from it.

Step 4: Validating and Restarting the HAProxy Service

Before restarting the daemon, always validate the syntax of your configuration file to prevent downtime due to typos or errors:

haproxy -c -f /etc/haproxy/haproxy.cfg

If the output reads "Configuration file is valid", you can safely restart HAProxy to apply the changes:

sudo systemctl restart haproxy

Testing and Monitoring the Infrastructure

With the setup complete, open a web browser and navigate to the public IP address of your HAProxy load balancer. Refresh the page multiple times. You should observe the page alternating between the content of Web Server 01 and Web Server 02. This behavior confirms that the Round Robin algorithm is executing correctly.

To view the performance metrics of your cluster, navigate to http://[Your_HAProxy_IP]/haproxy?stats. This administrative dashboard displays real-time data regarding data transfer speeds, active connection counts, and the health status of each individual backend node.

Advanced Optimization: Session Stickiness and SSL Termination

While a basic Round Robin setup suffices for static sites, modern enterprise applications often require advanced handling:

1. Session Stickiness (Sticky Sessions)

If your web application requires users to stay logged into a specific server to maintain session states, standard round-robin distribution will disrupt the user experience. By utilizing cookies, you can configure HAProxy to bind a user's session to the specific backend server that handled their initial request.

2. SSL Termination

Decrypting SSL/TLS traffic requires significant CPU processing power. By offloading SSL decryption to HAProxy (SSL Termination), you free up valuable CPU cycles on your backend web servers, allowing them to focus entirely on processing application logic and serving data efficiently.

Conclusion

Implementing HAProxy on a VPS cluster is a highly effective, cost-efficient strategy to achieve scalability and high availability for your digital products. By routing traffic through a centralized, intelligent proxy, you secure your infrastructure against unexpected hardware failures and handle traffic influxes with ease. As your operational demands grow, you can seamlessly add more backend web servers to this configuration without interrupting the end-user experience.

A Guide to High Availability: How to Configure HAProxy Load Balancing for Web Server Clusters on a VPS | DPTCloud