Install and Configure Nginx as load-balancer in Linux.

Nginx is one of the powerful web servers which is used to host multiple websites or creating a reverse proxy/load-balancer or both. In most of the cases, a load-balancer is used to configure highly available web applications and to minimize the downtime due to any failure. In this blog, we are going to create a nginx server as loadbalancer which will serve users requests to backend server in round-robin sequence.

 

 

Prerequisites.

  1. 3 VMs (2 web-servers and 1 nginx).
  2. Connectivity between all 3 servers. No firewall rule should block HTTP traffic from one VM to other VM.
  3. We assume that all three VMs are configured with below IP addresses.
    1. Nginx: 192.168.1.1
    2. Web-server-1 (apache): 192.168.1.2
    3. Web-server-2 (apache): 192.168.1.3

Steps to follow:

  1. Installing apache and configure a sample web page on both apache servers using below command.
  • sudo yum install httpd -y && sudo echo “web-server-1” > /var/www/html/index.html && sudo systemctl start httpd.service

 

  1. Similarly, run the above command to second apache server.
  • sudo yum install httpd -y && sudo echo “web-server-2” > /var/www/html/index.html && sudo systemctl start httpd.service

 

  1. Installing and configuring nginx server.
  • sudo yum install nginx -y

4. Now create a new file in /etc/nginx/conf.d/lb.conf and add below lines in this file.

upstream web-servers {

server 192.168.1.2;

server 192.168.1.3;

}

server {

listen 80;

location / {

proxy_pass http://web-servers;

}

}

5. Restart nginx service.

  • sudo systemctl restart nginx.service

 

6. Now you can request nginx IP(192.168.1.1) address in browser and you will see the web pages from apache servers in round-robin sequence.

Leave a Reply