1 Answer

0 votes
To ensure proper speed and optimized functioning, incoming network traffic is spread across a group of services. These backend services are commonly referred to as a server pool or server farm. With more spread across servers, there are fewer chances of a slowdown due to a loaded server.

High traffic websites serve thousands or even millions of people each day. Part of this service is displaying content, such as image, text, and video within seconds for users. Servers need to identify the data needed and execute it reliably every single time.

Load balancing is used to prevent servers from becoming crippled when there is an overflow of requests. A load balancer sends requests to servers that can efficiently handle them to maximize speed and performance.

Load balancing is a commonly used technique and an excellent way for resource utilization, maximizing throughput, reducing latency and ensuring fault-tolerant configurations across multiple application instances.

It is a useful mechanism to distribute incoming traffic around several capable virtual private servers.

Nginx, which is popular web server software, can be used to configure as a simple yet powerful load balancer to improve your server's resource availability and efficiency.

NGINX Load Balancing

Setup Nginx Load Balancing

1. Login via SSH

First of all, login to your nginx server as the root user.

ssh root@IP_address  

2. Update all the Packages

All packages installed on it must be up to date:

apt-get update && apt-get upgrade  

3. Install Nginx Web Server

We need nginx web server installed on the virtual private server (VPS). Use the following command to install the Nginx:

apt-get install nginx  

After installation of Nginx, use the following command to check the Nginx is running:

service nginx status  

4. Append the Load Balancing Configuration

Now, open your website's Nginx configuration file in any text editor:

vim /etc/nginx/sites-available/yourdomain.com.conf  

And then append the load balancing configuration at the top of the file:

upstream loadbalancer {  

server vps1.yourdomain.com;  

server vps2.yourdomain.com;  

server vps3.yourdomain.com;  


We should have Nginx installed and listening on 80 port number on all servers listed above.

5. Add Upstream Module

To set up a round-robin load balancer, we will have to use the nginx upstream module. Within the same configuration file i.e. yourdomain.com.conf, we need to add the upstream module in the virtual host configuration.

server {  

location / {  

proxy_pass http://loadbalancer;  



6. Restart Nginx Server

Save the configuration file and restart the nginx for the changes to take effect:

service nginx restart  

This whole configuration will equally distribute all incoming traffic across the three servers (vps1.yourdomain.com, vps2.yourdomain.com, vps3.yourdomain.com), nginx can be also configured to distribute the traffic more efficiently. It comes with the option of balancing such as weight balancing, max fails and IP hash balancing.

7. Weight Balancing

We can use the weight balancing to specify the proportion of the traffic distributed to each of the servers that we listed in the upstream:

For example:

upstream loadbalancer {  

server vps1.yourdomain.com weight=1;  

server vps2.yourdomain.com weight=2;  

server vps3.yourdomain.com weight=5;  


In the above example, vps2.yourdomain.com will receive twice as much as traffic compared to vps2.yourdomain.com and vps3.yourdomain.com will receive five times more traffic than vps1.yourdomain.com.

8. Max fails

When we see the default settings of nginx, it will send data to the servers even if they are down. We can use the option of Max fails to prevent such cases.

upstream loadbalancer {  

server vps1.yourdomain.com max_fails=4  fail_timeout=20s;  

server vps2.yourdomain.com weight=2;  

server vps3.yourdomain.com weight=4;  


In the above example, nginx server will attempt to connect to vps1.yourdomain.com, and if it is not responding for more than 20 seconds, it will make another attempt. After the 4 attempts, vps1.yourdomain.com will be considered as down.

9. IP hash balancing

With this method, the visitors will always be sent to the same server. So, if a visitor received the content of vps1.yourdomain.com, it will always be transferred to that server unless the servers are down or inaccessible.

upstream loadbalancer {  


server vps1.yourdomain.com;  

server vps2.yourdomain.com;  

server vps3.yourdomain.com down;  


In the above example, vps3.yourdomain.com is known to be inaccessible, and it is marked as down.

Related questions

0 votes
asked Sep 5, 2019 in NGINX by Robin
0 votes
asked Sep 5, 2019 in NGINX by Robin
0 votes
asked Sep 5, 2019 in NGINX by Robin