Home » Centos/RHEL » Nginx Load Balancer on Centos/RHEL/Fedora

Nginx Load Balancer on Centos/RHEL/Fedora

Nginx is is free open-source high performance load balancer with caching http server as well as reverse proxy web server, SMTP, IMAP, POP server.
Nginx provide the load balancing feature nginx easily distribute the load across multiple backend servers without any technical issues which scale your infrastructure and reduce the load from single node. Nginx can be used as a HTTP load balancer which can distribute traffic to multiple applications servers and to improve performance, reliability and scalability of web applications with nginx.

The following load balancing methods (or mechanisms) are supported in nginx:

round-robin — requests are distributed equally to the application servers in a round-robin method,
least-connected — request is sent/assigned to the server with the least number of active connections,
ip-hash — a hash-function is used to determine which server should be selected for the next available request (based on the client’s IP address).
Nginx-Load-Balancer
Here we are using the following scenario.
Apache Server Node 1 10.12.124.45
Apache Server Node 2 server 10.12.124.46
Apache Server Node 3 server 10.12.124.47
Nginx Load Balancer 10.12.124.44
Webserver Load Balancing and Failover with Haproxy and KeepAlive
Postfix load balancing and High Availability with HaProxy
This article is applicable and tested on Centos, RHEL and fedora server without any issue.First we need to install nginx 1.6 repository using below command:

[root@linuxpcfix~]# rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

Install nginx web-server

[root@linuxpcfix~]# yum install nginx

after that the change nginx directory and add the following line in nginx.conf file.

[root@linuxpcfix~]# cd /etc/nginx
[root@linuxpcfix~]# vi nginx.conf
include /etc/nginx/vhost/*.conf;

now setup file with named /etc/nginx/vhost/linuxpcfix.com.conf and add the below lines.

[root@linuxpcfix~]# vi /etc/nginx/vhost/linuxpcfix.com.conf
upstream linuxpcfixapps {
server 10.12.124.45;
server 10.12.124.46;
server 10.12.124.47;
}
server {
listen 80;
server_name linuxpcfix.com www.linuxpcfix.com;
location / {
proxy_pass http://linuxpcfixapps;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

finally restart nginx daemon service on the front end server as given below.

[root@linuxpcfix~]# service nginx restart

Additional directives.
Nginx have the saveral additional directive which provides advanced features to setup nginx load balancer.

ip_hash.
The ip_hash directive permits system admin to bind the client ip with particular or single server that mean a single server will response for each request instead of other server. If a single server is in inactive status then request will forwarded to another server.

upstream linuxpcfixapps {
ip_hash;
server 10.12.124.45 weight=1;
server 10.12.124.46 weight=2;
server 10.12.124.47weight= 4;
}

Weight
Weight directive allows us to set priorities using the “weight” option for each server. The weight of a server can be describes as how often it will be used.
For example if the weight of the 10.12.124.47 server is 4 and 10.12.124.45 is 1 and 10.12.124.46 is 2.
In this situation the first request will be proxied to server 10.12.124.45, the next 2 requests to server 10.12.124.46 and the next 4 requests will be proxied to server 10.12.124.47. After that the cycle will start again from the beginning.

upstream linuxpcfixapps {
server 10.12.124.45 weight=1;
server 10.12.124.46 weight=2;
server 10.12.124.47weight= 4;
}

Temporary Remove the server
If you want to exclude the particular server from load balacing then use as given below.

upstream linuxpcfixapps {
server 10.12.124.45;
server 10.12.124.46 down;
server 10.12.124.47;
}

Failover
In case any of the upstream server stops responding, then Nginx will not be able to connect it and it will serve the next available server.
Although the client will not actually face any downtime but will experiance a long response from the server.
Also the Nginx will try to connect to the server which is currently not responding, again and again on each cycle,
In overcome this problem we can use the parameter “max_fails”. By using this parameter we can set maximum amount of connection failures before the Nginx marks this server as down and stops trying to connect that server.
By default value of this option is set to 5, which means that after 5 connection failure Nginx will stop trying to connect the server for a particular amount of time, which can be defined by the option fail_timeout and by default value of this option is 10 seconds

upstream linuxpcfixapps {
server 10.12.124.45; max_fails=5 fail_timeout=120;
server 10.12.124.46;
server 10.12.124.47;
}

Backup Servers
Backup Servers directive provide facility to keep the spare servers and will be used only when all of the working upstream servers stop responding to requests.

upstream linuxpcfixapps {
server 10.12.124.45;
server 10.12.124.46;
server 10.12.124.47 backup;
}

Set-up and configure backend servers.

After that we need to install configure (LAMP) apache, php, mysql on all backend nodes, so use the following steps to install and configure Lamp on backend nodes.

[root@linuxpcfix~]# yum install httpd
[root@linuxpcfix~]# yum install php
[root@linuxpcfix~]# yum install mysql-server mysql

Once Lamp install gets completed open apache configuration file /etc/httpd/conf/httpd.conf and append followings line as given below.

[root@linuxpcfix~]# vi /etc/httpd/conf/httpd.conf
<VirtualHost 10.12.124.45:80>
ServerName linuxpcfix.com
ServerAlias www.linuxpcfix.com
DocumentRoot /home/everdata/public_html
ServerAdmin webmaster@linuxpcfix.com
ErrorLog logs/linuxpcfix.com-error_log
CustomLog logs/linuxpcfix.com-access_log combined
</VirtualHost>

Note :: Please follow the same above steps for remaining two web-server nodes.

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry since more than 14 years.

One thought on “Nginx Load Balancer on Centos/RHEL/Fedora

Leave a Reply

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

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud