HAProxy guide
What HAProxy does in production
HAProxy sits in front of application servers and distributes incoming connections according to configured rules. It handles HTTP routing (host/path ACLs), TCP passthrough, TLS termination, session persistence, and automatic removal of unhealthy backends. Unlike a general web server, HAProxy is specialized for proxying and load balancing at scale.
How a request is handled
For HTTP mode, the typical workflow is:
- Bind — a
frontendaccepts connections on an address:port - ACL evaluation — optional
aclrules match host, path, headers, or source IP - Backend selection —
use_backendordefault_backendpicks abackendpool - Server choice — the load-balancing algorithm (
balance) picks a healthyserverline - Health check — servers marked DOWN are skipped until checks pass again
- Proxy — HAProxy forwards the request and streams the response; stats and logs record the result
Configuration structure
Main config is usually /etc/haproxy/haproxy.cfg, organized in sections:
global— process-wide settings (user, max connections, stats socket)defaults— inherited settings for frontends and backends (timeouts, mode, logging)frontend— client-facing listener; binds ports and routes to backendsbackend— pool of servers with balancing and health checkslisten— combined frontend + backend in one block (common for simple setups)
Validate before reload: haproxy -c -f /etc/haproxy/haproxy.cfg.
Key concepts to know
mode httpvsmode tcp— L7 HTTP routing vs L4 passthroughbalance roundrobin— distribute requests evenly (also:leastconn,sourcefor sticky)- Health checks —
option httpchkor TCP checks; servers go UP/DOWN automatically - Stats page — built-in monitoring UI on a dedicated bind (often port 8404)
- ACLs —
acl is_api path_beg /apithenuse_backend api_servers if is_api - X-Forwarded-For —
option forwardforpasses client IP to backends
HAProxy vs nginx
Nginx can reverse-proxy and load-balance but HAProxy is purpose-built for advanced balancing, health checks, and high-connection throughput. Many architectures use HAProxy as the edge load balancer and nginx or app servers behind it.
Learning resources
- HAProxy documentation — haproxy.org/#docs (official docs index)
- Configuration manual — haproxy-dconv configuration (directive reference)
- Starter guide — haproxy.com/blog/haproxy-basic-configuration-tutorial (introductory walkthrough)
Practice scenarios
Hands-on HAProxy scenarios on live Linux VMs: haproxy