Load Balancing with NGINX

In this article, we will explain how to easily make Load Balancing using the NGINX webserver.

In this exercise, we will use three servers as follows:

The LB server will be used as a Load Balancer (more info on Load Balancers can be found in this article ).
Server SVR1 and Server SVR2 will be used as Webservers in the Server Pool (more info about the Server Pool can be found in this article ).

Here is an illustration of Load Balancing that we will practice:

The Load Balancer will receive all requests from all Clients, then distribute them to the servers in the Server Pool, then the results of processing the requests are returned to the Client. If one of the servers is down, the Load Balancer will forward requests from the Client to other normal servers in the Server Pool.

Load Balancing Options

There are three Load Balancing options on NGINX, namely:

1. Round Robin

Round-Robin is the default Load Balancing option on NGINX, where when we do not specify certain options in the NGINX configuration, this option will be active.

In this Round-Robin option, requests from the Client will be routed to all servers in the Server Pool in turn. For example, if we use two servers in the Server Pool, the first request will be routed to Server 1, the second request will be routed to Server 2, then the third request will be routed back to Server 1, and so on.

2. Least-Connected

The next option is Least-Connected. In this option, the request will be routed to the server that has the least number of active connections. To use this option, list least_conn in your NGINX configuration.

2. IP-Hash

While the last option is IP-Hash. This option instructs NGINX to use the Hash function to determine which server will handle the request from the Client based on the Client’s IP.

With this option, the User Session will be sticky or stick to a server. That is, all requests from a client will be served by the same server. This is of course different from the previous two options, where in the previous two options requests from the Client will not necessarily be handled by the same server, because the user session is not sticky on a particular server. To use this option, specify ip_hash in the NGINX configuration.

Load Balancing Settings

Here are the steps for setting Load Balancing on NGINX:

1. Install Web Server

– Install NGINX on Server SVR1 and Server SVR2 with the command as below:

$ sudo apt-get install -y nginx

– Change index.html :

$ uname -n | sudo tee /var/www/html/index.html

Note:
More info on installing NGINX can be found in this article .

2. Install Load Balancing

– Install NGINX on the LB Server with the command as below:

$ sudo apt-get install -y nginx

– Change the configuration file /etc/nginx/sites-available/default to be like this:

upstream web_backend {
# Uncomment for the IP Hashing load balancing method:
# ip_hash;
# Uncomment for the Least Connected load balancing method:
# least_conn;
# Replace the IP addresses with the IP addresses
# (or host names) of your back end web servers.
# Examples:
# server www1.example.com:8080;
# servers 192.168.1.100;
servers 10.87.0.197;
servers 10.87.0.196;
}

servers {
listen 80;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_backend;
}
}

– Reload NGINX services:

$ sudo service nginx reload

3. Test Load Balancing

– Open browser, call Server LB (10.87.0.198)

– Refresh the browser many times

– If the result is like the picture above, which is a server that handles alternate requests (SVR1 and SVR2) then your Load Balancing has gone well.

Thus the article about Load Balancing with NGINX, thank you for visiting and hopefully useful.

Leave a Reply

Your email address will not be published.