Tomcat Load Balancer with Apache using Mod Proxy and Session Sticky

Configure Tomcat with Apache utilizing Proxy module and Sticky session

Configuring Tomcat Load Balancer with Apache net server utilizing Mod Proxy is sort of straightforward.

It is easy in the event you observe the order, and all the pieces goes effectively. I’ve described step-by-step find out how to configure Apache with Tomcat to configure Load Balancer with Mod Proxy.

It’s at all times advisable to have load balancing in a manufacturing surroundings for higher availability.

Apache net server configuration

  • Swap proxy_module, proxy_balancer_module And proxy_http_module in httpd.conf of the Apache net server
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Add a proxy cross together with the identify of the balancer for the foundation of the applying context.

On this instance, I’ve proxy path as instance and balancer identify as mining cluster.

Essential to take with you sticky session If you do not have this selection, the identical request might be distributed to a number of Tomcat servers and you’ll expertise session expiration points in an software.

<IfModule proxy_module>
ProxyRequests Off
ProxyPass /examples balancer://mycluster stickysession=JSESSIONID
ProxyPassReverse /examples balancer://mycluster stickysession=JSESSIONID
<Proxy balancer://mycluster>
BalancerMember http://localhost:8080/examples route=server1
BalancerMember http://localhost:8090/examples route=server2
</Proxy>
</IfModule>

As you may see within the configuration above, I added a route in BalancerMember in order that the route worth could be added to the session ID.

Now let’s configure Apache to print JSESSIONID in entry logs.

  • Add the next within the LogFormat directive
%{JSESSIONID}C

Ex:

LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{Consumer-Agent}i""%{JSESSIONID}C"" mixed
  • Restart Apache net server

Tomcat configuration

It’s essential to configure tomcat situations with the identical route ID as in Balancer Member above.

  • Add jvmRoute parameters server.xml from Tomkat. This must be added to the engine identify tag.

Tomcat occasion configured with 8080 port

<Engine identify="Catalina" defaultHost="localhost" jvmRoute="server1">

Tomcat occasion configured with 8090 port

 <Engine identify="Catalina" defaultHost="localhost" jvmRoute="server2">
  • Restart the Tomcat server

Verification

Generate some load on the applying and verify the Apache server’s entry log to verify your request is forwarded to just one tomcat occasion.

Additionally, you will discover that your Session ID has been added to the route, as proven within the instance under.

Ex:

127.0.0.1 - - [18/Sep/2013:10:02:02 +0800] "POST /examples/servlets/servlet/RequestParamExample HTTP/1.1" 200 662 "http://localhost/examples/servlets/servlet/RequestParamExample" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:06 +0800] "GET /examples/servlets/servlet/RequestInfoExample HTTP/1.1" 200 693 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:17 +0800] "GET /examples/servlets/reqinfo.html HTTP/1.1" 200 3607 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:20 +0800] "GET /examples/servlets/servlet/SessionExample HTTP/1.1" 200 1124 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:26 +0800] "POST /examples/servlets/servlet/SessionExample HTTP/1.1" 200 1142 "http://localhost/examples/servlets/servlet/SessionExample" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:28 +0800] "GET /examples/servlets/servlet/SessionExample?dataname=fda&datavalue=fadaf HTTP/1.1" 200 1159 "http://localhost/examples/servlets/servlet/SessionExample" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B4EC1D73CF8C7482B7D46.server2" 
127.0.0.1 - - [18/Sep/2013:10:02:32 +0800] "GET /examples/servlets/servlet/SessionExample?dataname=foo&datavalue=bar HTTP/1.1" 200 1174 "http://localhost/examples/servlets/servlet/SessionExample?dataname=fda&datavalue=fadaf" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"
127.0.0.1 - - [18/Sep/2013:10:02:36 +0800] "GET /examples/servlets/servlet/RequestHeaderExample HTTP/1.1" 200 1423 "http://localhost/examples/servlets/" "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0""B80557A1D9B48EC1D73CF8C7482B7D46.server2"

I hope this helps you configure Tomcat Load Balancer with Apache Mod Proxy and Session Sticky.

If you wish to be taught extra about Tomcat administration, take a look at this on-line course.

Rate this post
Leave a Comment