Tomcat – Virtual Hosts with Tomcat + Mod JK

mod-jktomcatvirtualhost

I have set up Apache and Tomcat on my system and I want to combine both using Mod_JK.
I have created a mod_jk worker (ajp13_worker) and added a virtual host to my Apache config:

<VirtualHost *:80>
    ServerName      tomcat.mydomain.com
    DocumentRoot    /usr/local/tomcat/webapps/      

        # Tomcat
    JkMount /* ajp13_worker
    JkMount /*.jsp ajp13_worker
    JkMount /*.jspx ajp13_worker
</VirtualHost>

and it works fine, I end up on the default Tomcat page. The Manager applications are working fine too.

Next, I have my application, which sits in the PATH-TO-TOMCAT/webapps/myapp folder. It should be reachable under myapp.mydomain.com, so I added a second vhost, this time with "DocumentRoot /usr/local/tomcat/webapps/myapp". But it did not work: I always landed on the default Tomact page and not in the root of my app.
So I used ModRewite, to rewite the request to the context of my app. The Apache vhost looks like this:

<VirtualHost *:80>
        ServerName      myapp.mydomain.com
        DocumentRoot    /usr/local/tomcat/webapps/myapp/

        RewriteEngine On
        RewriteCond     %{REQUEST_URI}   !^/myapp/.*
        RewriteRule ^/(.*)$ /myapp/$1 [PT]

        # Tomcat
        JkMount /* ajp13_worker
        JkMount /*.jsp ajp13_worker
        JkMount /*.jspx ajp13_worker
</VirtualHost>

That vhost works, the requests are rewritten and my app is reachable under myapp.mydomain.com
But now, my Tomcat creates for every single request a new session and the session count explodes when a few users access the page.

I tried several tutorials, but most of them did not address this issue. I think I am missing something. Could someone explain to me how virtual hosts are correctly set up in the combination Apache/Tomcat/Mod-JK?

Thanks a lot,

Claude

Best Answer

Ok, I found it. In the Tomcat sever.xml, you can specify the context path for each host:

<Host name="app.mydomain.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="PATH-TO-TOMCAT/webapps/app"/>
</Host>

My mistake was, that I always tried to modify the appBase parameter and did not know about the Context element!