Web Server Load-Balancing with HAProxy on Ubuntu 14.04

What is HAProxy?

HAProxy(High Availability Proxy) is an open-source load-balancer which can load balance any TCP service. HAProxy is a free, very fast and reliable solution that offers load-balancing, high-availability, and proxying for TCP and HTTP-based applications. It is particularly well suited for very high traffic web sites and powers many of the world's most visited ones.

Since it's existence, it has become the de-facto standard open-source load-balancer. Although it does not advertise itself, but is used widely. Below is a basic diagram of how the setup looks like:

Installing HAProxy

I am using Ubuntu 14.04 and install it by:

apt-get install haproxy

You can check the version by:

haproxy -v

We need to enable HAProxy to be started by the init script /etc/default/haproxy. Set ENABLED option to 1 as:


To verify if this change is done properly, execute the init script of HAProxy without any parameters. You should see the following:

$ service haproxy <press_tab_key>
reload   restart  start    status   stop

HAProxy is now installed. Let us now create a setup in which we have 2(two) Apache Web Server instances and 1(one) HAProxy instance. Below is the setup information:

We will be using three systems, spawned virtually through VirtualBox:

Instance 1 - Load Balancer

Hostname: haproxy
OS: Ubuntu
Private IP:

Instance 2 - Web Server 1

Hostname: webser01
OS: Ubuntu with LAMP
Private IP:

Instance 2 - Web Server 2

Hostname: webserver02
OS: Ubuntu with LAMP
Private IP:

Here is the diagram of how the setup looks like:

Let us now configure HAProxy.

Configuring HAProxy

Backup the original file by renaming it:

mv /etc/haproxy/haproxy.cfg{,.original}

We'll create our own haproxy.cfg file. Using your favorite text editor create the /etc/haproxy/haproxy.cfg file as:

        log /dev/log   local0
        log   local1 notice
        maxconn 4096
        user haproxy
        group haproxy

        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        maxconn 2000
        contimeout     5000
        clitimeout     50000
        srvtimeout     50000

listen webfarm
    mode http
    stats enable
    stats uri /haproxy?stats
    balance roundrobin
    option httpclose
    option forwardfor
    server webserver01 check
    server webserver02 check


        log /dev/log   local0
        log   local1 notice
        maxconn 4096
        user haproxy
        group haproxy

The log directive mentions a syslog server to which log messages will be sent.
The maxconn directive specifies the number of concurrent connections on the front-end. The default value is 2000 and should be tuned according to your system's configuration.
The user and group directives changes the HAProxy process to the specified user/group. These shouldn't be changed.

        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        maxconn 2000
        contimeout     5000
        clitimeout     50000
        srvtimeout     50000

The above section has the default values. The option redispatch enables session redistribution in case of connection failures. So session stickness is overriden if a web server instance goes down.
The retries directive sets the number of retries to perform on a web server instance after a connection failure.
The values to be modified are the various timeout directives. The contimeout option specifies the maximum time to wait for a connection attempt to a web server instance to succeed.
The clitimeout and srvtimeout apply when the client or server is expected to acknowledge or send data during the TCP process. HAProxy recommends setting the client and server timeouts to the same value.

listen webfarm mode http stats enable stats uri /haproxy?stats balance roundrobin option httpclose option forwardfor server webserver01 check server webserver02 check

Above block contains configuration for both the frontend and backend. We are configuring HAProxy to listen on port 80 for webfarm which is just a name for identifying an application.
The stats directives enable the connection statistics page. This page can viewed with the URL mentioned in stats uri so in this case, it is a demo of this page can be viewed here.
The balance directive specifies the load balancing algorithm to use. Algorithm options available are:

  • Round Robin (roundrobin),
  • Static Round Robin (static-rr),
  • Least Connections (leastconn),
  • Source (source),
  • URI (uri) and
  • URL parameter (url_param).

Information about each algorithm can be obtained from the official documentation.

The server directive declares a backend server, the syntax is:

server <server_name> <server_address>[:port] [param*]

The name we mention here will appear in logs and alerts. There are some more parameters supported by this directive and we'll be using the check parameter in this article. The check option enables health checks on the web server instance otherwise, the web server instance is ?always considered available.

Once you're done configuring start the HAProxy service:

sudo service haproxy start

