Configure Tomcat with Apache using Proxy Module and Sticky Session

Configuring Tomcat Load Balancer with Apache webserver using Mod Proxy is quite easy. It’s easy when you follow the sequence and all goes well. I have listed following step-by-step on how to configure Apache with Tomcat to configure Load Balancer using Mod Proxy.

Having load-balanced is always recommended in production environment for better availability.

Apache Web Server configuration

  • Enable proxy_module, proxy_balancer_module and proxy_http_module in httpd.conf of Apache web server
LoadModule proxy_module modules/
LoadModule proxy_balancer_module modules/
LoadModule proxy_http_module modules/

Add proxy pass along with balancer name for application context root. In this example, I have proxy path as examples and balancer name as mycluster. Very important to include stickysession as not having this option will distribute same request to multiple tomcat server and you will have session expiry issues in application.

<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

As you can see in above configuration, I have added route in BalancerMember so route value can be appended to session ID. Now, let’s configure Apache to print JSESSIONID in access logs.

  • Add following in LogFormat directive


LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""%{JSESSIONID}C"" combined
  • Restart Apache Web Server

Tomcat configuration

You must configure tomcat instances with same route id as you did in BalancerMember above.

  • Add jvmRoute parameter in server.xml of Tomcat. This must be added in Engine name tag.

Tomcat instance configured with 8080 port

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

Tomcat instance configured with 8090 port

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


Check access log of apache server to ensure your request is getting routed to only one tomcat instance. You will also notice your session ID is appended with route as shown in below example.

Ex: - - [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" - - [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" - - [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" - - [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" - - [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" - - [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" - - [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" - - [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 in configuring Tomcat Load Balancer with Apache Mod Proxy and Session Sticky.

Reader Interactions


  1. Hi,

    I have done apache tomcat load balancing before. But in a big production server. I have two physical server and each server holds 12 tomcat instance. And one of the server holds the apache server. As it is a registration system, during the registration period we got very large number of concurrent request in the server. It can be above 50K request. And my experience shows that with that large request my server fail to show its normal behavior. Many request failed and many request wait for forever. And those who gets the response , take a long time for them. My server configuration is moderate. I have amost 64 GB RAM and much disk space.

    Can somebody guide me what can be the best approach to load balancing in such a situation ?

    • Thanks for visiting Jim.

      For that large concurrent users, I would go for Hardware based load balancing instead of software based. The fact is hardware based LB is more robust and have flexibility in configuration the way we want. Did you give a try with F5 LTM?

  2. Hi,

    tried as per details in the post and it does not work for me and let me explain what I am observing;
    – I am running two Tomcat’s 7 and Apache 2.4.2 on the same machine
    – the name of the machine NYVMCS6
    -having the following:

    ProxyRequests Off
    ProxyPass /rcm59018ora balancer://rcm59018cluster stickysession=JSESSIONID
    ProxyPassReverse /rcm59018ora balancer://rcm59018cluster stickysession=JSESSIONID

    BalancerMember http://localhost:8083/rcm59018ora route=server1
    BalancerMember http://localhost:8084/rcm59018ora route=server2

    and using
    from another machine, change URI into
    which is obviously failing. Changing to :

    BalancerMember http://nyvmcs6:8083/rcm59018ora route=server1
    BalancerMember http://nyvmcs6:8084/rcm59018ora route=server2

    generate URI


    – I expect only LB on URL, no Tomcat port
    – and second, it does not do LB, always connect to the first one on the list

    what did you do wrong?


  3. Hello!

    I saw your post is that you can help me? My name is Robson’m from Brazil
    I am trying unsuccessfully to make a Cluster my app in Java JSP, Apache Server 2.4 on the Windows platform, my app http: // localhost: 8080 / example, all two servers are running when you configure the Apache configuration file

    LoadModule negotiation_module modules/
    LoadModule proxy_module modules/
    LoadModule proxy_ajp_module modules/
    LoadModule proxy_balancer_module modules/

    ProxyPass / balancer://example/ skickysession = JSESSIONID nofailover = On

       BalancerMember ajp://localhost:8080 route = tomcat1 ping = 1

    Stop the service and it does not restart

    The Tomcat configuration in server.xml



Your email address will not be published. Required fields are marked *