HaProxy is the Reliable, High Performance TCP/HTTP Load Balancer.
It is used as Load Balancer, also known as Front End server. Its main purpose is to direct requests to application servers. Usually they have only proxy service running and direct requests to separate servers, however they also can forward requests to the local services (such configuration not recommended however). HaProxy can be used for both HTTP and HTTPS traffic.

Why would you need that?
The answer is simple. Such configurations are used by high load systems which can not be served by a single server system.
If you have a real popular web site with a lot of visitors then load balancing is a good option for you.

Installing HaProxy on Centos 6 server.

To begin the installation of haproxy on Centos 6 server a free IP, not bound to port 80, is needed.

There are two ways to install HaProxy on Centos server,
1. Compile from sources.
2. Install from Epel repository.

We prefer the second way since it is much easier to track updates.

When logged in via ssh to your server type the following commands:

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm 
# yum -y install haproxy

Once HaProxy installed we can get its config and init script from official site:

# wget http://layer1.rack911.com/haproxy/haproxy-standard.cfg -O /etc/haproxy.cfg
# wget http://layer1.rack911.com/haproxy/haproxy.init -O /etc/init.d/haproxy

And add an execute permissions to the init script:

# chmod +x /etc/init.d/haproxy

Now we are ready to start customizing configs.

First of all lets set number of CPUs on the server,

# cat /proc/cpuinfo |grep model
model        : 26
model name    : Intel(R) Xeon(R) CPU           X5550  @ 2.67GHz
model        : 26
model name    : Intel(R) Xeon(R) CPU           X5550  @ 2.67GHz

So my VPS has got two processing cores.
Open a file called /etc/haproxy.cfg in preferred text editor and find a line which reads as the following:

  nbproc      4 # Number of processing cores. Dual Dual-core Opteron is 4 cores for example.

And update that to apply your configuration, in my example:

  nbproc      2 # Number of processing cores. Dual Dual-core Opteron is 4 cores for example.

The next line you will want to edit is :

listen  http_proxy

Set IP address where your website will be pointed to:

listen  http_proxy

Almost done,
The next thing to do is to set the servers

server server1 weight 1 maxconn 512 check
server server2 weight 1 maxconn 512 check
server server3 weight 1 maxconn 512 check

Set your servers’ IPs there, you are free to add as many servers as you need, HaProxy will automatically remove servers which it can’t connect to and then enable them back once they are available.

That’s it, now we need to restart the service and add that to startup:

# service haproxy start
# chkconfig --add haproxy

That was the basic round-robing setup, for more information visit HaProxy official site:

So what we have just done is set up HaProxy load balancing server and set 3 http servers to send requests to.
All the requests will be forwarded to the specified servers automatically so total load will be divided between those three servers.

Save this article