Setting up Tomcat behind Apache 2.2 in a hierarchical path

Posted on September 28, 2007
Tags: java

The situation I want to deal with is this:

Public Web Root
  |-plone
  \-portals
     |-portal1 (server A)
     \-portal2 (server B)

In apache 2.2 they (the writers of the Apache docs) seem to prefer the use of mod_proxy_ajp, which is an AJP/1.3 driver for mod_proxy. At the apache end we just install mod_proxy_ajp and add a line such as the following to the proxy_ajp.conf file:
ProxyPass /portals/portal1 ajp://nigel-dev:8009/portals/portal1

Note that the paths on the proxy host and the application server are the same. This is important, as at the application end it will only know its path name from its context, and unless you want to do some url rewriting, and possible use something called ProxyHTMLURLMap. It, like most potentially useful software, isn’t available in Red Hat, so we can’t investigate this option.

So, the purpose of this post is to explain how to get the application server’s paths to match the proxies. Do the following:

  1. Create the path structure with in the $CATALINA_HOME/webapps directory, ie $CATALINA_HOME/webapps/portals
  2. Install the WAR or webapp install into its place in the hierarchy
  3. Create a context xml file for each application. These go in $CATALINA_HOME/conf/Catalina/localhost, and are named the same as the webapps path, with # in place of /. ie $CATALINA_HOME/conf/Catalina/localhost/portals#portal1.xml and $CATALINA_HOME/conf/Catalina/localhost/portals#portal2.xml.
  4. Inside these files put the following:
<Context docBase="portals/portal1.war" cookies="true">  
  
</Context>  

The cookies is important to force the use of cookies as opposed to URL rewriting to preserve the sessions. The docBase is obviously the path to the webapp or WAR file.