×

How to Setup a NGINX Load Balancer and Assign Weight to Particular Server

Load Balancing is a useful mechanism to distribute incoming traffic around several capable Virtual Private servers.By apportioning the processing mechanism to several machines, redundancy is provided to the application -- ensuring fault tolerance and heightened stability.

How to Setup a NGINX Load Balancer and Assign Weight to Particular Server

Setup for nginx server

The steps in this tutorial require the user to have root privileges on your VPS. You can see how to set that up in the Users Tutorial. Prior to setting up nginx load balancing, you should have nginx installed on your VPS. You can install it quickly with apt-get:

sudo apt-get install nginx

Upstream Module

In order to set up a round robin load balancer, we will need to use the Nginx upstream module. We will incorporate the configuration into the Nginx settings.

Go ahead and open up your website’s configuration (in my examples I will just work off of the generic default virtual host:

sudo nano /etc/nginx/sites-available/default

We need to add the load balancing configuration to the file. First, we need to include the upstream module which looks like this:

Please comment all the default server configuration and append the below codes

upstream myapp {


 

server 35.200.233.43;
 server 35.244.20.191;
}

We should then reference the module further on in the configuration:

server {
 location / {
   proxy_pass  http://myapp;
 }

}

 Restart Nginx:

sudo service Nginx restart

Setup for web server 1

Goto web server 1  and install apache

  • Create an Instance

  • SSH into it

  • Install Apache - apt-get install apache2

  • Change the directory to /var/www/html

  • Edit the index.html file and put your own content
    Edit the 000-default.conf file (nano /etc/apache2/sites-enabled/000-default.conf)

It should look like the below image



  • Hit the external IP of the server see whether the index page opening in the browser


Setup for web server 2

Goto web server 2  and install apache

  • Create an Instance

  • SSH into it

  • Install Apache - apt-get install apache2

  • Change the directory to /var/www/html

  • Edit the index.html file and put your own content
    Edit the 000-default.conf file (nano /etc/apache2/sites-enabled/000-default.conf)

It should look like the below image



  • Hit the external IP of the server see whether the index page opening in the browser

 

Now hitting the Nginx server IP alternatively web server 1 will open and the web server 2 will open with their respective content depending upon the priority set by the Nginx upstream




Trendy