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).
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:
Install nginx web-server
after that the change nginx directory and add the following line in nginx.conf file.
[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.
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.
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.
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.
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.
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
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.
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 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.
<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.
One thought on “Nginx Load Balancer on Centos/RHEL/Fedora”-
Pingback: Nginx RTMP Media server With Nginx-RTMP module On Centos - The LinuxPcFix