Wednesday, 5 October 2016

Setup Nginx as Load Balancer on Ubuntu

HOW TO SETUP NGINX LOAD BALANCING


LOAD BALANCING : Load Balancing is an efficient mechanism which improves the distribution of workloads or traffic across several capable Virtual Private Server.Using multiple components with load balancing instead of a single component may increase reliability and availability through redundancy.The Round Robin Algorithm will sequentially distribute requests across the group of servers and easy to implement.
SETUP : To setup Nginx Load Balancing
  • You must have root privilges on your VPS.
  • Nginx must be installed on your VPS.
By using "apt-get", you can easily install it.

sudo apt-get install nginx


UPSTREAM MODULE : In order to setup Round Robin Load Balancer,we have to use Nginx upstream module and include configuration into the Nginx setting.





sudo nano /etc/nginx/sites-available/default


Now, add the load balancing configuration to the file.

1. Include the Upstream Module


upstream xyz  
{
     server xyz1.example.com;
     server xyz2.example.com;
     server xyz3.example.com;
}


2. Now, reference this module in the configuration. request from http://xyz would be distributed to xyz1.example.com, xyz2.example.com and xyz3.example.com

server
 {
     location / 
     {
        proxy_pass  http://xyz;
     }
}


Now, restart nginx with:  


sudo service nginx restart




Several directives are used to direct site visitors more effectively to equally distribute load across virtual servers.
WEIGHT : Nginx allows us to assign weight to machines in number specifying the proportion of traffic that should be directed to each server.




upstream xyz
{
          server xyz1.example.com weight=1;
          server xyz2.example.com weight=2;
          server xyz3.example.com weight=4;
 }


If a server is known to be inactive, it should be marked as down like 





server xyz3.example.com.com  down;

MAX FAILS : There are two factors associated with the max fails: max_fails and fall_timeout.
Max_fails refers to the maximum number of failed attempts to connect to a server should occur before it is considered inactive.
Fall_timeout specifies the length of that the server is considered inoperative.

Now, the configuration look like :
Once the time expires, new attempts to reach the server will start up again. The default timeout value is 10 seconds.

upstream xyz
{
       server xyz1.example.com.com max_fails=3  fail_timeout=15s;
       server xyz2.example.com weight=2;
       server xyz3.example.com weight=4;      
}

This is all about using Nginx as a load balancer. Any doubts or suggestions are most welcome in comments.

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel