Skip to main content

Tomcat Clustering Series Part 5 : NginX as Load Balancer

Hi this is my fifth part of the Tomcat Clustering Series. In this post we are going to discuss how to replace Apache httpd load balancer to Nginx as a load balancer. [Check the video below for better understanding]

Hi i finished Tomcat Clustering Series. In these series we have 2 big setups needed for Tomcat Clustering
  1. Setup and configure the Load Balancer
  2. Configure the Session Replication in Tomcat.




In first setup, we configure the Load Balancer using Apache httpd web server. Its open source and widely used.
Second setup is session replication in Tomcat through modify/add <cluster> tag in server.xml file. These tomcat instance may run on single machine or different machine.



In our Tomcat Clustering Series, the big part is configure the Load balancer (Apache httpd web server). Because there are lots of steps are involved
  1. Install the Apache httpd web server from either source or repo
  2. Compile and install the mod_jk connector
  3. Create the workers.properties file (add tomcat IP and ajp port information )
  4. Configure into httpd.conf file (Add mod_jk related settings)
these setups are take more time and very difficult to debug.

Is there is any alternative to load balancer?
    Yes, Nginx its emerging web server. this post we are going to discuss how to setup nginx as a load balancer in our tomcat cluster.

What is Nginx?
      Nginx is open source and alternative to Apache httpd web server. Its provide more performance and little memory foot print.

Install Nginx
    we can install nginx through either repository (apt-get,yum) or from source. here i m build nginx from source from here. then extract the compressed file.
./configure --help      
above command shows possible command line options available for compile
for install use this command
./configure   --prefix=/home/ramki/nginx   --with-http_ssl_module     
here
--prefix used to specify where nginx server want to install, here i m using my home folder (like /usr/local/nginx)
--with-http_ssl_module here i specified install SSL module (https), its not necessary. If we want secure webpage then this module is needed.

then compile the source
 make      

install the nginx based on our configuration
sudo make install     

now installation is done, To start the nginx
cd /home/ramki/nginx/sbin   
sudo ./nginx     

now open browser and go to http://localhost to get nginx default page.

To stop the nginx, we need to pass stop signal via -s option
sudo ./nginx  -s stop     

Configure as a Load Balancer
     Nginx configurations are stored in nginx.con file in conf/ folder.  We need to perform 2 steps to make nginx as load balancer.

First define Upstream block in nginx.conf file

            Upstream block provides simple load-balancing across upstream (backend) servers. In upstream block have unique name (here  tomcatcluster), later we need to point out from other block.

       upstream tomcatcluster  {
                server 127.0.0.1:8181;
server 127.0.0.1:8282;
server 127.0.0.1:8383;
         }

here we define the back-end servers (Tomcat instances) via server directive.
In each server directive we mention IP address of server and HTTP port number(Not AJP port number). 

Second forward (proxy) the request to back-end servers

             This step is forward the request to upstream blocks, where we define the back end servers.

 location / {
             proxy_pass http://tomcatcluster;
        }   

here if location / is already present, then remove and add this new one.

here proxy_pass directive, proxifier(forward) the requests to upstream servers. here tomcatcluster is name of the upstream block and we used http protocol to forward the request.

that's it. Load balancer is ready.

now start the tomcat instances based on my previous post (session replication). then start nginx.
now access http://localhost.

now nginx forward the request in round robin fashion to back-end servers. all servers have the session, so it can able to process the request

This is same as non-sticky session method we used in my previous posts.


If we want sticky session, then nginx directly not provide, but some 3rd party patches are available. but nginx provide ip_hash directive. Its forward all request from single IP to same tomcat like sticky session. But sticky session works based on cookie (session). but ip_hash is based on IP address of client.

If u want to configure ip_hash, then modify the upstream block, add ip_hash

upstream tomcatcluster  {
                ip_hash;
                server 127.0.0.1:8181;
server 127.0.0.1:8282;
server 127.0.0.1:8383;
         }

just restart the server, u feel like sticky session concept.

when we compare the Nginx to Apache httpd web server, nginx is provide simple configuration, and can handle huge amount of traffic, consume little memory foot print and its take little CPU load.
search Nginx vs Apache we get some amazing benchmark results. 

Screen Cast :



Popular posts from this blog

Understanding Virtual Host Concept in Tomcat

Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective. Scenario: I am going to deploy 3 website with following domain names in single tomcat http://www.ramki.com http://www.krishnan.com http://www.blog.ramki.com The following diagram is my outline. Outline structure of Virtual Host Concept in Tomcat Here my tomcat IP address 192.168.1.15. or any IP address allocated my ISP. but it should be public IP address. How all domain names are pointing to my Tomcat?                   When we purchase the domain name we need to update the our tomcat IP address to it. like or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux tha file is located at /etc/hosts Now How Setup Virtual Host Concept? Before going to setup the virtual host. first tak

Virtual Host + Apache httpd server + Tomcat + mod_jk connector

In my last post ( Virtual Host in Tomcat ) we discussed about how setup the virtual host in Tomcat. Its cost effective technique because only one public IP is enough to host multiple domain. If we have big organization and each department want to host their website in locally in different machine. then how to achieve the virtual host concept?. In this post we will see the how we do this. Update :   I posted  Virtual Host + Nginx + Tomcat  Its easy to configure, compare to Apache httpd server Problem Scenario:         In big organization they have multiple department, each department want to host their website in different machine. so these websites are accessed locally with different local IP address. When we mapping to public address then we face the problem. We have two choice either purchase as many public address or Put one server front  and delegate these request.  We going to use 2nd option. we put Apache httpd web server in front of all department servers. so onl

Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)

In this post i will show Tomcat Clustering in Docker Container. In  my previous post i discussed how to achieve tomcat clustering with Nginx Front end . Its almost same scenario, but this time we will achieve via docker container. Docker Docker  is an  open-source  project that automates the deployment of  applications  inside  software containers , by providing an additional layer of abstraction and automation of  operating-system-level virtualization  on  Linux . [4]  Docker uses resource isolation features of the Linux kernel  such as  cgroups  and kernel  namespaces  to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting and maintaining  virtual machine   --Wikipedia