[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Apache 1.3 mass virtual hosting recipe



Hi,

I finally got around to trying out mod_rewrite and it looks like things will 
work exactly as I'd hoped.  I'm posting this recipe since I was unable to 
find a documented example of how to do it and I figured lots of people might 
be interested in doing it.  It does need some more testing but at this point 
I see no reason why it will not work.

The basic concept is to create a directory and have apache serve it 
automatically under the correct domain name (zero config apache). Let's say I 
want to have wehave.net and www.wehave.net hosted on my server.  I could 
create a VirtualHost with "ServerName wehave.net" and "ServerAlias 
www.wehave.net" or I could do this:

  RewriteEngine on
  RewriteLog /tmp/junk
  RewriteLogLevel 9
  RewriteMap      vhosts-map      prg:/usr/local/sbin/vhost-redirector.pl
  RewriteRule     ^/(.*)$ ${vhosts-map:%{HTTP_HOST}}$1

My perl script looks like this:

  #!/usr/bin/perl -wT
  use strict;
  $| = 1;
  while (<>) {
    # FIXME, do we need to do syntax checking on hostname?
    chomp;
    my @path = split(/\./, $_);
    while ( ! -d '/var/www/' . join('/', reverse(@path)) . '/web/' and @path){
      shift @path;
    }
    if (@path) {
      print '/var/www/' . join('/', reverse(@path)) . "/web/\n";
    } else {
      print "/var/www/web/\n";
    }
  }

The end result is that once dns points something at your server simply 
creating the appropriate directory heirarchy is enough to host a given 
website (and aliases).  Given the previous example (hosting wehave.net), I 
would create the directory /var/www/net/wehave/web and 
http://wehave.net/whatever would translate to the filesystem 
as /var/www/net/wehave/web/whatever ... requests for 
http://www.wehave.net/whatever would translate to the same file unless I 
wanted to serve something different for that in which case I would just need 
to create /var/www/net/wehave/www/web/whatever 

If I want www.wehave.ca to be the same as wehave.net then I just link ...
ln -s /var/www/net/wehave /var/www/ca/wehave

Any site pointed to your server which doesn't have an appropriate config would 
end up directed to the filesystem directory /var/www/web ... there could be a 
default page there stating "Site not configured".

One thing I haven't shown in my example is that you would have to either not 
rewrite shared paths (such as /icons/, /squirrelmail/, etc.) or you would 
have to hardcode those paths into the perl script.

The perl code starts when apache starts and stays resident so the extra 
overhead is minimal (I am guessing) i.e. you are not firing up a perl process 
for every single request.

Obviously under this scheme all hostnames get treated the same, you would have 
to override the defaults in some cases (SSL sites for example) but that can 
be done by adding normal rules in <virtualhost> or <Directory> containers.

On September 5, 2004 09:06 pm, Fraser Campbell wrote:
> Hi,
>
> I'm setting up a new server and would like to use mod_vhost_alias, or other
> mass virtual hosting method, if possible.  mod_vhost_alias is very simple
> to setup and works as advertised:
>
>     LoadModule vhost_alias_module /usr/lib/apache/1.3/mod_vhost_alias.so
>     UseCanonicalName Off
>     VirtualDocumentRoot /var/www/%0/web
>
> Unfortunately, it is as simple as it is simple to setup ;-)  My question is
> in regards to ServerAlias.  "Normal" clients host their website at
> http://www.abcd.com/ but they (and I) also like it when http://abcd.com/
> works as well.  Is there any way (besides filesystem link) to make
> vhost_alias find the right DocumentRoot?
>
> Ideally I'd like a directory structure like this:
>
>     /var/www/com/abcd/web/
>     /var/www/com/abcd/subdomain/web/
>
> When serving http://www.abcd.com/ apache wouldn't
> find /var/www/com/abcd/www/web/ so it would fallback
> to /var/www/com/abcd/web/, if that were missing it would fallback
> to /var/www/com/web/ and then to /var/www/web/
>
> Perhaps something like the above is possible using mod_rewrite???  I'll
> keep plugging away for a while but if anyone knows the answer I'm all for
> shortcuts.
>
> Second question.  Supposing that the above can work somehow I'd like to use
> one common logfile for all virtualhosts, with the virtualhost's name
> prepended to each log line.  This is easy of course.  The catch of course
> is that I'd like to have consistent names for hosts, i.e. I don't want to
> be splitting off logfiles for www.abcd.com and abcd.com when those are
> actually the same site.
>
> Basically, I'd like apache logging to be smart enough to realize that it is
> actually serving abcd.com although the hostname in the request was
> www.abcd.com.
>
> --
> Fraser Campbell <fraser@wehave.net>                 http://www.wehave.net/
> Georgetown, Ontario, Canada                               Debian GNU/Linux



Reply to: