How to load balance with apache
deployment overview
web server site1.yourdomain.com 192.168.1.35
web server siste2.yourdomain.com 192.168.1.36
data server db1.yourdomain.com 192.168.1.37
load balancer balance-1.yourdomain.com 192.168.1.45
install and enable apache and proxy_balancer
1.create a dedicated server for load balancing. install apache2 and then install mod proxy_balancer and proxy_http with dependencies.
2.enable mod_proxy in httpd.conf. note that i’m leaving ProxyRequests off since we’re only using the ProxyPass and ProxyPassReverse directives. this keeps the server secure from spammers trying to use your proxy to send email.
<IfModule mod_proxy.c>
ProxyRequests Off
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
#Allow from .example.com
</Proxy>
ProxyVia On
</IfModule>
configure mod_proxy and mod_proxy_balancer
mod_proxy and mod_proxy balancer serve as a very functional load balancer. however mod_proxy_balancer makes slightly unfortunate assumptions about the format of the cookie that you’ll use for sticky session handling. one way to work around this is to create your own session cookie (very easy with apache). the examples below describe how to do this
first create a virtual host or use the default and add this configuration to it:
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from 192.168
</Location>
<Proxy balancer://mycluster>
# cluster member 1
BalancerMember http://site1.yourdomain.com:80 route=lb1
# cluster member 2
BalancerMember http://site2.yourdomain.com:80 route=lb2
</Proxy>
ProxyPass /balancer-manager !
ProxyPass / balancer://mycluster/ lbmethod=byrequests stickysession=BALANCEID
ProxyPassReverse / http://site1.yourdomain.com/
ProxyPassReverse / http://site2.yourdomain.com/
Note:
this is a simplistic load balancing configuration. apache has many options to control timeouts, server loading, failover etc. too much to cover but read more in the apache documentation
configure the web servers to write a session cookie
on each of the web servers, add this code to your vhost configuration: RewriteEngine On making sure to specify the correct route e.g. you also probably want to setup your cookie domain properly in drupal, i.e. modify useful urls for testing are:
RewriteRule .* - [CO=BALANCEID:balancer.lb1:.yourdomain.com]lb1 on site1.yourdomain.com etc. drupal/sites/default/settings.php as follows: # $cookie_domain = 'example.com';
$cookie_domain = 'yourdomain.com';important urls
References:
About Load Banlance topic you can read my old post: