How to Setup HAproxy Load Balance Server for Sharing Web Traffic

This article is written to prepare high availability of a website. This can also be used by those who want their website to be available to their clients or users with high availability servers and to share between their servers. It is a free and open source application used as a TCP/HTTP load balancer which will distribute web traffic to multiple servers and improves performance and reliability of the web server.


Assume that load balancer HA proxy server IP address is

HAproxy Server details

OS:  Centos 6.7
IP Address:


OS:  Centos 6.7
IP Address:


OS:  Centos 6.7
IP Address:

We need to install the Apache on both the web servers,  i.e., and

# yum install http*

After installation, please access the Apache from web browser http://your-server-ip-address

Installing HAProxy Server

# yum install haproxy openssl-devel

Open the host files and add the below lines in all 3 servers (HAproxy Load balancer, webserver1, webserver2)     haproxy    haproxy    haproxy

# vi /etc/hosts   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 haserver ha2 ha1

Now, you need to enable the HAproxy Logs to identify the problems for your future debugging

# vi /etc/haproxy/haproxy.cfg

Add these below lines to enable the logging

log  localNeeded to enable the UDp SYSLOG reception in /etc/rsyslog.conf
# vi /etc/rsyslog.conf
#### MODULES ####

$ModLoad imuxsock # provides support for local system logging (e.g. Via logger command)
$ModLoad imklog   # provides kernel logging support (previously done by rklogd)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

# Provides TCP SYSLOG reception
#$ModLoad imtcp
#$InputTCPServerRun 514

Now create a haproxy.conf in /etc/rsyslog.d/ folder to create log files

# vi /etc/rsyslog.d/haproxy.conf

Add the below line to create the file

local.*  /var/log/haproxy.log

Restart the rsyslog services to update the changes we made

# service rsyslog restart

Configuring HAproxy global settings

we needed to setup the glogal setting in /etc/haproxy/haproxy.cfg
# vi /etc/haproxy/haproxy.cfg
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

We have to provide the details of both the web servers as front-end and back-end servers in case of failure.

frontend LB
   reqadd X-Forwarded-Proto: http
   default_backend LB

backend LB
   mode http
   stats enable
   stats hide-version
   stats uri /stats
   stats realm Haproxy Statistics
   stats auth haproxy:redhat            # Credentials for HAProxy Statistic report page.
   balance roundrobin                   # Load balancing will work in round-robin process.
   option httpchk
   option  httpclose
   option forwardfor
   cookie LB insert
   server ha2 cookie ha1 check                # backend server.
   server ha1 cookie ha2 check                # backend server.

We need to restart the services and make the changes effective.

# service haproxy restart

To start the service automatically, use the below command

# chkconfig haproxy on

To check the autostart –

# chkconfig --list haproxy

After the services, re-start so that, we can access the load balancer on

For testing the HAproxy load balancer, we will have to create an index.html file in both web servers with below code.

Create an index.html file in the webserver1 with the bellow code.

 <title>HAProxy Test Page</title>
 <!-- Main content -->
 <h1>My HAProxy Test Page Server 1 </h1>

Create an index.html file in webserver2 with the bellow code.

 <title>HAProxy Test Page</title>
 <!-- Main content -->
 <h1>My HAProxy Test Page Server 2 </h1>

Now access the haproxy server IP

By default, you can see the page of webserver 1



Now, test the load balance by going to webserver 1 and stop the HTTPd server

# service httpd stop

Then, access the haproxy server IP,

Now, you will see the web page of webserver2 automatically



To verify, you can access the HAproxy load balancer statistics  page




After the configuration, you should be able to get your website without interruption even if one of the servers is down or unavailable. In the same way, we can also set up a HAproxy for Apache. You can also observe that, Webserver1 is not accessible or Down in the HAproxy load balancer statistics page by configuring the above as shown. You can also add any number of servers depending upon the requirement.

Other Interesting Posts