views:

1070

answers:

4

Ok, so I want to get a webapp running in tomcat (5.5) to run behind apache 2 (2.2.3 to be precise) serving from the root of the site (i.e. without the context), with static content being served via apache.

So if the app is running under "/myapp" on tomcat I want to use apache (plus mod_rewrite) to make it behave as if it's running under "/" instead.

Mod_jk is setup and working ok. I can access the app from "/myapp", but I can't quite get the last bit working. Below is the config I've got for mod_rewrite to try and get this working. It correctly gets rewrites /static/ urls to get apache to serve them from the unpacked webapp and if I enable the rewrite log I see that it does attempt to pass through all other requests to /myapp via mod_jk. However it seems that mod_jk is not processing the request afterwards.


JkMount /myapp/*      worker1

RewriteEngine On

# ensure static stuff gets served by apache
RewriteRule ^/static/(.*)$ /var/lib/tomcat5.5/webapps/myapp/static/$1 [L]
# everything else should go through tomcat
RewriteRule ^/(.*)$ /myapp/$1 [L,PT]

When I've done this with apache 1 in the past I've had to make sure mod_jk get's loaded before mod_rewrite, but I can't seem to achieve this under apache 2. Any thoughts? How do other people usually do this?

A: 

We use the 'R' flag instead of 'PT':

RewriteRule ^/(.*)$ /myapp/$1 [L,R]

Edit: I missed the point to not alter the URL the user sees. An alternative way is to do:

JkMount /* worker1
JkUnmount /static/* worker1

Then you won´t need the RewriteRule's.

And according to Apache Tomcat Site the new default settings of the mod_jk are incompatible with mod_rewrite and you should use +ForwardURICompatUnparsed.

Leonel Martins
That will just do a redirect though, so the final URL (that the user sees) with have /myapp at tge beginning. Basically I'm trying to get http://mysite.com/ to work, rather than having to redirect the user to http://mysite.com/myapp/ (which is a bit ugly).
John Montgomery
+1  A: 

Managed to get this working in the end. It appears that I need to set a JkOption to:


JkOptions     +ForwardURICompat

And then mod_jk looks at the rewritten URL. Must admit not 100% sure quite why this works, but I believe it's reverting to an older form of behaviour. If anyone can tell me a better/more correct way of doing this I'd love to know.

John Montgomery
A: 

It might be easier or more transparent what happens if you use either Tomcat Virtual Hosts or different connectors for different hosts and just deploy root applications (at "/") and setup Apache mod_jk forwarding to the different connectors or virtual hosts.

Note: When I needed this I've worked with different connectors for different tomcat hosts or Engines (can't remember) and deployed ROOT applications. I've never tried virtual hosts (name based) in tomcat, only guessing that this could work.

Benefit: no path translation, thus a lot easier to understand once you have to make changes to the installation months after initial deployment.

I confess, it feels somewhat strange, but the benefit of readability is worth a lot to me.

Olaf
A: 

May be better use Apache for proxy instead of mod_jk. Something like this:

ProxyPass /static/ http://localhost:8080/myapp/static/