Load Balancing with Apache

Greetings dear reader. In this article I want to describe how to configure Apache to load balance multiple back-end servers. You’ll need the following two modules:

  • mod_proxy
  • mod_proxy_balancer

Host configuration example:

<VirtualHost *:80>
	ServerName mywebsite.com
	Proxy Requests On
	ProxyVia On
	
	<Proxy balancer://mycluster>
		BalancerMember http://192.168.1.50:80
		BalancerMember http://192.168.1.51:80
		BalancerMember http://192.168.1.52:80
	</Proxy>

	ProxyPass / balancer://mycluster
</VirtualHost>

In order to enable sticky sessions you need to bring the config to the following form:

<VirtualHost *:80>
	ServerName mywebsite.com
	Proxy Requests On
	ProxyVia On
	Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

	<Proxy balancer://mycluster>
		BalancerMember http://192.168.1.50:80 route=1
		BalancerMember http://192.168.1.51:80 route=2
		BalancerMember http://192.168.1.51:80 route=3
		ProxySet stickysession=ROUTEID
	</Proxy>

	ProxyPass / balancer://mycluster
</VirtualHost>