About Rama Krishnan

Ramki is a Application Developer working in the C-DAC, Pune. He has Extensive Design and Development experience in Java, Java Server Faces, Servlets, Java Persistent API (Hibernate), CDI, EJB and experience in applying Design Patterns of JavaEE Architecture.

Tomcat Clustering Series Part 5 : NginX as Load Balancer

This is my fifth part of the Tomcat Clustering Series.[Check the video below for better understanding]. In this post we are going to discuss how to replace Apache http load balancer to Nginx as a load balancer. 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 it is 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 your 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. Then 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. If you search Nginx vs Apache we get some amazing benchmark results.

Screen Cast :


 

Reference: Tomcat Clustering Series Part 5 : NginX as Load Balancer from our JCG partner Rama Krishnan at the Ramki Java Blog blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


× one = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books