Testing Load-Balancing and Fail-over

We will append the server name in both the default index.html file located by default at /var/www/index.html

On the Instance 2 - Web Server 1 (webserver01 with IP-, append below line as:

sudo sh -c "echo \<h1\>Hostname: webserver01 \(\)\<\/h1\> >> /var/www/index.html"

On the Instance 3 - Web Server 2 (webserver02 with IP-, append below line as:

sudo sh -c "echo \<h1\>Hostname: webserver02 \(\)\<\/h1\> >> /var/www/index.html"

Now open up the web browser on local machine and browse through the haproxy IP i.e.

Each time you refresh the tab, you'll see the load is being distributed to each web server. Below is screenshot of my browser:

For the first time when I visit , I get:

And for the second time, i.e. when I refresh the page, I get:

You can also check the haproxy stats by visiting

There's more that you can do to this setup. Some ideas include:

  • take one or both web servers offline to test what happens when you access HAProxy
  • configure HAProxy to serve a custom maintenance page
  • configure the web interface so you can visually monitor HAProxy statistics
  • change the scheduler to something other than round-robin
  • configure prioritization/weights for particular servers

That's all!

Share this page:

16 Comment(s)

Add comment


From: Chono

Great tutorial, I was thinking about it for some time.. Is that HAproxy has to be installed on extra VirtualBox instance or can it run on one of the Web Server?


From: Ildefonso

Can this be used as a load balancer for ADSL modems?

I have three ADSL modems with a very bad service from my ISP (mainly due to the distance from the telco's central to my office) and over twenty PCs. I wish I could use something similar in order to send my PCs outbond internet trafic to  the ADSLs, in a way that does not saturate the bandwidth of the single modem we are currently using. I have a Linux bot that works as a router and gateway, connected to a single modem.

Any advice would be much appreciated.

From: Sanchit Jain Rasiya

I am not sure of if it can be used for ADSL load balancing. By the way, how many broadband lines do you have? If you 3 lines, you can configure your Linux Box as a load balancer. Check out- http://unix.stackexchange.com/questions/138956/implementing-load-balancing-on-any-linux-distro

You can also have a look at this device- http://www.amazon.com/TP-LINK-TL-R470T-Broadband-Changeable-Ethernet/dp/B005SYQBN8/ref=sr_1_22?s=pc&ie=UTF8&qid=1427375224&sr=1-22&keywords=load+balancer

Hope it helps!

From: Sanchit Jain Rasiya

Thanks Chono. Not necessarily on VM. But it would be strongly suggested to have it separate on production so that it is independent of any host. If the host with both services is down, it will be a problematic situation.

From: Ildefonso

Thanks Sanchit

I have not three, but four broadband lines, so I will be trying your suggestion as of next weekend.


From: hipr

I am now wondering, how to set up an htts and SSL cetrificate, i.e. where goes what and how to configure it...

From: Zeru

I'm using similar configuration as in the above but I can't get a reply for high connection rates. For instance, for --rate 500 and num_conn 10, that is, when the number of reuest greater than 5000, the haproxy has no min, avg, and max reply rate.


From: LandonC

Thanks -- works like a charm for Solr load-balancing with SolrCloud and ZooKeeper.

From: Tom Dings

Used this basic explanation to balance 64 web servers, and 128 mysql database machines. Works out very well.

From: Sanchit Jain Rasiya

Wow, great to know. Thanks!

From: Anthony

I've managed to start up HAProxy successfully. But when I try to access my VIP, I got this error "503, Service Unavailable"

i am am sure that both my web server is up. I am able to access directly using the web server IP.  The default web location is /var/www/html/index.html, where should I change the directory to point to the correct one?

Appreciate your advice.


thank you,

From: shreedhar

nice tutorial. Very crisp and clear !!!

From: Sanchit Jain Rasiya


From: kishore

I have a three web servers in which haproxy is running to balance the load.I'm using Ubuntu, so one of my web servers showing major difference in bandwidth usage and rest two are showing almost equally bandwidth usage. I have used the Round-robin algo in haproxy config.So how to equally divide the bandwidth usage. Please suggest asap

From: VuPhan

great tut!

From: Santoso

Great Tutorial! But now I'm wondering if the load balancer is down, so what happens to the web server? Thanks alot.