The key is twofold:
* RewriteRule with %{HTTP_HOST} in Apache
* Per-host Context configuration in Tomcat
The simplest Apache configuration that will work:
ProxyPreserveHost On
RewriteEngine On
RewriteRule ^(.*)$ ajp://localhost:8009/%{HTTP_HOST}$1 [P,L]
That proxies every request for
http://mydomain.com/whatever/page-or-asset to Tomcat as
/mydomain.com/whatever/page-or-asset
You don't need any VirtualHost directives in Apache for that to work.
In Tomcat's server.xml, compress the localhost <Host> entry to just this:
<Host name="localhost"/>
You don't need any other hosts. We're going to use one web context per domain.
In Tomcat's conf/ folder add a Catalina/ folder and inside that add a
localhost/ folder.
Now we will add a context descriptor file for each domain you want to support.
Create a file called: mydomain.com.xml
Containing:
<Context docBase="/path/to/my/webroot"/>
That's it!
Some caveats:
* If you create the context XML file before you create the web root,
you'll need to 'touch' the XML file to get Tomcat to re-read it (and
deploy the app)
* If you want to handle www. and non-www. domains identically, you
need a smarter RewriteRule
* If you want Apache to handle non-CFML requests, you'll need
something more complicated - read on...
To have Apache handle non-CFML requests, it needs a document root. You
can't have a dynamic webroot per se but if you are prepared to
structure your projects so that you have something like:
/var/
www/
html/
domain1.com/
domain2.com/
To match the Tomcat contexts:
{tomcat}/
conf/
Catalina/
localhost/
domain1.com.xml
domain2.com.xml
Then you can define /var/www/html to be your DocumentRoot in Apache:
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
And then you can rewrite non-CFML requests:
RewriteRule ^(.*\.)(html?|jpe?g|gif|xml|txt)$ /%{HTTP_HOST}$1$2 [L]
So, in summary, you create you per-domain webroots under some common
folder (if you want Apache to serve content - otherwise they can be
anywhere), you create per-domain Context XML files in the
{tomcat}/conf/Catalina/localhost/ folder. You don't need VirtualHosts
in Apache, you don't need Host definitions in Tomcat.
I suspect it'll need a bit more testing and refinement, but the basis
of that seems to work just fine for me.
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/
"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood
:Den
--
To love to read is to exchange hours of ennui for hours of delight.
Charles de Montesquieu
I have a day job - when else am I going to spend time messing with
Tomcat for fun? :)
Correct. If you follow this approach, you can just create your new
webroot and folders, then create the new context XML file and Tomcat
automatically loads it within a few seconds.
--
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/
Nerd! :)
:Den
--
Useless laws weaken the necessary laws.
Charles de Montesquieu
this only works if you only have one host in the server.xml. Great job so
far Sean, now what would of course be superb is if we could do this per
virtual host. And without the rewrite rule. But for now this is really
great. It was one of the obstacles for Tomcat with some clients.
Thanks again Sean.
Greetings from Switzerland
Gert Franz
Railo Technologies Professional Open Source
skype: gert.franz ge...@getrailo.com
+41 76 5680 231 www.getrailo.com
-----Ursprüngliche Nachricht-----
Von: ra...@googlegroups.com [mailto:ra...@googlegroups.com] Im Auftrag von
denstar
Gesendet: Montag, 20. September 2010 11:01
An: ra...@googlegroups.com
Betreff: Re: [railo] Dynamic hosts and Apache/Tomcat
Since everything is handled via contexts - and host headers are
preserved - only one <Host> is needed for this.
You can have other <Host>s in server.xml but they will then be handled
as specified by their nested <Context>. The default <Host> handles any
requests for hostnames not explicitly declared elsewhere. Not sure I'd
recommend mixing two styles tho'. The same applies to Apache
<VirtualHost>s - see below.
> now what would of course be superb is if we could do this per
> virtual host. And without the rewrite rule. But for now this is really
> great. It was one of the obstacles for Tomcat with some clients.
Well, the RewriteRule is generic. You can use <VirtualHost>s in Apache
if you want to specify different webroots or different RewriteRules
but I was trying to keep it all as simple as possible:
* one (default) <Host> in server.xml
* no <VirtualHost> needed in Apache
* simple, generic rule to pass things to Tomcat
If you do add host-specific <VirtualHost>s, you can still have a
catch-all <VirtualHost> (that has no ServerName specified) and it will
handle all requests not explicitly handled by any other <VirtualHost>.