Configure the servers
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.
Configure Nginx
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!!