Back to articles
AI & Cloud

Complete Guide to Configuring Load Balancing for Web Server Clusters Using HAProxy on VPS 1

A detailed guide to configuring HAProxy for load balancing web server clusters on VPS, including installation, configuration, testing, and performance optimization.

DPTCloud Engineering4 min read
Complete Guide to Configuring Load Balancing for Web Server Clusters Using HAProxy on VPS 1

Introduction to Load Balancing and HAProxy

Load balancing distributes incoming traffic across multiple web servers to optimize performance, increase availability, and improve scalability. In self-hosted environments using VPS, HAProxy is a widely used, efficient, and free solution for implementing load balancing. It functions as a reverse proxy, receiving user requests and forwarding them to backend servers based on a defined balancing strategy.

This guide walks through configuring HAProxy to balance traffic for a web server cluster (e.g., Nginx or Apache) on VPS. The instructions are compatible with Linux distributions such as Ubuntu 20.04/22.04 and Debian 11/12.

Requirements and Preparation

To complete this guide, you will need:

  • One or more VPS instances (running Ubuntu 20.04/22.04 or Debian 11/12)
  • At least two web server backend instances (Nginx or Apache installed)
  • A VPS dedicated as the HAProxy frontend to receive and distribute traffic
  • Stable network connectivity between all VPS instances
  • Root or sudo access on all machines

Configuring HAProxy on VPS

We will install and configure HAProxy on the VPS acting as the load balancer.

1. Install HAProxy

On the VPS, update the package list and install HAProxy:

sudo apt update
sudo apt install haproxy -y

2. Configure HAProxy

Open the configuration file:

sudo nano /etc/haproxy/haproxy.cfg

Replace the file content with the following configuration (adjust backend IPs as needed):

global
    log /dev/log local0
    log /dev/log local1 notice
    maxconn 4096
    user haproxy
    group haproxy
    daemon

defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http_front
    bind *:80
    default_backend web_servers

backend web_servers
    balance roundrobin
    option httpchk GET /health
    http-check expect status 200
    server web1 192.168.1.101:80 check
    server web2 192.168.1.102:80 check

Explanation:

  • frontend http_front: Listens on port 80 (HTTP) for incoming requests.
  • backend web_servers: Defines the list of backend web servers.
  • balance roundrobin: Uses round-robin balancing strategy.
  • httpchk GET /health: Checks server health via the /health endpoint.
  • check: Enables backend health checks.

3. Start and Verify HAProxy

Restart and verify its status:

sudo systemctl restart haproxy
sudo systemctl status haproxy

If no errors are shown, it is running successfully.

Testing and Performance Optimization

To verify load balancing effectiveness:

1. Test with curl

Run from a client machine or another VPS:

curl -I http://<HAProxy-IP>

Access the website and confirm traffic is evenly distributed across backends.

2. Check HAProxy Status

Access the admin interface (if enabled):

sudo systemctl enable haproxy

Visit http:///admin to monitor backend status and connection counts.

Troubleshooting Common Issues

  • HAProxy fails to start: Check logs at /var/log/haproxy.log.
  • Backends not receiving traffic: Verify network connectivity, firewall rules, and backend server status.
  • Uneven traffic distribution: Review balance settings and backend health status.

References

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478