Wednesday, March 9, 2011

Simple http load balancing with Apache.

 Since version 2.2 the Apache web server ships a load balancer module called mod_proxy_balancer. All you need to do is to enable this module and the modules mod_proxy and mod_proxy_http:
 
vi /etc/httpd/conf/httpd.conf
LoadModule proxy_module mod_proxy.so
LoadModule proxy_http_module mod_proxy_http.so
LoadModule proxy_balancer_module mod_proxy_balancer.so

:wq!

Please don't forget to load mod_proxy_http, because you wouldn't get any error messages if it's not loaded. The balancer just won't work.

Because mod_proxy makes Apache become an (open) proxy server, and open proxy servers are dangerous both to your network and to the Internet at large, I completely disable this feature: 

vi /etc/httpd/conf/httpd.conf
 
ProxyRequests Off

Order deny,allow
Deny from all

:wq!


The load balancer doesn't need this feature at all.

now create a index.html file on all servers



And here's the actual load balancer configuration:
############################

vi /etc/httpd/conf/httpd.conf

{Proxy balancer://clusterABCD}
BalancerMember http://serverA
BalancerMember http://serverB
BalancerMember http://serverC
BalancerMember http://serverD
Order allow,deny
Allow from all
{/Proxy}
ProxyPass / balancer://clusterABCD/

:wq!
/etc/init.d/httpd restart

The {Proxy}...{/Proxy} container defines which backend servers belong to my balancer. I chose the name clusterABCD for this server group, but you are free to choose any name you want.
And the ProxyPass directive instructs the Apache to forward all incoming requests to this group of backend servers.

No comments: