You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by "Ralf S. Engelschall" <rs...@engelschall.com> on 1998/02/26 17:07:36 UTC
Virtual Hosting w/ mod_rewrite (complete solution?)
In the past we have tried to support mass virtual hosting without
<VirtualHost> sections by using mod_rewrite. We have discovered that we need
UseCanonicalName, etc. Now that we have the functionality we should thing
again about this interesting point and try to discover a complete solution we
can present to the users.
So, I've started from scratch and my current solution looks like the
following. I've still not tested it, but it should be 95% correct. Please fix
me were I'm wrong or where we need to configure more. Please give feedback.
-----------------------------------
##
## vhost.map
##
www.vhost1.dom:80 /path/to/docroot/vhost1
www.vhost2.dom:80 /path/to/docroot/vhost2
:
www.vhostN.dom:80 /path/to/docroot/vhostN
-----------------------------------
-----------------------------------
##
## httpd.conf
##
:
# use the canonical hostname on redirects, etc.
UseCanonicalName
:
# add the virtual host in front of the CLF-format
CustomLog /path/to/access_log "%{VHOST}e %h %l %u %t \"%r\" %>s %b"
:
# enable the rewriting engine in the main server
RewriteEngine on
# define two maps: one for fixing the URL and one which defines
# the available virtual hosts with their corresponding
# DocumentRoot.
RewriteMap lowercase int:tolower
RewriteMap vhost txt:/path/to/vhost.map
# Now do the actual virtual host mapping
# via a huge and complicated single rule:
#
# 1. make sure we don't map for common locations
RewriteCond %{REQUEST_URL} !^/commonurl1/.*
RewriteCond %{REQUEST_URL} !^/commonurl2/.*
:
RewriteCond %{REQUEST_URL} !^/commonurlN/.*
#
# 2. make sure we have a Host header, because
# currently our approach only supports
# virtual hosting through this header
RewriteCond %{HTTP_HOST} !^$
#
# 3. lowercase the hostname
RewriteCond ${lowercase:%{HTTP_HOST}|NONE} ^(.+)$
#
# 4. lookup this hostname in vhost.map and
# remember it only when it is a path
# (and not "NONE" from above)
RewriteCond ${vhost:%1} ^(/.*)$
#
# 5. finally we can map the URL to its docroot location
# and remember the virtual host for logging puposes
RewriteRule ^/(.*)$ %1/$1 [E=VHOST:${lowercase:%{HTTP_HOST}}]
:
-----------------------------------
Ralf S. Engelschall
rse@engelschall.com
www.engelschall.com
Re: Virtual Hosting w/ mod_rewrite (complete solution?)
Posted by Dean Gaudet <dg...@arctic.org>.
On Thu, 26 Feb 1998, Brian Atkins wrote:
> Ralf S. Engelschall wrote:
>
> >
> > -----------------------------------
> > ##
> > ## vhost.map
> > ##
> > www.vhost1.dom:80 /path/to/docroot/vhost1
> > www.vhost2.dom:80 /path/to/docroot/vhost2
> > :
> > www.vhostN.dom:80 /path/to/docroot/vhostN
> > -----------------------------------
>
> We have ours set up to deal with our subdomains instead of
> vhost.com type names (i.e. username.hypermart.net). This
> way we do not need a config file like this that may take
> up a lot of mem in each process (we have over 11k vhosts now
> on one box). It would also be nice to be able to use mod_rewrite
> for our regular vhost.com vhosts as well- perhaps you could
> develop a combo ruleset that would cover both types of vhosts-
> one using a map file and one without?
You can probably do multiple domains without everything in memory too...
aren't there file test operators in mod_rewrite? 'cause you could
just make sure to rewrite domains that had directories on disk.
> > -----------------------------------
> > ##
> > ## httpd.conf
> > ##
> > :
> > # use the canonical hostname on redirects, etc.
> > UseCanonicalName
>
> Is this part of 1.3? We are still running 1.2.4- does this
> accomplish the same thing as the hacks we made to our server
> to use the Host: header when available?
Yeah that's what it does. You should check it out to make sure it does
everything you need. It's "UseCanonicalName on" actually, the default is
off (to preserve backwards compatibility).
Dean
Re: Virtual Hosting w/ mod_rewrite (complete solution?)
Posted by Dean Gaudet <dg...@arctic.org>.
On Thu, 26 Feb 1998, Brian Behlendorf wrote:
> VHostMappedServerRoot /www
Ok I'm convinced.
> > Hey guys make sure that absoluteURI hosts are handled properly... I think
> > they are with UseCanonicalName on. I haven't checked.
>
> Oh, another point - I'd rather leave it up to the core to determine just
> "what" virtualhost the connection is for, and for the module to translate
> that into a directory path, than have the module have to track HTTP evolution.
Yup, that's one of the goals of the patch I just posted. With it, a
module can look at r->hostname (er, get_request_host()) and r->uri,
absoluteURIs are completely dealt with behind the scenes. I just wanted
someone to be aware that it will need testing with mod_vhosterama.
Dean
Re: Virtual Hosting w/ mod_rewrite (complete solution?)
Posted by Brian Behlendorf <br...@organic.com>.
At 07:50 PM 2/26/98 -0800, Dean Gaudet wrote:
>On Thu, 26 Feb 1998, Brian Behlendorf wrote:
>
>> Dean, please don't mention the case of "Host: www". Please? :)
>
>Grumble.
>
>For this setup you want mod_rewrite... because
>
>(a) you'll be reinventing the wheel
How? I think this can be done easily without mod_rewrite, because 1)
there's no need for any sort of "rewriting", logically, and 2) there's no
need for doing any regex work. And just logically, this seems like
something much more like what mod_userdir does than what mod_rewrite does.
>(b) mod_rewrite can rewrite to a default page if none of the hosts match
>the Host: header
ErrorDocument 404?
>Of course you can just do all of this with yet another module. But. But.
>Why?
Speed. Memory. Code simplicity. Config file simplicity.
The great thing about mod_rewrite is it gives you all the configurability
and flexibility of sendmail. The downside to mod_rewrite is that it gives
you all the configuability and flexibility of sendmail.
I love mod_rewrite. There are many things it's enabled me to do, which
would have been very complex to do otherwise. I'm totally happy it's a
part of the core distribution, and that Ralf is such an active contributor.
But along these same lines we should dump mod_userdir.
Let me put it like this - if we could encapsulate all those mod_rewrite
directives into a simple
VHostMappedServerRoot /www
we'd gain a *lot*, even if under the hood it was mod_rewrite.
> Hey guys make sure that absoluteURI hosts are handled properly... I think
> they are with UseCanonicalName on. I haven't checked.
Oh, another point - I'd rather leave it up to the core to determine just
"what" virtualhost the connection is for, and for the module to translate
that into a directory path, than have the module have to track HTTP evolution.
Brian
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
specialization is for insects brian@organic.com
Re: Virtual Hosting w/ mod_rewrite (complete solution?)
Posted by Dean Gaudet <dg...@arctic.org>.
On Thu, 26 Feb 1998, Brian Behlendorf wrote:
> Dean, please don't mention the case of "Host: www". Please? :)
Grumble.
For this setup you want mod_rewrite... because
(a) you'll be reinventing the wheel
(b) mod_rewrite can rewrite to a default page if none of the hosts match
the Host: header
Of course you can just do all of this with yet another module. But. But.
Why?
Hey guys make sure that absoluteURI hosts are handled properly... I think
they are with UseCanonicalName on. I haven't checked.
Dean
Re: Virtual Hosting w/ mod_rewrite (complete solution?)
Posted by Brian Behlendorf <br...@organic.com>.
At 11:52 AM 2/26/98 -0500, Brian Atkins wrote:
>Ralf S. Engelschall wrote:
>> -----------------------------------
>> ##
>> ## vhost.map
>> ##
>> www.vhost1.dom:80 /path/to/docroot/vhost1
>> www.vhost2.dom:80 /path/to/docroot/vhost2
>> :
>> www.vhostN.dom:80 /path/to/docroot/vhostN
>> -----------------------------------
>
>We have ours set up to deal with our subdomains instead of
>vhost.com type names (i.e. username.hypermart.net). This
>way we do not need a config file like this that may take
>up a lot of mem in each process (we have over 11k vhosts now
>on one box).
Agreed. It'd be nice to have something just like mod_userdir that
translates requests for a.b.c into a server root of
/specified/path/port/a.b.c/. That would simplify lots of things.
ServerAliases could simply be done as symlinks. wildcards handled using a
symlink filename of "*.b.c" (hey, Unix can handle it!) :)
The advantage is that a module could be written (perhaps modeled closely
after mod_userdir) to do this without needing to link in mod_rewrite. I
love mod_rewrite, but being able to accomplish this without using it would
be preferable IMHO.
Dean, please don't mention the case of "Host: www". Please? :)
Brian
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
specialization is for insects brian@organic.com
Re: Virtual Hosting w/ mod_rewrite (complete solution?)
Posted by Brian Atkins <br...@posthuman.com>.
Ralf S. Engelschall wrote:
>
> In the past we have tried to support mass virtual hosting without
> <VirtualHost> sections by using mod_rewrite. We have discovered that we need
> UseCanonicalName, etc. Now that we have the functionality we should thing
> again about this interesting point and try to discover a complete solution we
> can present to the users.
>
> So, I've started from scratch and my current solution looks like the
> following. I've still not tested it, but it should be 95% correct. Please fix
> me were I'm wrong or where we need to configure more. Please give feedback.
We have had this going for over a month now here at Hypermart...
We would be very interested in having the ability added into
the normal Apache dist instead of us having to re-port our
patches to each new version.
>
> -----------------------------------
> ##
> ## vhost.map
> ##
> www.vhost1.dom:80 /path/to/docroot/vhost1
> www.vhost2.dom:80 /path/to/docroot/vhost2
> :
> www.vhostN.dom:80 /path/to/docroot/vhostN
> -----------------------------------
We have ours set up to deal with our subdomains instead of
vhost.com type names (i.e. username.hypermart.net). This
way we do not need a config file like this that may take
up a lot of mem in each process (we have over 11k vhosts now
on one box). It would also be nice to be able to use mod_rewrite
for our regular vhost.com vhosts as well- perhaps you could
develop a combo ruleset that would cover both types of vhosts-
one using a map file and one without?
>
> -----------------------------------
> ##
> ## httpd.conf
> ##
> :
> # use the canonical hostname on redirects, etc.
> UseCanonicalName
Is this part of 1.3? We are still running 1.2.4- does this
accomplish the same thing as the hacks we made to our server
to use the Host: header when available?
>
> :
> # add the virtual host in front of the CLF-format
> CustomLog /path/to/access_log "%{VHOST}e %h %l %u %t \"%r\" %>s %b"
> :
>
> # enable the rewriting engine in the main server
> RewriteEngine on
>
> # define two maps: one for fixing the URL and one which defines
> # the available virtual hosts with their corresponding
> # DocumentRoot.
> RewriteMap lowercase int:tolower
> RewriteMap vhost txt:/path/to/vhost.map
Is this map shared between all servers or will it bloat all
of them?
>
> # Now do the actual virtual host mapping
> # via a huge and complicated single rule:
> #
> # 1. make sure we don't map for common locations
> RewriteCond %{REQUEST_URL} !^/commonurl1/.*
> RewriteCond %{REQUEST_URL} !^/commonurl2/.*
> :
> RewriteCond %{REQUEST_URL} !^/commonurlN/.*
> #
> # 2. make sure we have a Host header, because
> # currently our approach only supports
> # virtual hosting through this header
> RewriteCond %{HTTP_HOST} !^$
> #
> # 3. lowercase the hostname
> RewriteCond ${lowercase:%{HTTP_HOST}|NONE} ^(.+)$
> #
> # 4. lookup this hostname in vhost.map and
> # remember it only when it is a path
> # (and not "NONE" from above)
> RewriteCond ${vhost:%1} ^(/.*)$
> #
> # 5. finally we can map the URL to its docroot location
> # and remember the virtual host for logging puposes
> RewriteRule ^/(.*)$ %1/$1 [E=VHOST:${lowercase:%{HTTP_HOST}}]
> :
Yes this is very similiar to what we are using, minus the
map file... The ultimate test of these rules seems to be
MS Frontpage. If these vhosts work with that then they should
work with everything :-)
> -----------------------------------
>
> Ralf S. Engelschall
> rse@engelschall.com
> www.engelschall.com
--
The future has arrived; it's just not evenly distributed.
-William Gibson
______________________________________________________________________
Visit Hypermart at http://www.hypermart.net for free business hosting!
RE: Virtual Hosting w/ mod_rewrite (complete solution?)
Posted by Lars Eilebrecht <La...@unix-ag.org>.
According to Ralf S. Engelschall:
> -----------------------------------
> ##
> ## vhost.map
> ##
> www.vhost1.dom:80 /path/to/docroot/vhost1
> www.vhost2.dom:80 /path/to/docroot/vhost2
> :
> www.vhostN.dom:80 /path/to/docroot/vhostN
> -----------------------------------
Hmm... why do we need a map?
If the "/path/to/docroot" is common for all vhosts
I would simply use the domainname as a subdirectory and rewrite
"www.vhost1.dom" to "/path/to/docroot/vhost/www.vhost1.dom".
Or I'm missing something?
ciao...
--
Lars Eilebrecht - You don't have to be crazy to live
sfx@unix-ag.org - in this planet... but it helps.
http://www.si.unix-ag.org/~sfx/