Dynamic hosts and Apache/Tomcat

351 views
Skip to first unread message

Sean Corfield

unread,
Sep 19, 2010, 10:53:09 PM9/19/10
to ra...@googlegroups.com
I spent some time experimenting with Tomcat / Apache today to see if I
could simplify the process of adding new hosts to an Apache / Tomcat
setup. If you're willing to accept a couple of compromises, yes you
can!

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

denstar

unread,
Sep 20, 2010, 1:21:32 AM9/20/10
to ra...@googlegroups.com
This is beautiful Sean, good work- and on a weekend y todo!

:Den

--
To love to read is to exchange hours of ennui for hours of delight.
Charles de Montesquieu

Sean Corfield

unread,
Sep 20, 2010, 1:38:29 AM9/20/10
to ra...@googlegroups.com
On Sun, Sep 19, 2010 at 10:21 PM, denstar <vallia...@gmail.com> wrote:
> This is beautiful Sean, good work- and on a weekend y todo!

I have a day job - when else am I going to spend time messing with
Tomcat for fun? :)

Martijn van der Woud

unread,
Sep 20, 2010, 1:41:54 AM9/20/10
to Railo
This sounds good! Sean can I add new host without having to restart
Tomcat if I use this approach?



On 20 sep, 04:53, Sean Corfield <seancorfi...@gmail.com> wrote:
> I spent some time experimenting with Tomcat / Apache today to see if I
> could simplify the process of adding new hosts to an Apache / Tomcat
> setup. If you're willing to accept a couple of compromises, yes you
> can!
>
> 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 forhttp://mydomain.com/whatever/page-or-assetto Tomcat as
> Railo Technologies, Inc. --http://getrailo.com/
> An Architect's View --http://corfield.org/

Sean Corfield

unread,
Sep 20, 2010, 2:30:16 AM9/20/10
to ra...@googlegroups.com
On Sun, Sep 19, 2010 at 10:41 PM, Martijn van der Woud
<martijnv...@gmail.com> wrote:
> This sounds good! Sean can I add new host without having to restart
> Tomcat if I use this approach?

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/

denstar

unread,
Sep 20, 2010, 5:00:59 AM9/20/10
to ra...@googlegroups.com
On Sun, Sep 19, 2010 at 11:38 PM, Sean Corfield wrote:

> On Sun, Sep 19, 2010 at 10:21 PM, denstar wrote:
>> This is beautiful Sean, good work- and on a weekend y todo!
>
> I have a day job - when else am I going to spend time messing with
> Tomcat for fun? :)

Nerd! :)

:Den

--
Useless laws weaken the necessary laws.
Charles de Montesquieu

Gert Franz

unread,
Sep 20, 2010, 5:18:04 AM9/20/10
to ra...@googlegroups.com
Some remarks,

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

Judah McAuley

unread,
Sep 20, 2010, 11:56:05 AM9/20/10
to ra...@googlegroups.com
Great stuff Sean, does this require Tomcat 7? Tomcat 6?

Sean Corfield

unread,
Sep 20, 2010, 2:35:05 PM9/20/10
to ra...@googlegroups.com
It works on Tomcat 6.

Sean Corfield

unread,
Sep 20, 2010, 2:43:10 PM9/20/10
to ra...@googlegroups.com
On Mon, Sep 20, 2010 at 2:18 AM, Gert Franz <ge...@getrailo.com> wrote:
> this only works if you only have one host in the server.xml.

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>.

Reply all
Reply to author
Forward
0 new messages