🔥 1️⃣ What is HAProxy?¶
HAProxy = High Availability Proxy
It works as:
- Layer 4 (TCP) load balancer
- Layer 7 (HTTP) load balancer
- Reverse proxy
- SSL terminator
Used in:
- SaaS platforms
- Fintech systems
- Large-scale web apps
🔥 2️⃣ Install HAProxy (Local)¶
🟢 Windows (Docker Recommended)¶
🟢 Ubuntu¶
Config file location:
🔥 3️⃣ Basic HAProxy Config Structure¶
Main config file:
Structure:
🔥 4️⃣ Simple Reverse Proxy Example¶
Goal:
haproxy.cfg¶
global
daemon
maxconn 256
defaults
mode http
timeout connect 5s
timeout client 50s
timeout server 50s
frontend http_front
bind *:80
default_backend app_servers
backend app_servers
server app1 127.0.0.1:3000
Restart:
🔥 5️⃣ Load Balancing (Multiple Servers)¶
backend app_servers
balance roundrobin
server app1 127.0.0.1:3000 check
server app2 127.0.0.1:3001 check
Algorithms:
- roundrobin
- leastconn
- source
- uri
🔥 6️⃣ Health Checks¶
Notice:
HAProxy checks if backend is alive.
If one fails:
- Traffic automatically stops to that server.
You can customize:
🔥 7️⃣ SSL Termination¶
First generate certificate:
Config:
Now HAProxy handles HTTPS.
🔥 8️⃣ Sticky Sessions (Important for Stateful Apps)¶
Used when:
- Sessions stored in memory
- Need same user → same backend
backend app_servers
balance roundrobin
cookie SERVERID insert indirect nocache
server app1 127.0.0.1:3000 check cookie s1
server app2 127.0.0.1:3001 check cookie s2
🔥 9️⃣ HAProxy Stats Dashboard¶
Add:
Access:
Shows live backend status.
🔥 10️⃣ HAProxy With Docker Compose¶
docker-compose.yml¶
services:
backend1:
image: nginx
expose:
- "80"
backend2:
image: nginx
expose:
- "80"
haproxy:
image: haproxy:latest
ports:
- "80:80"
- "8404:8404"
volumes:
- ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
haproxy.cfg¶
global
daemon
defaults
mode http
frontend http_front
bind *:80
default_backend web
backend web
balance roundrobin
server web1 backend1:80 check
server web2 backend2:80 check
listen stats
bind *:8404
stats enable
stats uri /
Run:
🔥 11️⃣ Layer 4 (TCP Mode)¶
For database load balancing:
frontend tcp_front
bind *:3306
default_backend mysql_servers
backend mysql_servers
mode tcp
balance leastconn
server db1 127.0.0.1:3306 check
🔥 12️⃣ Advanced Routing (Path-Based)¶
frontend http_front
bind *:80
acl is_api path_beg /api
use_backend api_servers if is_api
default_backend web_servers
🔥 13️⃣ Production Best Practices¶
- Enable logging
- Set maxconn properly
- Use timeouts
- Enable health checks
- Use SSL offloading
- Monitor stats endpoint
- Use keep-alive tuning
🔥 14️⃣ Common Errors¶
502 → Backend down 503 → No backend available Connection refused → Wrong IP/Port Timeout → Backend slow
🔥 15️⃣ HAProxy vs NGINX¶
| HAProxy | NGINX |
|---|---|
| Strong L4 | Strong static serving |
| Enterprise LB | Web server + LB |
| Very fast | More flexible |