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.





Setting up a basic reverse proxy using nginx

The aim was to achieve the following, a user will connect to machine1 running on Nginx at port 80. The Nginx will serve the files unless the URL is of the form test1 or test2, if the url is of the form test1 it will forward the request to an apache server running on the same machine at port 85. If it is of form test2/ then the request will be sent to another machine and that response will be served 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/test2 and include some text 

I haven't set up PHP or database. You can do that if you want.

Setting up the reverse proxy:

To set up the reverse proxy, 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 location / and at the end of that block add a new location as shown below

location /test1/ {
proxy_pass http://127.0.0.1:85;
}

This will pass any request of the form /test1/ to localhost:85/test1/ . For example, a request like /test1/apix will be forwarded to apache in machine1 as localhost:85/test1/apix

Similarly, for test2, you can just add the IP address of machine2 (if you are hosting it somewhere use its public IP) instead of the localhost address

location /test2/ {
proxy_pass http://192.168.1.5;
}

And that's it, you may also need to forward the ip address of the user to the apache servers. If not the address of the Nginx server will be recorded for each request. To do so add the headers as shown below
location /test1/ {
proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:85;
  }

You can also set the reverse proxy in such a way that all php files will be served from a particular server.







 
 

 



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...