How to set up a Load Balancer with Apache in 5 Minutes

True, there are professional tools for Load Balancing like the HAP Proxy which are used by big enterprises in the web. However, for small-scale until medium scale users such a high end tool may be a bit of an overhead. Instead one could think of using the build-in load balancing features of the apache webserver. They are really easy to set up and full-fill basic requrirements. A simple setup looks like follows

[sourcecode language=”text”]

<Proxy balancer://mycluster>
# WebHead1
BalancerMember http://node1:80/ route=1 loadfactor=4
# WebHead2
BalancerMember http://node2:80 route=2 loadfactor=6

# Security "technically we aren’t blocking
# anyone but this the place to make those
# chages
Order Deny,Allow
Deny from none
Allow from all

# Load Balancer Settings
# We will be configuring a simple Round
# Robin style load balancer. This means
# that all webheads take an equal share of
# of the load.
ProxyPreserveHost On
ProxySet lbmethod=bybusyness
#ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID

In this case the load balancer is thought to produce use sticky sessions which is achieved by a cookie. If you want to achive really persistent session you might think just setting the expiration date of your cookie far in the future. An example you can find here. The load-factor simply defines a weight for the particular node.

[sourcecode language=”text”]
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; Expires=Tue, 15-Jan-2019 21:47:38 GMT; path=/" env=BALANCER_ROUTE_CHANGED

There are also other ways to implement stickyness for the Apache Load Balancer. You can find a good overview about them in the apache documentation:

That is everything you need to know to set up a load balancer using mod balancer and Apache. Hope that helps. Life can sometimes really be easy…

Leave a Reply

Your email address will not be published. Required fields are marked *