Skip to content

Instantly share code, notes, and snippets.

@daemonza
Created March 6, 2012 07:57
Show Gist options
  • Save daemonza/1984806 to your computer and use it in GitHub Desktop.
Save daemonza/1984806 to your computer and use it in GitHub Desktop.
HAproxy virtual site load balancing
# This configuration use acl's to distinguish between url's passwd and then route
# them to the right backend servers. For the backend servers to handle it correctly, you
# need to setup virtual hosting there as well, on whatever you use, tomcat, nginx, apache, etc.
# For this to work with SSL, put pound before HAproxy and use a configuration file similar to
# https://gist.github.com/1984822 to get it working
global
log 127.0.0.1 local0
log 127.0.0.1 local1 info
maxconn 4096
user haproxy
group haproxy
daemon
stats socket /var/run/haproxy.sock
pidfile /var/run/haproxy.pid
defaults
log global
mode http
option httplog clf
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
# status page.
listen stats 0.0.0.0:8000
mode http
stats enable
stats uri /haproxy
stats realm HAProxy
stats auth hauser:hapasswd
frontend http-in
bind *:80
#### Setup virtual host routing
# www.example.com
acl is_www_example_com hdr_end(host) -i www.example.com
use_backend www_example_com if is_www_example_com
# mobile.example.com
acl is_mobile_example_com hdr_end(host) -i mobile.example.com
use_backend mobile_example_com if is_mobile_example_com
default_backend default_site
# www.example.com
backend www_example_com
balance roundrobin
cookie SERVERID insert nocache indirect
option httpchk HEAD /check.txt HTTP/1.0
option httpclose
option forwardfor
server node1 192.168.1.1:80 cookie node1
server node2 192.168.1.2:80 cookie node2
server node3 192.168.1.3:80 cookie node3
# mobile.example.com
backend mobile_example_com
balance roundrobin
cookie SERVERID insert nocache indirect
option httpchk HEAD / HTTP/1.0
option httpclose
option forwardfor
server node1 192.168.1.1:2812 cookie node1
server node2 192.168.1.2:80 cookie node2
server node3 192.168.1.3:80 cookie node3
backend default_site
balance roundrobin
cookie SERVERID insert nocache indirect
option httpchk HEAD /check.txt HTTP/1.0
option httpclose
option forwardfor
server node1 192.168.1.1:80 cookie node1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment