Sunday 1 April 2018

Nginx06-Upstream-Directive

Upstream Directive and Load Balancing

  • We have did this exercise for proxying and load balancing Apache-Tomcat with Nginx.
  • Here while using the default function of loadbalancing with Nginx Upstream directive it uses round-robin method.
  • Means if we have configured LoadBalancing between 2 nodes then the Nginx will forward traffic to 1 hit to 1 server and the 2nd hit to another and goes on.
  • If we are clear that one of the 2 machines can handle more load then we can prioritize that host to have the number of consicutive connections through the weight function used along with the server in the upstream directive.
  • Let us try this for the above Load Balancing concept of Tomcat
  • OLD
upstream myTomcat {
    server dev01.linux-library.ll:8080;
    server dev02.linux-library.ll:8081;
}
  • NEW
upstream myTomcat {
    server dev01.linux-library.ll:8080 weight=1;
    server dev02.linux-library.ll:8081 weight=2;
}
  • Means 1 connection will be forwarded to dev01 and 2 connections will be forwarded to dev02.
  • We are going to have 2 times the load of dev01 coming in dev02

Nginx05-VHost-Files

VHosts Files

  • Till now we have seen how to configure a basic webserver for our local host.
  • Let us now see the process of configuring a custom website.
  • Either we should have our DNS configured for the site we are looking for or we can put an entry in the /etc/hosts file for the IP which we want to host from.
  • In my case I want to name my website as www.lltest1.ll, so I am adding an entry for that in the /etc/hosts file
# vi /etc/hosts

192.168.1.110 www.lltest1.ll lltest1.ll
  • Now I'll create a config file for this website. For this we need to create a config file in the vhost.d directory.
  • If we name our custom website config files with the name of the site followed by an extension .conf would be easier for us to know which config file corresponds to which site.
# cd /etc/nginx/vhost.d/
# vi www.lltest1.ll.conf

server {
 listen 80;
 
 root /var/www/html/lltest1;
 index index.html index.htm index.php;

 server_name www.lltest1.ll lltest1;
}
  • In the above case we are giving the alias name of our website. As in nginx we have server_name directive capable to handle both server name and server alias functions.
  • After creating the config file check for any errors.
# nginx -t
  • Now let us create the root directory for this site and an index file
# mkdir -p /var/www/html/lltest1
# cd /var/www/html/lltest1
# echo "WELCOME TO LINUX_LIBRARY_TESTING SITE" > index.html
  • Reload the service and test the new site
# nginx -s reload
# elinks http://www.lltest1.ll
# elinks http://lltest1