Nginx for Front-end Developers - Load Balancers

In this article we will see, Nginx for Front-end Developers - Load Balancers. this is a Part three of Nginx for Front-end Developers. To read the Previous articles

Above all , Load balancers are an important feature in Nginx. we will see how to configure it in this article.

What is Load Balancing?

Load Balancing is nothing but distributing the traffic to different servers.

Mainly, there are three mechanism with which the nginx distributes the request to the speicifed server.

Load Balancing Mechanism

  • Round - Robin - this mechanism distributes the request to the server according to the server weight.
1upstream backend {
2 # no load balancing method is specified for Round Robin
3 server backend1.example.com;
4 server backend2.example.com;
5}
  • Least - Connection - this mechanism distributs the request to the server which has a least active connection
1upstream backend {
2 least_conn;
3 server backend1.example.com;
4 server backend2.example.com;
5}
  • IP - Hash - The server to which a request is sent is determined from the client IP address. In this case, either the first three octets of the IPv4 address or the whole IPv6 address are used to calculate the hash value. The method guarantees that requests from the same address get to the same server unless it is not available.
1upstream backend {
2 ip_hash;
3 server backend1.example.com;
4 server backend2.example.com;
5}

upstream is the keyword used to specify the load balancer in nginx configuration.

Configuring Load Balancer in nginx

we are going to load balance the request to three different servers using the nginx load balancer.

Further, create a three simple node server with different ports and run the server.

First Server - app.js

1const express = require("express")
2
3const app = express()
4
5app.get("/", (req, res) => {
6 res.send("Welcome to Server 1")
7})
8
9app.listen(3000, () => {
10 console.log("app is listening to port 3000")
11})

Second Server - app.js

1const express = require("express")
2
3const app = express()
4
5app.get("/", (req, res) => {
6 res.send("Welcome to server 3")
7})
8
9app.listen(3001, () => {
10 console.log("app is listening to port 3002")
11})

Third Server - app.js

1const express = require("express")
2
3const app = express()
4
5app.get("/", (req, res) => {
6 res.send("app is running in server 2")
7})
8
9app.listen(3002, () => {
10 console.log("app is listening to port 3001")
11})

Meanwhile , Run all the servers with the following command

1$ node app.js

Now, servers are running. After that, you need to configure nginx to load balance between these severs.

I assume that you have already installed nginx and configured it before. if you are not, please follow this article before continuing.

create a file called node-app in the /etc/nginx/sites-available.

(Note : I am using Ubuntu , the nginx file location is different for other operating systems)

1upstream backend {
2
3 server 127.0.0.1:3000;
4
5 server 127.0.0.1:3001;
6
7 server 127.0.0.1:3002;
8
9 }
10
11server {
12
13 listen 80;
14
15 server_name localhost;
16
17 location / {
18
19 proxy_pass "http://backend";
20
21 }
22}

there are two directive that you need to note.

  • upstream - upstream is a directive to define the load balancer with contains the server name of the load balancing servers
  • server - it contains the server configuration and proxy_pass which contains the upstream load balancer

After that, you need to move the file from sites-available to sites-enabled

1$ sudo ln -s /etc/nginx/sites-available/node-app /etc/nginx/sites-enabled

Similarly , restart the nginx service

1$ sudo service nginx restart

Once the nginx service resstarted successfully. visit the http://localhost in the browser

In conclusion , that's how the nginx load balance between different servers.

To sum up, this article series covered Nginx Setup , Basic Configuration and Nginx Load Balancer

Reference :

Official Nginx Documentation

Author

Hey, I’m Ganesh, Full stack engineer.I love to write technical content and help developers like me to grow in the industry. please consider supporting me.

To Read More

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

Building a Production - Ready Node....

In this article, we will see how to build a Nodejs, TypeScript Application and d...

Nginx for Front-end Developers

This article is to explain Nginx for Front-end Developers in a much simpler way....

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...