#
# This configuration can be used as an example of how URL-switching may be
# implemented with current haproxy versions.
#
# Right now (version 1.2), haproxy can only select a server based on the cookie
# provided by the client. While this may sound limitated, it is yet possible to
# combine this feature to rewrites to provide full URL-switching capabilities.
#
# For this, we have to chain 3 levels :
#  - front-end : will match the expected URIs and assign a cookie accordingly ;
#                it uses regexps and could match on anything else (Host:,
#                cookies, ...)
#  - switch    : will select a back-end depending on the cookie above
#  - back-ends : will perform the load balancing between multiple servers for
#                the same group. Note that this level can be omitted if there
#                is only one server for each backend.
#
# Logging is performed at the lower level (back-ends) so that local server
# problems can be identified quickly with the timers. The client's IP is
# propagated in the X-Forwarded-For: header.
#

global
	daemon
	maxconn 6000      # warning: this has to be 3 times the expected value!
	log 192.168.0.1 local0

defaults
	mode	http
	balance roundrobin
	option	dontlognull
	option	httpclose
	retries	1
	redispatch
	maxconn		2000
	contimeout	5000
	clitimeout	50000
	srvtimeout	50000

#
# This is the instance the client connects to.
#
listen	frontend 10.20.30.40:80
	option forwardfor		# add 'X-Forwarded-For: IP'

	# remove an eventual 'backend' cookie the client might have sent
	reqidel ^Cookie:\ backend=

	# add cookie 'backend=2' for any HTTP method followed by
	#  '/img' only or '/img/' followed by anything.
	reqirep ^[^:\ ]*\ /img[/\ ].* \0\nCookie:\ backend=2

	# add cookie 'backend=3' for any HTTP method followed by
	#  '/home' only or '/home/' followed by anything.
	reqirep ^[^:\ ]*\ /home[/\ ].*   \0\nCookie:\ backend=3

	# send everything to next stage
	server	switch 127.0.0.2:8000


#
# This instance is only seen by the 'frontend' instance above. It receives all
# of its traffic.
#
listen switch 127.0.0.2:8000
	# cookie name 'backend' inserted by the 'frontend' instance above
	cookie backend

	# default server 'backend1' gets the default traffic.
	server backend1 127.0.0.3:8001

	# those servers get traffic only if their cookie is present because
	# they are tagged 'backup'.
	server backend2 127.0.0.3:8002 cookie 2 backup
	server backend3 127.0.0.3:8003 cookie 3 backup

#
# Backend 1 for dynamic contents.
# It is made of 4 apache servers which we can test thanks to a CGI script.
#
listen	backend1 127.0.0.3:8001
	log	global
	option	httplog
	capture request header X-Forwarded-For len 15
	option	httpchk /cgi-bin/testhost.pl
	server	apache1 192.168.1.1:80 maxconn 100 check inter 2000 fall 3
	server	apache2 192.168.1.2:80 maxconn 100 check inter 2000 fall 3
	server	apache3 192.168.1.3:80 maxconn 100 check inter 2000 fall 3
	server	apache4 192.168.1.4:80 maxconn 100 check inter 2000 fall 3

#
# backend 2 for images (/img).
# It is made of 3 Tux servers which we test by requesting the /img/logo.png
# file which should be present when file-systems are mounted.
#
listen	backend2 127.0.0.3:8002
	log	global
	option	httplog
	capture request header X-Forwarded-For len 15
	option	httpchk /img/logo.png
	server	tux5 192.168.1.5:80 check inter 2000 fall 3
	server	tux6 192.168.1.6:80 check inter 2000 fall 3
	server	tux7 192.168.1.7:80 check inter 2000 fall 3

#
# backend 3 for home directories (/home). These are the same machines as for
# dynamic content, except that a different server is bound to another port.
# We test the service by checking that the file "/home/webmaster/started"
# exists.
#
listen	backend3 127.0.0.3:8003
	log	global
	option	httplog
	capture request header X-Forwarded-For len 15
	option	httpchk /home/webmaster/started
	server	light1 192.168.1.1:8080 check inter 2000 fall 3
	server	light2 192.168.1.2:8080 check inter 2000 fall 3
	server	light3 192.168.1.3:8080 check inter 2000 fall 3
	server	light4 192.168.1.4:8080 check inter 2000 fall 3

