Install And Configure Steps NGINX As A Load Balancer on Ubuntu 16.04
Install And Configure Steps NGINX As A Load Balancer on Ubuntu 16.04.
A load balancer is a distributes that is very useful for the workloads across multiple servers. Load balancing increases fault acceptance of your site and improves more to the performance.
In this article we will guide you through installing NGINX and configuring NGINX to do Round Robin load balancing. For this, we are going to use Ubuntu 14.04. We have given you a network diagram below that is an overview of the set up of it.
So Let now,we start it by making sure that system you have is up to date by running the following command:
sudo apt-get update
First Installing NGINX on Ubuntu 14.04
Next, we will go to install NGINX with the command given below:
sudo apt-get install nginx
Once you have installed, verify that NGINX is working and you can done it by going to your server IP address in your browser. You should get a page same as the one below:
Configuring NGINX Load Balancing
Now when you have done it mean NGINX is installed, we required to edit our NGINX config and time is to enable load balancing with upstream. The NGINX default configuration file is on Ubuntu systems and is located at /etc/nginx/sites-available/default . You easy edit it with the given command:
sudo nano /etc/nginx/sites-available/default
As the default configuration is simple and easy set up for a basic website, we can easily remove it and add in the following configuration. We are going to add upstream to configuration we have,that is an NGINX module and it enables load balancing. In upstream replace the private IPs in this example with web servers of your sytem. You can also try private IPs, public IP’s or hostnames and now you are going to set the server_name to you domain name or IP address. You can set the upstream name and name it that you like, just so long as it is the same on the proxy_pass. In our case, we are using www the name.
upstream www { server 10.5.105.2; server 10.5.105.3; server 10.5.105.4; } server { listen 80; server_name www.solutionclub.in; location / { proxy_pass http://www; } }
Absolutely necessary, this configuration help to enables NGINX to listen Domain name you have or Public IP and once when it is accessed, it will start passes traffic to the next web server as round robin sequence.
Now that we have done our configuration changes we havt to restart NGINX with the command following .
sudo service nginx restart
You can now test it out just by going to your hostname or IP of your load balancer in your browser. For testing, we have 3 default NGINX servers and that are sitting behind our load balancer, each has a slightly modified index page.
With the above configuration , our load balancer start distributing the load equally across 3 servers we have. However, there are some rare cases in which you may need a particular web server to have more traffic than another.
NGINX Load Balancing Weight example
To complete different distribution, now we can add the NGINX weight directive. So, the default weight is 1, which we also have in the example above uses. If we want that our 1st webserver to gain 50% of our traffic we must use the following configuration.
upstream www { server vps1.solutionclub.in weight=1; server vps2.solutionclub.in server vps3.solutionclub.in }
You can try with various weights per web server to make your configuration that is ideal for you . Remember that always restart NGINX when adding to your configuration files.
sudo service nginx restart
Weight Balancing
We can apply this option to specify the proportion of the traffic distributed to each of the servers we listed in the upstream.
For example:
upstream www { server vps1.solutionclub.in weight=1; server vps2.solutionclub.in weight=2; server vps3.solutionclub.in weight=5; }
In the given example, vps2.solutionclub.in will get double as much traffic compared to vps2.solutionclub.in, and vps3.solutionclub.in will get 5 times more traffic as compare to vps1.solutionclub.in.
Max Fails
If you are on the the default Nginx settings,then it will send data to the servers even if they are down. We need to use the option of Max fails to prevent such kind of cases.
upstream www { server vps1.solutionclub.in max_fails=4 fail_timeout=20s; server vps2.solutionclub.in weight=2; server vps3.solutionclub.in weight=4; }
In the above case, Nginx will test to connect to vps1.solutionclub.in and if it do not responding for more than 20 seconds then it will again try to attempt. After four test vps1.solutionclub.in will be considered it as down.
However, if user is doing refreshing to their page, it might be possible to confusing them that it will getting different pages. So, We need to fix it with IP hash.
How to Use IP HASH On Your NGINX Load Balancer
To eliminate all confusion,you should add ip_hash to your upstream configuration. IP hash do work when you using your client’s IP address as a hashing key so that your client will always find to the web server same . We have Below show an example of how to adding IP hash. Remember you need to restart NGINX when making any of the changes.
upstream www { ip_hash; server vps1.solutionclub.in; server vps2.solutionclub.in; server vps3.solutionclub.in down; }
In the above example, vps3.solutionclub.in is known to be inaccessible, and it is marked as it is down.
And For the more details : Nginx Help