Sunday, 5 July 2020

Setting up a basic load balancer in Nginx

The aim is to achieve the following, a user will connect to machine1 running on Nginx at port 80. Requests of the form test1/* will be forwarded in such a way that one out of every three requests will be sent to an apache server running on the same machine at port 85. the remaining two requests will be sent to another machine and the response will be sent to the user


Both the machines are running on Lubuntu in Vbox.

Setting up Machine1:

In machine1 Nginx was installed first
Sudo apt install nginx
Followed by Apache2
sudo apt install apache2

Now, apache2 will fail to start since Nginx is already listening on port 80. To change the listening port edit /etc/apache2/ports.conf and change the line Listen 80 to Listen 85

You must also edit /etc/apache2/sites-enabled/000-default.conf and make the following changes

  1.  <VirtualHost *: 80> should be changed as <VirtualHost *: 85>
  2. change the DocumentRoot from /var/www/html  to /var/www/html_apache
Open folder /var/www/html and CUT and paste index.html to /var/www/html_apache

If you are unable to save changes to files and get a permission error, run your text editor as sudo. If you get errors while copy-pasting index files run the file manager as root. In Lubuntu you can open a terminal and type sudo pcmanfm

Also, create an index.html inside /var/www/html_apache/test1 and include some text so that you know the response is from apache.
Restart apache with the command 
sudo service apache2 restart
Now opening Localhost should point to Nginx welcome page and localhost:85 should show the apache welcome page

Setting up Machine2:

In machine2 you can simply install apache2, no special changes are required. Get the local IP address of machine2 using the command ifconfig (mine was 192.168.1.5). Also, create an index.html inside /var/www/html/test1 and include some text. Although in real scenarios we will be providing the same functionality, for this demo include a different text compared to the one in machine1 so that the request change can be noticed.

Setting up the load balancer:

To set up the load balancer, in machine1 you should edit the file /etc/nginx/sites-available/default (another approach will be to create a new conf file and linking it to sites-enabled, but I will be editing the default file). Find the block that says server before it starts add the block

upstream sampleloadbalancer {
    server 127.0.0.1:85 weight=1;
    server 192.168.1.5 weight=2;
}

here we are creating an upstream block with the name sampleloadbalancer and we define the two locations, the local server at port 85 and the one running in machine2. The weight parameter defines how many requests are sent to each server. YOu can also exclude a server temporarily by specifying it as down. Now finally, inside the server block Find the block that says location / and at the end of that block add a new location as shown below

location /test1/ {
proxy_pass http://sampleloadbalancer;
}

By default, it follows the round-robin algorithm. However, you can specify a different algorithm as given here.





No comments:

Post a Comment

Setting GitHub hooks in Jenkins

While setting up a Freestyle project in Jenkins we may need to set Github webhooks in Jenkins so that Jenkins will get notified each time th...