HAProxy Quickstart w/ full example config file
I recently installed HAProxy as a web server load balancer. While their documentation is great, I found it lacking a complete example of a working configuration file. For reference, here is the config file I ended up with along with comments.
# /etc/haproxy/haproxy.cfg, version 1.4
global
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
# logs which servers requests go to, plus current connections and a whole lot of other stuff
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
log 127.0.0.1 local0
# use rsyslog rules to forword to a centralized server
log 127.0.0.1 local7 debug
# check webservers for health, taking them out of the queue as necessary
option httpchk
# this load balancer servers both www.site.com and static.site.com, but those two URLS have
# different servers on the backend (app servers versus statis media apache instances)
# also, I want to server www.site.com/static/* from the later farm
frontend http
bind 0.0.0.0:80
# important, see comment from Willy Tarreau bellow
option http-server-close
# NAT static host names and static paths in other hostnames to static.bullhornreach.com
acl host_static hdr_beg(host) -i static
acl url_static path_beg /static
use_backend static if host_static
use_backend static if url_static
default_backend www
backend www
balance roundrobin
server www1 www1 check port 80
server www2 www2 check port 80
server www3 www3 check port 80
# provide a maintenance page functionality, only used when all other servers are down
server load1 localhost:8080 backup
backend static
# for static media, connections are cheap, plus the client is very likely to request multiple files
# so, keep the connection open (KeepAlive is the default)
balance roundrobin
server media1 media1 check port 80
server media2 media2 check port 80
listen stats :1936
mode http
stats enable
stats scope http
stats scope www
stats scope static
stats scope static_httpclose
stats realm Haproxy\ Statistics
stats uri /
stats auth haproxy:YOURPASSWORDHERE
Edit: made changes suggested by Willy Tarreau.