Nginx as a Load Balancer

I am a Software Engineer. I have worked with different languages like NodeJS and Python. I have keen interest in Backend Engineering, Cloud Native Technologies and System Architecture.
Search for a command to run...

I am a Software Engineer. I have worked with different languages like NodeJS and Python. I have keen interest in Backend Engineering, Cloud Native Technologies and System Architecture.
No comments yet. Be the first to comment.
What is a Bastion host? A bastion host is a server whose purpose is to provide access to a private network from an external network, such as the Internet. Because of its exposure to potential attack, a bastion host must minimize the chances of penetr...

What is helm? Helm is a package manager for Kubernetes. It makes updates and rollback of applications more efficient and improves team collaboration. Kubernetes objects are challenging to manage Helm automates maintenance of YAML manifests for Kubern...

Introduction Automation is the key to streamlining your work processes for any software development project. GitHub actions allow you to add your software development lifecycle workflows directly to your repository. GitHub Actions is a continuous int...

Flake8 is a popular lint wrapper for python. In this article, we will understand the uses of flake8 against our python projects. What is flake8? Install flake8 in your project Run flake8 against your project Configure flake8 in config.cfg What ...

In this article, we will go through the installation of Kubernetes in your local system and run a simple nginx server via kubernetes. We need to follow this process Introduction to kubernetes Install minikube locally Run your first pod Access kub...

You need to have node preinstalled in your system or you can use any server of your choice.
Our server running on the following sockets:
I will be creating 3 simple node servers running on my local machine.
Run npm init in your root project directory.
Then create three files server_1.js, server_2.js, server_3.js.
Here is what my project directory will look like.
.
├── package.json
├── server_1.js
├── server_2.js
└── server_3.js
Here is the code for server_1.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<h1>Hello from - server_1</h1>\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Run node server_1.js for starting this server on 3000.
Here is the code for server_2.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3001;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<h1>Hello from - server_2</h1>\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Open a new terminal and run node server_1.js for starting this server on 3001.
Here is the code for server_3.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3002;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<h1>Hello from - server_3</h1>\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Open a new terminal and run node server_3.js for starting this server on 3002.
If you want to install Nginx from the source code, please follow the guideline of this article https://techblog.akashojha.com/install-nginx-from-source-code
If you already running Nginx, first you need to stop it.
sudo systemctl stop nginx
Now let's add a new file load_balancer.conf in /etc/nginx directory.
sudo vim /etc/nginx/load_balancer.conf
Let's add the following content in your load_balancer.conf file.
events {
}
http {
upstream nodeapis {
server localhost:3000;
server localhost:3001;
server localhost:3002;
}
server{
listen 8888;
location / {
proxy_pass http://nodeapis/;
}
}
}
Let's start your Nginx server.
sudo nginx -c /etc/nginx/load_balancer.conf
If you go to your terminal and run the following command
while sleep 0.5; do curl http://localhost:8888/; done;
You should be able to see the response like this in the terminal
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
<h1>Hello from - server_1</h1>
<h1>Hello from - server_2</h1>
<h1>Hello from - server_3</h1>
This is following the round-robin method i.e., choose from all the local servers with equal priority.
You can go to your browser and open http://127.0.0.1:8888 and see the same response by reloading the server again and again.
So, we have learnt how to use Nginx as a load balancer.
Happy Learning!!