Networking HowTos
Networking HowTos

Load Balancing With HAProxy

February 2, 2012 Linux

HAProxy is a free, reliable, high performance load balancing solution capable of proxying TCP and HTTP applications.
This article will outline how to set up a simple HAProxy server to allow you to load balance web site requests to one or more back-end web servers. While this example may not be suitable for a production set up on the internet, its a good start if you just want to set up a simple load balanced web site.
The following example is using Ubuntu, however the configuration will be the same regardless of the distribution. The steps to install HAProxy will vary depending on the distribution. It also assumes that the back end web servers are on different physical machines to the one running HAProxy.
Installing HAProxy:

$ sudo apt-get install haproxy

On Ubuntu, by default the HAProxy service is disabled. To enable it you need to modify the /etc/default/haproxy file.

$ sudo nano /etc/default/haproxy

Modify the ‘ENABLED=0’ line to ‘ENABLED=1’ as shown below.
Change this:

# Set ENABLED to 1 if you want the init script to start haproxy.
# Add extra flags here.
#EXTRAOPTS="-de -m 16"

To this:

# Set ENABLED to 1 if you want the init script to start haproxy.
# Add extra flags here.
#EXTRAOPTS="-de -m 16"

Save and exit the editor.
The next step is to modify the HAProxy configuration file.

$ sudo nano /etc/haproxy/haproxy.cfg

In the default configuration file, there will likely be a number of ‘listen’ sections. These can be removed or commented out, as they wont be needed. You just want to keep the ‘global’ and ‘defaults’ sections for now.
Add in the following ‘frontend’ and ‘backend’ sections:

frontend http_in
        bind *:80
        default_backend webservers
backend webservers
        balance roundrobin
        server webserver1
        server webserver2

What this does is sets up a proxy frontend called ‘http_in’, listening on port 80, on all IP addresses on the machine. This proxy frontend will pass all traffic through to the ‘backend’ which has been configured with the name ‘webservers’. This is set with the ‘default_backend’ setting pointing to ‘webservers’.
THe ‘backend’ section is where we configure what servers will be avaliable to load balance between, and what type of load balancing model to use. This example uses the round robin model, which basically just rotates evenly between the servers listed. In this example, we have named the server running on as ‘webserver1’ and the server running on as ‘webserver2’. These names dont have to resolve to anything as they are just used for identifiers within HAProxy.
The configuration file should now look similar to this:

        log   local0
        log   local1 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        user haproxy
        group haproxy
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
frontend http_in
        bind *:80
        default_backend webservers
backend webservers
        balance roundrobin
        server webserver1
        server webserver2

Save the configuration, and exit the editor.
Restart/Start HAProxy for the changes to take effect.

$ sudo /etc/init.d/haproxy restart

You should now be able to go to the url of the HAProxy machine, and have the requests redirected to the back end web servers that were set up in the ‘backend’ section.
There are a large number of configuration options avaliable to do things like weighted balancing, server status checking, URL redirection, stats pages, and more. Read the documentation on the HAProxy website ( for more information on the various configuration options available.

You Might Also Like

  • Sen February 3, 2012 at 6:26 am

    Great Article to know about HAProxy. I’m currently working on an assignment wherein we need to connect to multiple SQL Servers(SQL Server 2005 Standard Edition) from web servers(.NET applications) in a replication environment. We are not in a position to use SQL Server clustering solution now. So, Can we use HAProxy to load balance the requests from web servers to SQL Server?.

  • admin February 3, 2012 at 9:21 am

    Hi Sen,
    You should be able to at a network level, but I haven’t tested it, so I’m not sure about the implications regarding replication delays, etc.
    Try and replace the listen and backend sections with the following to allow TCP load balancing. Be sure to enable the TCP/IP protocol on Microsoft SQL Server and replace 1433 with the listening port number. You may have to connect directly to the IP address of the HAProxy machine, using a specific port number in your .Net application.

    frontend sql_in
            bind *:1433
            mode tcp
            default_backend sqlservers
    backend sqlservers
            balance roundrobin
            server sqlserver1 check
            server sqlserver2 check
  • Sen February 3, 2012 at 11:27 am

    Thanks for the input, will try and see.