haproxy load balancer configuration

HAProxy Load Balancer Configuration in Linux

HAProxy is a free, open source high availability proxy server and load balancer that is used with popular web servers such as Apache, NGINX, etc. It allows you to easily distribute workload among multiple web servers, database servers, etc. as per your requirement. HAProxy is available for almost all Linux distributions. In this article, we will look at how to install & configure HAProxy as a high availability load balancer that distributes requests across multiple web servers.

HAProxy Load Balancer Configuration in Linux

Here are the steps to install HAProxy load balancer configuration in Linux. For our example, we will setup load balancer at IP address and domain www.example.com. We will use HAProxy to distribute load among 2 web servers with IP addresses, each running Apache server.

1. Install Apache Server

First, we need to install Apache web server on all the 2 web servers. Open terminals on the four web servers at, and run the following commands.

# yum install httpd		[On RedHat based Systems]
# apt-get install apache2	[On Debian based Systems]

After you install Apache on these 2 web servers, just open your web browser and visit their ip addresses as http://ip_address. You should be able to see the default Apache screen.

2. Install HAProxy

Open terminal on the server with IP address where we want to install HAProxy and run the following commands to install HAProxy.

# yum install haproxy openssl-devel	[On RedHat based Systems]
# apt-get install haproxy		[On Debian based Systems]

3. Configure HAProxy Logs

Next we need to enable logging in HAProxy. Run the following command to open its configuration file in a text editor.

# vi /etc/haproxy/haproxy.cfg

Next, based on your distribution enable the following lines in this document by removing # at its beginning.


Under #Global settings

log local2


Under #Global settings, replace the following lines

log /dev/log        local0
log /dev/log        local1 notice 


log local2

4. Configure Syslog

Next, we configure syslog to create separate logs for HAProxy. For this, open rsyslog.conf file in text editor.

# vi /etc/rsyslog.conf

Uncomment the following lines by removing # at their beginning. Our server listens to port 514 for logging.

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

We create a separate HAProxy.conf file for separate logging.

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

Add the following line to this file.

local2.*	/var/log/haproxy.log

Restart Rsyslog service.

# service rsyslog restart 

5. Configure HAProxy Global Settings

Next we need to configure HAProxy to communicate with out 2 web servers running Apache server. Open its configuration file in text editor.

# vi /etc/haproxy/haproxy.cfg

Update the configuration file as shown below. You just need to update the parts in bold below.

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		# Username:Password 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 web1-srv cookie web1-srv check		# backend server.
   server web2-srv cookie web2-srv check		# backend server.

The first line above specifies the label LB for this front end section. The next line mentions the ip address and port number HAProxy is listening to. The third line specified the protocol to be followed. In the above example, we are binding HAProxy to port 80. The next line refers to the label LB of back end section where the request needs to be forwarded.

Similarly, add the corresponding back end section (line starting with backend) to serve requests received from this front end section. Just like front-end section, you can have multiple back end sections, for different websites, each labelled differently.

Let us say we want HAProxy to forward incoming requests to 2 web servers running on port 80 at ip_address1 ( and ip_address2 ( In such case, add the following backend section.

Replace ip_address1 and ip_address2 with ip addresses of your 2 servers. Also specify server1 & server 2 names to differentiate both of them.

backend backendnodes
balance roundrobin
server <server1 name> <ip_address1>:80 check    
server <server2 name> <ip_address2>:80 check

In our case, we have used the following values.

server web1-srv cookie web1-srv check
server web2-srv cookie web2-srv check

Save and close the file. Restart HAProxy to apply changes.

# service haproxy restart
# chkconfig haproxy on
# chkconfig --list haproxy

Please note, you will be able to access HAProxy stats at URL by specifying the username haproxy and password redhat that you have mentioned in config file. In some Ubuntu/Debian systems you may need to add the following line to /etc/default/haproxy.


6. Verify HAProxy installation

Add the following HTML file index.html in both the web servers running Apache server.

  <title>HAProxy Test Page</title>

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

<p>Welcome to HA Proxy test page!


Open browser and visit and you should be able to view the above html page indicating that HAProxy has fetched the page from one of the web servers.

You can view HAProxy stats by opening browser and visiting URL. You will be prompted for username & password. Enter them as haproxy & redhat respectively, as specified in your HAProxy configuration file.

That’s it. In this article, we have learnt how to install and configure HAProxy load balancer and proxy.

Also read:

How to Clone Partition or Hard Disk in Linux
How to Assign Multiple IP Address to Network Interface
How to Find Public IP Address of Server in Linux
How to Find Folder by Name in Linux
How to Check Open Ports in UFW

Leave a Reply

Your email address will not be published. Required fields are marked *