rrichards
5/25/2012 - 2:10 PM

HAProxy Config with ACL for Load Balancing Multiple Named Virtual Hosts

HAProxy Config with ACL for Load Balancing Multiple Named Virtual Hosts

# Config ideal for load balancing many back-end Virtual Hosts with a pair of load balancers (one is for fail-over).

# HAProxy's ACL is ideal for dedicating more resources to certain virtual hosts especially to handle traffic spikes.

# Reference: 
# http://www.techrawr.com/2009/09/18/using-the-acl-in-haproxy-for-load-balancing-named-virtual-hosts

# Don't copy blindly, change to suit your needs; at a minimum:-
# change maxconn based on your back-end server(s) capabilities
# enable/disable cookie option - disable if you don't want sticky sessions
# change some_username & some_password to something secure

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        ulimit-n 65536
        maxconn 25000
        #debug
        #quiet
        user haproxy
        group haproxy

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option  abortonclose
        retries 3
        option  redispatch
        maxconn 25000
        contimeout      300000
        clitimeout      300000
        srvtimeout      300000
        stats enable
        stats uri     /myhaproxy?stats
        stats auth some_username:some_password

frontend all_http 192.168.1.1:80
        acl example_req hdr_dom(host) -i example.com

        use_backend special_webfarm if example_req
        default_backend default_webfarm


        backend special_webfarm
               mode http
               balance roundrobin
               #cookie SERVERID insert nocache indirect
               option httpclose
               option forwardfor
               option httpchk HEAD /robots.txt HTTP/1.0
               server web1  10.1.1.1:80 weight 1 check
               server web2  10.1.1.2:80 weight 1 check


        backend default_webfarm
               mode http
               balance roundrobin
               #cookie SERVERID insert nocache indirect
               option httpclose
               option forwardfor
               option httpchk HEAD /robots.txt HTTP/1.0
               server web1  10.1.1.1:80 weight 1 check
               server web2  10.1.1.2:80 weight 1 check
               server web3  10.1.1.3:80 weight 1 check
               server web4  10.1.1.4:80 weight 1 check