Setup Nginx as a Load Balancer in Hindi | Nginx Tutorial | Configure Load Balancer | Two App Server

Опубликовано: 27 Декабрь 2021
на канале: Techno Pathshala
1,163
29

Setup Nginx as a Load Balancer in Hindi | Nginx Tutorial | Configure Load Balancer | Two App Server

#nginx #load #balancer #apache #ubuntu

How to set up Nginx load balancing server on Ubuntu 20.04

In this tutorial, we'll learn how to set up an Nginx load balancing on Ubuntu 20.04 server.
Let's get started!

Requirementsa
Three servers running on Ubuntu 20.04.
For the purpose of this article, we'll use the following setup:
• Nginx Load Balancer : 192.168.0.253
• App server1 : 192.168.0.249
• App server2 : 192.168.0.251

Install Nginx Server
First, you will need to install Nginx on all servers. You can install it with the following command:

apt-get install nginx -y

Once installed, start the Nginx service and enable it to start at system reboot:
systemctl start nginx
systemctl enable nginx

Set up Application Servers

Set up an Nginx Load Balancer

Next, you will need to set up a load balancer server that distributes load among both application server.

First, remove the Nginx default configuration file and create a new load balancer configuration file:

rm -rf /etc/nginx/sites-enabled/default
nano /etc/nginx/conf.d/load-balancing.conf

Add the following lines:

upstream backend {
server 192.168.0.249;
server 192.168.10.250;
}

server {
listen 80;
server_name teknopathshala.com;

location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://backend;
}
}

Save and close the file when you are finished.
verify the Nginx for any syntax error with the following command:
nginx -t

Next, restart the Nginx service to apply the changes:
systemctl restart nginx

Verify Load Balancing
Now, open your web brower and access the load balancing server using the URL http://192.168.0.253. You will be redirected to the application server1
Now, refresh the page continuously after some time your application should be loaded from the second application server as shown below: