Installation
Bash
# Ubuntu / Debian
sudo apt update && sudo apt install -y haproxy
# CentOS / RHEL
sudo yum install -y haproxy
# Check version
haproxy -v
# Start the service
sudo systemctl enable --now haproxyBasic Configuration Structure
CODE
/etc/haproxy/haproxy.cfgHAPROXY
global
log /dev/log local0
maxconn 50000
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
option forwardfor # Add X-Forwarded-For header
option http-server-close
# Frontend (accept client connections)
frontend web_front
bind *:80
bind *:443 ssl crt /etc/ssl/mysite.pem
redirect scheme https if !{ ssl_fc } # HTTP → HTTPS redirect
default_backend web_back
# Backend (server pool)
backend web_back
balance roundrobin # Algorithm: roundrobin, leastconn, source
option httpchk GET /health # Health check path
server web1 192.168.1.10:3000 check weight 1
server web2 192.168.1.11:3000 check weight 1
server web3 192.168.1.12:3000 check weight 2 # 2x weightHealth Check Configuration
HAPROXY
backend api_back
option httpchk GET /api/health HTTP/1.1\r\nHost:\ api.example.com
http-check expect status 200
server api1 10.0.0.1:8080 check inter 2s rise 2 fall 3
server api2 10.0.0.2:8080 check inter 2s rise 2 fall 3
# inter: check interval, rise: successes to mark up, fall: failures to mark down
# Backup server (used when all servers are down)
server backup 10.0.0.99:8080 check backupSticky Sessions (Session Persistence)
HAPROXY
backend app_back
balance leastconn
cookie SERVERID insert indirect nocache # Cookie-based sticky sessions
server app1 10.0.0.1:3000 check cookie app1
server app2 10.0.0.2:3000 check cookie app2Stats Dashboard
HAPROXY
frontend stats
bind *:8404
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:StrongPass123 # Basic authentication
stats show-legendsOpen http://SERVER_IP:8404/stats in a browser
Validate Configuration and Reload
Bash
# Check configuration syntax
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
# Zero-downtime reload (keeps existing connections)
sudo systemctl reload haproxy
# Check status
sudo systemctl status haproxyEditorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)OpenSSL documentation ↗
Questions & answers
Ask a question about this guide. We'll review and reply.