February 19, 2020

Nginx Load Balancing Configuration

Hi folks,

I am going to explain the process of load balancing the web application using Nginx

Pre-requisites


  • One machine with Nginx installation
  • Two machines with running the same web application

Test setup

All are Ubuntu server machines

  1. 10.10.1.1 - Load Balancer
  2. 10.10.1.2 - Node 1
  3. 10.10.1.3 - Node 2



Configuration

In the Nginx server machine (10.10.1.1) do the following load balancing configuration


  • Make sure you stop the nginx server if running

$ sudo service nginx stop

  • Edit the nginx.conf file 

$ sudo vi /etc/nginx/nginx.conf

  • Place the following sample content

http {
        upstream testsetup {
        server 10.10.1.2;
        server 10.10.1.3;
}
    server{
        listen 8090;
        server_name testsetup;
        location / {
            proxy_pass http://testsetup;
        }
    }
}

  • Start the Nginx server

$ sudo service nginx start

Load Balancing Test


  1. Access the 10.10.1.1:8090 in the web browser
  2. It will redirect to Node 1 (10.10.1.2)
  3. Refresh/Reload the page, the load balancer redirects to Node 2 (10.10.1.3)

Tip

To differentiate the web server, add a footer with different content (Static IP Address) in your index.html


Please send your comments and feedback to psrdotcom@gmail.com

No comments:

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com