Configuring Apache, Tomcat, mod_jk and mod_rewrite to serve up tomcat from top-level

I'm trying to set up my java app as the main component of a new site.

I have some static resources I'd like served by Apache, so I setup Apache and Mod_jk, with the following rules,

    JkMount /java_app* ajp13
    JkUnmount /*static/* ajp13
    RewriteEngine On
    RewriteRule ^/$ /java_app/ [L,PT]

Workers file and everything is working swell. When I go to http://www.example.com/, I get the index file for my java app due to the rewrite rule. If I browse around the site, I still get my java app due to mod_jk, like http://www.example.com/java_app/view/1/.

The problem: when I head back to http://www.example.com, I completely lose any session information. If I was logged in, I'm logged out, and the links from there are getting appended with jsessionids.

How do I fix this? Is this the proper way to connect apache and tomcat, or is there a better way?

EDIT:

To respond to the first answer,

Still a bit stuck. When I load up firebug, I see this when I navigate to /

Set-Cookie JSESSIONID=8D63C9682E39B81F669E277ED07542E1; Path=/javaapp 

As I browse around inside the java app, I see that the same jsessionid stays with me - firebug says I'm sending in the request headers.

When I hit / again, I get this in the response,

Set-Cookie JSESSIONID=5DC9F39CE85AB1E71B8D87EB9D485FE9; Path=/javaapp

So I guess Apache forwards on my request without my cookie information, so Tomcat gives me a new one? Can I tell mod_rewrite to say ex. ^/$ /javaapp/;jsessionid={cookie} ?


Solution 1:

Don't use mod_jk2 (which is deprecated anyway), but use mod_proxy with ProxyPass and ProxyReversePass. Much easier to configure, even your exception for static content can be expressed easily:

ProxyPass /statics !
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080

The drawback is obviously that if the Java application wants to read the client's IP address (and other client-information), it will get the Apache's address instead as he is the playing proxy. For this situations, you will need to use mod_jk

Solution 2:

I suspect it's a cookie path problem. To confirm that, you may fire up your HTTP debugger and analyse the HTTP traffic.