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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


six × 3 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close