Nginx implementation with HAProxy for Load Balancing

15.05.2017.

We live in a time when the high availability and speed of network services are critical parts of every infrastructure. By using the HAProxy Reverse Proxy Server, and its capacityto balance both load and high availability, we ensure that such systems are immune to infrastructure malfunctions.

HAProxy is a free, powerful, flexible and straightforward reverse proxy that provides the option of Load Balancing. The program was written by Willy Tarreau, and also supports SSL, compression, keep-alive, configurable log prints, and header changes in the application. Since it is not demanding and needs a small amount of resources to run, web pages such as StackOverflow and Github use it. We will describe below how to set up a HAProxy proxy server, with a task to balance the load on two Nginx web servers.

 

The basics of HAProxy

HAProxy supports two modes of operation, TCP layer 4 mode, in which HAProxy passes packages on a particular IP address and tcp port to configured backend servers, and HTTP layer 7 mode, in which HAProxy parses HTTP requests and forwards them to web servers. We will use HTTP layer 7 mode.

 

Load Balancing Types

 

Roundrobin

This is the simplest algorithm for load balancing and forwards each new request to the next configured backend web server.

 

Lastconn

The HAProxy server submits the request to the backend web server with the smallest number of requests and is very good in scenarios that involve large loads.

 

Source

In this case, the HAProxy server forwards the request to the backend servers based on the user’s IP address.

 

 

Nginx implementation with HAProxy for Load Balancing

 

 

Logical display

 

System Requirements and Network Configuration


3 CentOS 7 servers:
HAProxy 192.168.0.101
nginx1 192.168.0.201
nginx2 192.168.0.202

 

HAProxy server

We install HAProxy

 

yum install haproxy

 

We copy the default configuration file

 

cd /etc/haproxy
cp haproxy.cfg haproxy.cfg.back

 

In /etc/haproxy/haproxy.cfg we replace all frontend and backend parts of the configuration

 

frontend haproxy *:80 ### we set a frontend called haproxy to listen on the port 80 
default_backend websrv ### we set the name of the backend to which the requests will be directed 

backend websrv ### We define the backend that we have previously set 
balance roundrobin ### We define the HAProxy server operation mode 
server nginx1 192.168.0.201 check ### We define backend servers, in this case nginx1 and nginx2 
server nginx2 192.168.0.202 check

 

We configure firewalld firewall to admit tcp port 80

 

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

 

We enable auto start and launch the HAProxy service

 

systemctl enable haproxy
systemctl start haproxy

 

Nginx1 and Nginx2 web servers

 

We enable EPEL repository and install Nginx service

 

yum install epel-release
yum install nginx

 

We create test index.html files

 

cd /usr/share/nginx/html
echo “<h1>nginx1</h1> > index.html
### na nginx2
echo “<h1>nginx2</h1> > index.html

 

We configure firewalld firewall to admit tcp port 80

 

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

 

We enable auto start and launch Nginx service

 

systemctl enable nginx
systemctl start nginx

 

Testing

 

We open the address in the web browser:
http://192.168.0.101

Pressing the F5 key on the keyboard to refresh the page, should open a new index.html file depending on which server the request was redirected to, in this case either nginx1 or nginx2. In this way, we have successfully balanced the load between two Nginx web servers, using the HAProxy server.