Wiki

Clone wiki

Linux / How_to_configure_Nginx_load_balancer

Overview

Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.

Requirements

We need atleast 3 Ubuntu Box, In my scenario setup is like this

192.168.138.129 = Nginx Load Balancer

192.168.138.132 = Nginx Web01

192.168.138.133 = Nginx Web02

Steps - Log on to Web01 server using root privileges

  • Install Nginx
#!html

root@web01:~# apt install nginx
  • Once installation is completed start Nginx using bellow command
#!html

root@web01:~# systemctl start nginx

Change Nginx default page text with an identical name

  • Open Nginx default page from below command
#!html

root@web01:~# vi /var/www/html/index.nginx-debian.html
  • Change

    Welcome to nginx!

    replace with this

    Web01 nginx!

  • Now Web01 Default web page will look like below Image

web01.PNG

Log on to Web02 server using root privileges

  • Install Nginx
#!html

root@web02:~# apt install nginx
  • Once installation is completed start Nginx using bellow command
#!html

root@web02:~# systemctl start nginx

Change Nginx default page text with an identical name

  • Open Nginx default page from below command
#!html

root@web02:~# vi /var/www/html/index.nginx-debian.html
  • Change

    Welcome to nginx!

    replace with this

    Web02 nginx!

  • Now Web02 Default web page will look like below Image

web02.PNG

Now Logon to Nginx LB server using root privilege

  • Install Nginx
#!html

root@LB:~# apt install nginx
  • Once installation is completed start Nginx using bellow command
#!html

root@LB:~# systemctl start nginx
  • Now edit Nginx configuration file so that this server will act as LB and send the client request to Web01 and Web02 server.
#!html

root@LB:~# vi /etc/nginx/sites-available/default

Comment all open line and go to end of this file

  • Add below mentioned syntax end of this file and your's config file looks like below
#!html

        upstream web_rack {
            server 192.168.138.132;
            server 192.168.138.133;
        }
        server {
           listen 80;
        server_name 192.168.138.129;
           location / {
                proxy_pass http://web_rack;
    }
}

* Save file and exit.

  • Restart Nginx using below command.
#!html

root@LB:~# systemctl restart nginx

*Now Open Browser and hit LB IP address

  • See below image result for first hit

LBhit01.PNG

  • See below image result for Second hit

LBhit02.PNG

  • ByDefault Nginx load balancer work as round robin method.

Updated