What HAProxy Does
- Load balancing: spreads connections or requests across several backend servers. See what is a load balancer.
- Reverse proxy: receives client requests and forwards them, so the backends can stay on private addresses. See what is a reverse proxy.
- TCP and HTTP modes: in TCP mode it forwards any TCP stream, such as a database connection or TLS passed through unchanged; in HTTP mode it reads each request and can route by host name, path, or header.
- TLS termination: it can hold the certificate and talk to the backends over a private network.
Nginx covers much of the same ground. HAProxy is a dedicated proxy with no web server inside, detailed health checks, and a live statistics page. For one site with one backend, an Nginx reverse proxy is often simpler.
Install HAProxy
sudo apt install haproxy # Ubuntu, Debian
sudo dnf install haproxy # AlmaLinux, Rocky Linux
haproxy -v
Distribution packages trail the newest releases, which is fine for most setups.The configuration file is /etc/haproxy/haproxy.cfg. On Ubuntu and Debian the service starts after installation; on AlmaLinux and Rocky Linux, run sudo systemctl enable --now haproxy once your configuration is in place.
Frontends and Backends
The packaged file starts with a global and a defaults section for logging and timeouts; keep them and add your own sections below. A frontend says where HAProxy listens, and a backend lists the servers that answer:
frontend web
bind *:80
mode http
option forwardfor
default_backend app_servers
backend app_servers
mode http
balance roundrobin
option httpchk
http-check send meth GET uri /health ver HTTP/1.1 hdr Host example.com
server app1 10.0.0.11:8080 check
server app2 10.0.0.12:8080 check
option forwardfor adds an X-Forwarded-For header, so the application sees the visitor's address instead of HAProxy's. Let the backends accept connections only from HAProxy's address.
Balancing Algorithms
| Algorithm | Sends a new connection to |
|---|---|
roundrobin | The next server in turn |
leastconn | The server with the fewest connections |
source | The same server for the same client IP |
first | The first server below its connection limit |
roundrobin suits short HTTP requests; leastconn suits long sessions such as WebSockets or database connections. Add weight 2 to a server line to give it twice the share of another server with the default weight.
Health Checks
The word check on a server line makes HAProxy test that server at a fixed interval: by opening a TCP connection, or, with option httpchk, by sending an HTTP request and counting a 2xx or 3xx answer as healthy. The http-check send line above sets the method, path, HTTP version, and Host header of that request; without it, HAProxy sends an OPTIONS request for / over HTTP/1.0. A server that fails several checks in a row is marked down and gets no traffic until it passes again. inter 5s fall 3 rise 2 on the server line sets the interval and how many failures or successes change its state. Give the application a light health URL that also tests what it depends on, such as its database.
A Protected Stats Page
The stats page shows every frontend, backend, and server with its state and traffic. Never leave it open to the internet: bind it to the server itself and require a login.
listen stats
bind 127.0.0.1:8404
mode http
stats enable
stats uri /stats
stats auth STATS_USER:STRONG_PASSWORD
Reach it from your computer through an SSH tunnel, then open http://localhost:8404/stats in your browser:
ssh -L 8404:127.0.0.1:8404 user@your_server_ip
Tunnels are explained in SSH port forwarding.
Test, Then Reload
A broken file stops HAProxy from starting, and every site behind it with it. Check the configuration before each reload:
sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg
sudo systemctl reload haproxy
With -V, the check prints "Configuration file is valid" on success; on an error it names the file and line and exits with a non-zero status. A reload lets existing connections finish while new ones use the new configuration. Read the logs with sudo journalctl -u haproxy, and open the public ports as shown in port 443.
Common Problems
- 503 Service Unavailable: every server in the backend is marked down. Request the health URL with curl from the HAProxy server; see 503 Service Unavailable.
- Cannot bind socket: another program, often Nginx or Apache, already listens on that port.
- Backends unreachable on AlmaLinux and Rocky Linux: SELinux lets HAProxy connect only to certain ports. The haproxy_connect_any boolean, off by default, lets it connect to any TCP port; turn it on with
sudo setsebool -P haproxy_connect_any 1, not by turning SELinux off; see what is SELinux.