How to mount context-referenced Tomcat application with mod_jk?
Asked Answered
H

2

5

I have an WAR application running in Tomcat at /foo context, meaning that its URL is http://example.com:8080/foo. Now I'm trying to connect Apache HTTP Server to Tomcat through mod_jk. This is my workers.properties file:

worker.list=foo
worker.foo.port=8009
worker.foo.host=localhost
worker.foo.type=ajp13
worker.foo.mount=/foo/*

Works fine, but at this URL: http://example.com/foo. I would like it to be at http://example.com. What am I missing?

ps. This is my mod-jk.conf, which is included into httpd.conf:

LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/tomcat/conf/workers.properties
<VirtualHost *:80>
  ServerName foo.example.com
  JkMount /* foo
</VirtualHost>
Headlock answered 28/2, 2011 at 6:26 Comment(0)
H
11

You basically have two options:

  1. Modify your Tomcat configuration to mount the WAR at the root. How this is done depends on how exactly you're deploying your application. This is the cleaner approach unless there's some preventing factor.
  2. Handle the problem on the Apache side by using mod_rewrite to rewrite URLs starting with / to /foo, at which point it will be passed through your JkMount to Tomcat

For the second option, your Apache configuration would look something like this:

# Turn on mod_rewrite
RewriteEngine On
# This is the rule. Use regexp to match any URL beginning with /, and rewrite it to
# /foo/remaining_part_of_URL. The [PT] (pass-through) is necessary to make rewritten
# requests go through JkMount
RewriteRule ^/(.*) /foo/$1 [PT]

# Forward all URLs starting with foo to Tomcat
JkMount /foo/* worker

(this isn't actually tested, hope it works as is!). You may also need to enable mod_rewrite in your Apache (check out your distribution, a mods-enabled directory might be the answer).

And if you need to know more about mod_rewrite (quite a powerful beast), go here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule

Hitherward answered 2/3, 2011 at 0:32 Comment(3)
I prefer the second option, mostly because there are many applications running in Tomcat, each one in its own context. Could you please give an example of RewriteRule usage in this particular case?Headlock
@Headlock Sure, added an example Apache mod_rewrite confHitherward
maybe you can help with a related question: stackoverflow.com/questions/5165781?Headlock
L
0

Here is a Mod Rewrite Solution

WORKERS.PROPERTIES

worker.list=worker1
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13
worker.worker1.mount=/foo/*           #THIS IS THE APP NAME: "FOO"

HTTPD.CONF

<VirtualHost *:80>
   RewriteEngine On
   RewriteRule ^/(.*)/Foo/$1 [PT]
   ServerName example.com             #DOMAIN NAME: "example.com"
   ServerAlias www.example.com
   JkMount /foo/* worker1
 </VirtualHost>
Lexis answered 26/10, 2013 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.