Cloud Computing‎ > ‎Load Balancing‎ > ‎HAProxy‎ > ‎



HAProxy is a free and open-source Linux application used for load balancing network traffic. This tutorial will guide you through deploying it for both simple web applications and large, complex web sites.

Load Balancing Concepts

Depending on the complexity of your needs, balancing can be accomplished many ways using different techniques. One of the first things you’ll need to identify is do use layer 4 or layer 7 balancing. Each is a solution for different needs, so it’s very important to understand your actually requirements before making a decision on a load balancer configuration.

Layer 4

The higher you are on the network stack layer, the faster you will be able to process requests. The caveat is that the extra performance gain comes at the cost of feature and ability loss. Ignoring features you may find in advanced enterprise-ready load balancers, you can’t beat the performance of a layer 4 balancing. It works by modifying the destination IP and MAC address part of every packet’s header to forward it to one of the available nodes. This approach is accomplished by using very little processing.

Layer 4 web server load balancing

The biggest downside to this method of balancing is your nodes must host every component of your application (PHP, Java, CSS, Javscript, IMGs, etc), and the application files on each node must be the exact same versions. Otherwise, you’re users’ experience will differ each time they access your website.

Layer 4 balancing is ideal for small websites or those with few complexities.

Layer 7

Layer 7 balancing may require more horsepower to process, but what it offers in return for large and complex websites is worth the extra CPU time. This type of balancing allows you to separate every component onto it’s own server, and it does so transparently to the user. Another benefit is being able to move heavily accessed areas of your website onto separate servers. For example, maybe you’re hosting a news website that also has forums to allow users to discuss things. Given enough popularity, the forum may cause other areas of your website to be sluggish or become inaccessible. Using layer 7, you could separate your forum onto its own server or server group to allow you to scale it out.

Layer 7 load balancing

Layer 7 works by analyzing the application request part of every packet, and then matching it against a set of policies or rules. For a web server, it looks at the HTTP request to identify the action and destination URI.  Using our news website with a forum example above, if an HTTP GET or POST request is submitted starting with /forum, the traffic will be routed to the servers hosting the forum application, allevating stress from the news posting part of the site.


Installing HAProxy

HAProxy isn’t available in the default repositories for CentOS or Red Hat. In order for us to be able to install it, we need to either compile it from source (preferred) or add the EPEL repository to our server and install it using Yum.

Install epel repository

sudo yum -y install epel-release

Refresh repolist

sudo yum repolist

To list all available packages under a repo called epel, enter:

$ sudo yum --disablerepo="*" --enablerepo="epel" list available


Install HAProxy

yum install -y haproxy

sudo service haproxy status | start | stop

sudo haproxy -v
HA-Proxy version 1.5.4
Copyright 2000-2014 Willy Tarreau <>

Add HAProxy config with above 2 servers as backend

Config -

sudo vi /etc/haproxy/haproxy.cfg

backend app
balance roundrobin
server serv-1 check
server serv-2 check
# server app1 check
# server app2 check
# server app3 check
# server app4 check

Validate HAProxy

Add Stats

sudo vi /etc/haproxy/haproxy.cfg

listen stats
bind *:8181
stats enable
stats uri /
stats realm Haproxy\ Statistics
stats auth cloud:cloud

$ service haproxy restart
Access the HAProxy Stats URL

This shows the stats page


Install openssl