Operating load-balancing with HAproxy

HAproxy is a software allowing Load-Balancing between several web servers by allocating requests in an almost transparent way for the user.


There are three necessary machines at least : one HAproxy server and two web servers in which incoming requests will be allocated.
In both web servers, there is a need to make apache2 operational at least, each having access to an identical content (in order to avoid displaying two versions of the web site.)

The server HAproxy acts as a bridge between the web servers network and the external network. It has to dispose of 2 network interfaces : one to cope with incoming requests and the other to communicate with the various servers of the cluster.

In our example, the server HAproxy would have as “external” IP (frontal web) and as “internal” IP (web servers network)

Web servers’ IP are:

Implementation of Load-Balancing

Here are the steps to follow in order to install and launch the server HAproxy to implement Load-Balancing.

We change the name of the machine HAproxy:

You might also want to think about changing its name in /etc/hosts:

Install the service HAproxy :

For HAproxy to be entirely considered as a service :

Now we are going to set up a file of basic confguration to test in our two web servers.

Another Load-Balancing mode can be used :
– mode “leastconn” : the server having had the least connections will first answer requests.

Client-server lasting connection

Some customs need a persistent connection between client and server (sessions management, shopping cart on e-commerce).
To cope with this issue, we can send a cookie to the user.
According to the cookie content, the Load-Balancing set up can transfer the requests in the matching server and the client could maintain lasting exchanges with this one.

All you need is to add these lines :

To check on the cookies presence and their actions on a Firefox browser : Option > Private life > Cookies display

Logs et stats

It is possible to view the servers’ Load-Balancing thanks to HAproxy web interface.

The HAproxy interface of general information includes many informations about different web servers functioning and about the Load-Balancing state.

To consult these information, you just need to type the following URL :


Operating load-balancing with HAproxy