Configure HAProxy for L4 TCP & L7 HTTP Load Balancing
Learn how to set up HAProxy for Layer 4 and Layer 7 load balancing on Debian/Ubuntu and AlmaLinux/Rocky VPS or dedicated servers.
5 min read
Load balancing is a core component of high‑availability architectures. Whether you serve static sites, APIs, or complex web applications, spreading traffic across multiple backend servers improves performance, resilience, and scalability. HAProxy is a proven, open‑source solution that handles both Layer 4 (TCP) and Layer 7 (HTTP) traffic with minimal overhead. This guide walks through installing HAProxy, configuring TCP and HTTP load balancing, and verifying that traffic is correctly distributed across two web‑server pools.
1. Install HAProxy
HAProxy is included in the default repositories of most Linux distributions. Follow the steps that match the operating system of your AtoZNode VPS or dedicated server.
Debian / Ubuntu (apt)
# Refresh the local package index
sudo apt update
# Install HAProxy and its dependencies
sudo apt install -y haproxy
# Show the installed version
haproxy -v
What each command does:
apt update – updates the local list of available packages.
apt install haproxy – installs the HAProxy binary, systemd unit, and required libraries.
haproxy -v – prints the HAProxy version, confirming a successful installation.
dnf install epel-release – adds the Extra Packages for Enterprise Linux repository, which provides HAProxy.
dnf install haproxy – installs the HAProxy package and its service files.
haproxy -v – displays the installed version.
2. HAProxy Configuration Overview
The main configuration file is /etc/haproxy/haproxy.cfg. It is divided into four sections:
global – process‑wide settings (user, max connections, logging).
defaults – default values inherited by all frontend and backend blocks.
frontend – defines where HAProxy listens for client connections.
backend – defines the pool of servers that will handle the traffic.
The following minimal global and defaults blocks work for both TCP and HTTP use cases.
# /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
daemon
maxconn 2000
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
retries 3
The mode http setting in defaults can be overridden per‑frontend with mode tcp when you need Layer 4 load balancing.
3. Layer 4 TCP Load Balancing
TCP load balancing is ideal for services such as MySQL, PostgreSQL, SMTP, or any custom protocol that does not require HTTP inspection. The example below balances MySQL traffic on port 3306 across two backend servers.
backend mysql_back
mode tcp
balance roundrobin
option tcp-check
server db1 10.0.1.11:3306 check fall 3 rise 2
server db2 10.0.1.12:3306 check fall 3 rise 2
Key directives explained:
bind *:3306 – HAProxy listens on all interfaces for TCP port 3306.
mode tcp – disables HTTP parsing; traffic is treated as raw TCP.
balance roundrobin – distributes connections evenly across servers.
option tcp-check – performs a simple TCP connect test to verify backend health.
check fall 3 rise 2 – marks a server down after three consecutive failures and up after two successes.
4. Layer 7 HTTP Load Balancing
For web traffic HAProxy can inspect HTTP headers, route based on URL paths, and optionally terminate SSL. The configuration below sets up an HTTP load balancer on port 80 that directs general web requests to one pool and API requests to another.
Inspect the response or add a custom header (e.g., X-Backend: web1) on each web server to confirm which backend served the request.
6. Optional Enhancements
SSL termination – add bind *:443 ssl crt /etc/ssl/haproxy.pem to the HTTP frontend and keep backend connections on plain HTTP.
Sticky sessions – for stateful applications, include cookie SERVERID insert indirect nocache in the backend and cookie SERVERID on each server line.
Health‑check tuning – adjust inter, fall, and rise values to match your application’s response characteristics.
Logging – configure rsyslog or the systemd journal to capture HAProxy logs for troubleshooting.
Conclusion
HAProxy offers a flexible, high‑performance platform for both Layer 4 TCP and Layer 7 HTTP load balancing. By installing the package from the native repositories, defining clear frontend and backend sections, and using appropriate health checks, you can build a resilient front‑end for any web‑server pool hosted on AtoZNode. The same configuration works on Debian/Ubuntu and AlmaLinux/Rocky systems, keeping your infrastructure consistent across different VPS or dedicated‑server deployments.