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.)

apt-get install apache2

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:

hostname SRV_haproxy

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

vim /etc/hosts

Install the service HAproxy :

apt-get update && apt-get install haproxy

For HAproxy to be entirely considered as a service :

vim /etc/default/haproxy
Put parameter ENABLED to 1
service haproxy status
service haproxy start

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

vim /etc/haproxy/haproxy.cfg

# We detail the external interface of server (where it listens)
listen SRV_haproxy

# We work on the stream http
mode http

# Choice of the Load-Balancing mode
balance roundrobin

# We close the connection after the client's response
option httpclose

# We check if the server is still active (health check)
httpchk HEAD /index.html HTTP/1.0

# Response deadlines client - server
contimeout 5000
clitimeout 50000
srvtimeout 50000

# Definition of the load-balanced servers (real servers) :
SRV_Web1 check server
SRV_Web2 check

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 :

vim /etc/haproxy/haproxy.cfg
cookie insert indirect
server SRV_Web1 cookie SRV_Web1 check
server SRV_Web2 cookie SRV_Web2 check

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.

vim /etc/Haproxy/haproxy.cfg
stats uri /stats
stats auth btsig:btsig

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 :