Load Balancing with Nginx

Nuwan Tissera
3 min readJan 31, 2020

--

It's all about efficient distribution.

Load balancing is distributing of network traffic efficiently across a group of backend servers. The goal of this is to obtain maximum resource optimization, maximum throughput and minimum latency.

Nginx[1] is a popular open-source software for load balancing. In this, I will do some basics in load balancing with an example. First, We will configure a setup as below.

I will dive into the installation of Nginx (on Ubuntu instance) on LB node.

sudo apt-get update
sudo apt-get install nginx

Then visit the IP of the instance and you may find the page like follows

This is the default configuration. Now we have to replace this with our configurations so that Nginx is aware of the servers where the traffic wants to route.

Backup /etc/nginx/sites-available/default file.

sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default-backup

Edit the default file now using administrator mode.

sudo su
vi /etc/nginx/sites-available/default

After modification, the file will look like this.

upstream backend {
server 10.128.15.224;
server 10.128.15.225;
}

server {
listen 80;
location / {
proxy_pass http://backend;
}
}

Then restart the Nginx.

sudo service nginx restart

Now we will ssh to servers connected to LB.

sudo apt-get update
sudo apt-get install apache2

Visit the instance by IP and you will see Apache default page.

Edit the file /var/www/html/index.html and restart Apache.

Server 1 /var/www/html/index.html

<html>
<head>
<title>Server 1</title>
</head>
<body>
<h1> I am from Server 1 <h1>
</body>
</html>

Server 2 /var/www/html/index.html

<html>
<head>
<title>Server 2</title>
</head>
<body>
<h1> I am from Server 2<h1>
</body>
</html>

Now check the status of the apache. It should be active in all servers.

sudo systemctl status apache2

Do this to all the servers mentioned in upstream.

After that, visit the external IP of the LB. So you should see the page switching between servers.

This is a simple load balancing with Round robin algorithm. This is also known as “next in the loop”. If we are using Active-active connection, the default method of the round-robin will be fine. We will discuss the other mechanisms in a separate blog.

Load Balancing Algorithms in Nutshell

Only a few are listed here… for more visit [2]

1. Round Robin (Default)

Rotating between servers in a sequential manner.

2. Random

Rotating between servers in random order.

3. Weighted Round Robin

Every server is given a static number based on weighting. Servers with high numbers get more requests.

upstream backend  {
server backend1.example.com weight=1;
server backend2.example.com weight=2;
server backend3.example.com weight=4;
}

The default weight is 1. With a weight of 2, traffic will be twice as backend1, and backend3, with a weight of 4, will have 2x traffic as backend2 and 4x as backend 1.

4. Least connections

Detect the no. of open connections for each server and route traffic to the least busy ones.

upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}

5. Least traffic

Detect the amount of traffic for each server and route traffic to ones with less traffic.

6. Least latency

Detect the bitrate from each server and route traffic to ones with least latency.

7. IP Hash

First three octets of the IPv4 address or the whole IPv6 address are used to calculate the hash value. Requests from the same IP address get to the same server unless it is not available.

upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}

if you want to keep one server at rest, use down as below.

upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
}

Thank you !!

References

[1] https://www.nginx.com/resources/glossary/nginx/

[2] https://docs.nginx.com/

--

--

Nuwan Tissera
Nuwan Tissera

Written by Nuwan Tissera

Software Engineer WSO2 | Msc in CSE UoM | Blogger

No responses yet