HAProxy

HAProxy config generator

A complete haproxy.cfg for HAProxy 2.8 LTS (also fine on 2.4–3.0, differences are noted in the comments). The defaults fit HAProxy terminating TLS in front of Apache on port 80. Settings are kept in the URL; the stats password is not.

Start from

Frontend

Backends

Servers: one per line, [name] address:port [weight N] [maxconn N] [backup], e.g. web1 10.0.0.11:80 weight 100.

Routing

By host name (Host header; SNI for TCP below)

HostBackendRemove
example.com exact · *.example.com subdomains only · .example.com both. Exact names are checked first.

By path (checked before the host rules)

Host (optional)MatchPathBackendRemove

Forwarded headers

Headers are only added when the backend did not send its own. More headers (CSP, Permissions-Policy): header builder.

Block IPs

For http-request return the file is plain HTML. For errorfile it is a whole HTTP response with status line and headers.

NamePortSNI names (optional)ServersCheckPROXYRemove
With SNI names the service is routed by req.ssl_sni without decrypting. On port 443 it shares the port with the HTTPS frontend.

Global and defaults

Times: 500ms, 5s, 1m, 1h. What each one does: timeouts cheat sheet.

haproxy.cfg


  

Check and reload

Save the file as /etc/haproxy/haproxy.cfg (keep a copy of the old one), then:

    Version notes