skip to content

Search

Syspirit
EN

HAProxy

HAProxy load balancer - configuration, backend and high-performance monitoring!

HAProxy is an open source load balancer and proxy server designed for high availability, load distribution and proxying for TCP and HTTP applications.

Service Management

ActionCommand
Start HAProxysudo systemctl start haproxy
Stop HAProxysudo systemctl stop haproxy
Restart HAProxysudo systemctl restart haproxy
Reload configurationsudo systemctl reload haproxy
Service statussudo systemctl status haproxy
Enable at bootsudo systemctl enable haproxy
Test configurationsudo haproxy -f /etc/haproxy/haproxy.cfg -c
HAProxy versionhaproxy -v

Configuration

File/DirectoryDescription
/etc/haproxy/haproxy.cfgMain configuration
/var/log/haproxy.logHAProxy logs
/var/lib/haproxy/statsStatistics socket
/etc/default/haproxyService configuration
/etc/rsyslog.confSystem logs configuration

Configuration Structure

Main Sections

SectionDescriptionUsage
globalGlobal configurationProcess, logs, security
defaultsDefault parametersTimeouts, mode, options
frontendRequest entry pointListening, routing
backendDestination serversLoad balancing, health
listenCombined Frontend + BackendSimple configuration

Basic Configuration

Simple HTTP Configuration

global
    daemon
    user haproxy
    group haproxy
    chroot /var/lib/haproxy
    stats socket /var/run/haproxy.sock mode 660
 
defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms
 
frontend web_frontend
    bind *:80
    default_backend web_servers
 
backend web_servers
    balance roundrobin
    server web1 192.168.1.10:8080 check
    server web2 192.168.1.11:8080 check
    server web3 192.168.1.12:8080 check

HTTPS Configuration with SSL

frontend https_frontend
    bind *:443 ssl crt /etc/ssl/certs/example.pem
    redirect scheme https if !{ ssl_fc }
    default_backend web_servers
 
backend web_servers
    balance roundrobin
    option httpchk GET /health
    server web1 192.168.1.10:8080 check
    server web2 192.168.1.11:8080 check backup

Load Balancing Algorithms

AlgorithmDescriptionConfiguration
roundrobinCircular rotationbalance roundrobin
leastconnLeast connectionsbalance leastconn
sourceSource IP hashbalance source
uriURI hashbalance uri
url_paramURL parameter hashbalance url_param id
randomRandom selectionbalance random
firstFirst available serverbalance first

Configuration Examples

# Load balancing by source IP (sticky session)
backend api_servers
    balance source
    hash-type consistent
    server api1 10.0.1.10:3000 check
    server api2 10.0.1.11:3000 check
 
# Load balancing by URI
backend static_servers
    balance uri whole
    server static1 10.0.2.10:80 check
    server static2 10.0.2.11:80 check

Health Checks and Monitoring

Check TypeConfigurationExample
Simple TCPcheckserver web1 ip:port check
HTTP GEToption httpchk GET /pathoption httpchk GET /health
HTTP with headersoption httpchk GET /path HTTP/1.1\r\nHost:\ domainCustom headers
Check intervalcheck inter 5sCheck every 5s
Retry countcheck rise 2 fall 32 OK for UP, 3 KO for DOWN

Advanced Health Check Configuration

backend app_servers
    option httpchk GET /api/health HTTP/1.1\r\nHost:\ api.example.com
    server app1 10.0.1.10:3000 check inter 10s rise 2 fall 3
    server app2 10.0.1.11:3000 check inter 10s rise 2 fall 3
    server app3 10.0.1.12:3000 check inter 10s rise 2 fall 3 backup

Statistics Interface

Stats Configuration

# In the frontend or as a listen section
listen stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 30s
    stats admin if TRUE
    stats auth admin:password123

Accessing Statistics

ActionURL/Command
Web interfacehttp://server:8404/stats
JSON statisticshttp://server:8404/stats?stats;json
Disable a serverWeb UI or echo "disable server backend/server1" | socat stdio /var/run/haproxy.sock
Enable a serverecho "enable server backend/server1" | socat stdio /var/run/haproxy.sock
Real-time statisticsecho "show stat" | socat stdio /var/run/haproxy.sock

Advanced Configuration

SSL/TLS and Security

global
    ssl-default-bind-ciphers ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20:!aNULL:!SHA1:!AESCCM
    ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
 
frontend https_frontend
    bind *:443 ssl crt /etc/ssl/certs/ alpn h2,http/1.1
 
    # Security headers
    http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains"
    http-response set-header X-Frame-Options "DENY"
    http-response set-header X-Content-Type-Options "nosniff"
 
    # HTTPS redirection
    redirect scheme https if !{ ssl_fc }

Content-Based Routing

frontend web_frontend
    bind *:80
 
    # ACLs for routing
    acl is_api path_beg /api/
    acl is_static path_beg /static/
    acl is_admin hdr(host) -i admin.example.com
 
    # Conditional routing
    use_backend api_servers if is_api
    use_backend static_servers if is_static
    use_backend admin_servers if is_admin
    default_backend web_servers

Rate Limiting

# Global rate limiting
frontend web_frontend
    stick-table type ip size 100k expire 30s store http_req_rate(10s)
    http-request track-sc0 src
    http-request deny if { sc_http_req_rate(0) gt 20 }

Logs and Monitoring

ActionConfiguration/Command
Enable HTTP logsoption httplog
Detailed TCP logsoption tcplog
Logs to sysloglog 127.0.0.1:514 local0
View real-time logssudo tail -f /var/log/haproxy.log
Statistics via socketecho "show info" | socat stdio /var/run/haproxy.sock
Server stateecho "show servers state" | socat stdio /var/run/haproxy.sock

Log Configuration

global
    log 127.0.0.1:514 local0 info
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
 
defaults
    log global
    option httplog
    option dontlognull
    option log-health-checks

Common Troubleshooting

ProblemSolution
Configuration errorhaproxy -f /etc/haproxy/haproxy.cfg -c
Service won’t startCheck syntax and permissions
Backend unreachableCheck health checks and connectivity
Degraded performanceAdjust timeouts and LB algorithm
Stats inaccessibleCheck stats bind and authentication
SSL issuesCheck certificates and paths

Management Commands

ActionCommand
General statisticsecho "show info" | socat stdio /var/run/haproxy.sock
Backend stateecho "show stat" | socat stdio /var/run/haproxy.sock
Disable serverecho "disable server backend/server1" | socat stdio /var/run/haproxy.sock
Enable serverecho "enable server backend/server1" | socat stdio /var/run/haproxy.sock
Change server weightecho "set weight backend/server1 50%" | socat stdio /var/run/haproxy.sock
Active sessionsecho "show sess" | socat stdio /var/run/haproxy.sock
Clear sessionsecho "clear counters all" | socat stdio /var/run/haproxy.sock

Practical Tips

Best Practices

  • Always test configuration: haproxy -f /etc/haproxy/haproxy.cfg -c
  • Monitor stats via web interface :8404/stats
  • Enable logs to diagnose issues
  • Use leastconn for variable loads
  • Configure a whitelist for admin IPs