Firstly, do not use mod_jk. Instead, enable mod_proxy, mod_proxy_ajp13 and mod_rewrite to pass on Apache 2 requests to Tomcat. This has benefits:
- Allows you to use Apache to control resources that would otherwise be under the control of Tomcat (i.e. you can use Apache's mod_rewrite on a .cfm resource where previously you would have to use Tomcat's rewriter). I find this preferable; I can use the plethora of Apache modules for cfm requests and leave Tomcat solely to dealing with the Java app side of things.
- Allows you to easily setup a single Tomcat host for multiple sites.
How to use mod_proxy_ajp13 with mod_rewrite
To use mod_proxy_ajp13, one simply adds rewrite rules to an Apache virtual host (e.g. in its .htaccess file). Here is a simple example:
RewriteEngine on
RewriteRule ^(.*?)/(.*?)\.cfm$ ajp://localhost:8009/mysite/$1/$2.cfm [P]
RewriteRule ^(.*?)\.cfm$ ajp://localhost:8009/mysite/$1.cfm [P]
My mod_rewrite skills aren't particularly hot, and I imagine you could put that in one rule, but hopefully that paints the picture. Simply put, we take all .cfm files and rewrite them to our ajp proxy.
How does that help?
Because we are proxying the request to Tomcat we can use any domain we like; in the case above I was using localhost but the apache virtual host could be for mysite.com. Let's say we have another Apache virtual host for myubersite.com, and setup rewrite rules like:
RewriteEngine on
RewriteRule ^(.*?)/(.*?)\.cfm$ ajp://localhost:8009/myubersite/$1/$2.cfm [P]
RewriteRule ^(.*?)\.cfm$ ajp://localhost:8009/myubersite/$1.cfm [P]
Now we have two Apache virtual hosts proxying .cfm requests to Tomcat on localhost. Here's how we could setup the localhost in Tomcat's /conf/server.xml to cater for the two sites:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="mysite/" docBase="mysite/" reloadable="true" privileged="true" antiResourceLocking="false" anitJARLocking="false"/>
<Context path="myubersite/" docBase="myubersite/" reloadable="true" privileged="true" antiResourceLocking="false" anitJARLocking="false"/>
</Host>
I have just this minute set this up on my Ubuntu desktop and currently, unsurprisingly, each site is still using a single Railo instance (each has the full Railo WEB-INF directory in its root). There is, however, a single Tomcat host and this is allowing me to control my box's memory more easily as memory appears to be allocated per host (somebody correct me) - when each of my dozen or so sites was on their own Tomcat host, I was running out of memory fast.
I'll leave it for someone to bridge the gap and figure out how to have a single Railo server config that controls all the individual sites (that still have their own 'web' configs). I don't have a need for that so I'm not going to kill my Sunday...
Dom
0 comments:
Post a Comment