:::: MENU ::::

Apache HTTP Server: Load Balancer

Apache HTTP Server has a module that allows us to use load balance. Let’s see how it’s used.

Introduction

Apache HTTP Server has several modules with which you can do many things, such as overwrite the URL, use SSL security, or balance the load between multiple machines. Load balancing consists in using a single URL and, internally, redirecting the traffic to different machines (each one contain the same web page).

Note: In this example we will use two different Web pages only in order to demonstrate the balance.

Previous requirements

It’s necessary:

  • An Apache HTTP Server installed and running.
  • Several locations with the same Web page.

About the locations: They could be independent machines, each with its own URL. In our case, we will have two Web pages located in the folders:

/var/www/html/webpage-1/index.html

and:

/var/www/html/webpage-2/index.html


It is important that in these files the content is different, because otherwise we will not be able to appreciate the balance.

Example of files:

webpage-1/index.html

<html>
  <head>
    <title>First Web page</title>
  </head>

  <body>
    <h1>This is the first Web page.</h2>
  </body>
</html>

webpage-2/index.html

<html>
  <head>
     <title>Second Web page</title>
  </head>
  <body>
    <h1>This is the second Web page.</h2>
  </body>
</html>

Installation

The load balance module is called “proxy_balancer” and to install it we have to enter these commands:

a2enmod proxy
a2enmod proxy_http
a2enmod lbmethod_bytraffic
a2enmod proxy_balancer

As we can see, it’s necessary to install other modules previously.

Setup

As a general rule, almost everyone configures Apache in the following file:

/etc/apache2/apache2.conf

Personally, I prefer to configure things separately because that way there are fewer distractions. To configure the modules, specific files are used, which are located in the folder:

/etc/apache2/mods-enabled

We edit the file

/etc/apache2/mods-enabled/proxy_balancer.conf

and we leave it like this:

<IfModule mod_proxy_balancer.c>

    <Proxy "balancer://myset">
            BalancerMember "http://localhost/webpage-1/index.html"
            BalancerMember "http://localhost/webpage-2/index.html"
            Order deny,allow
            Allow from all
            ProxySet lbmethod=byrequests
    </Proxy>

    ProxyPreserveHost On

    <Location "/">
            ProxyPass "balancer://myset/"
            ProxyPassReverse "balancer://myset/"
    </Location>

</IfModule>

vim: syntax=apache ts=4 sw=4 sts=4 sr noet

This way:

  1. BalanceMember indicates the locations on which to balance.
  2. ProxySet lbmethod indicates, in this case, that each request has to balance to a different location.
  3. Location “/” indicates that when we load the page http://localhost/ the balance has to be executed.

Test

We restart the server in order to let the server read the new configuration with:

service apache2 restart

We load the initial page of our Apache HTTP Server:

http://localhost/

And we will see that each time we reload the page the content of the page changes.


So, what do you think ?