Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount context-referenced Tomcat application with mod_jk?

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>
like image 580
yegor256 Avatar asked Feb 28 '11 06:02

yegor256


People also ask

What is difference between mod_jk and mod_proxy?

Which connector: mod_jk or mod_proxy? mod_jk is mature, stable and extremely flexible. It is under active development by members of the Tomcat community. mod_proxy_ajp is distributed with Apache httpd 2.2 and later.

What is mod_jk connector?

The mod_jk connector is an Apache HTTPD module that allows HTTPD to communicate with Apache Tomcat instances over the AJP protocol. The module is used in conjunction with Tomcat's AJP Connector component.

What is the use of mod_jk?

mod_jk is an Apache module used to connect the Tomcat servlet container with web servers such as Apache, iPlanet, Sun ONE (formerly Netscape) and even IIS using the Apache JServ Protocol. The mod_proxy_ajp module performs a similar function to this module, while being integrated into the mod_proxy framework.

What is JkMount in Apache?

You must indicate the exact same name with the "modules" file path prefix. The JkMount directive tells Apache which URLs it should forward to the mod_jk module (and, in turn, to the Servlet containers). In the above file, all requests with URL path /application/* are sent to the mod_jk load-balancer.


1 Answers

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

like image 96
Shay Rojansky Avatar answered Sep 30 '22 19:09

Shay Rojansky