mass.html revision 2eaf662cbc81e823e8d9aeb8d54e69e63032493e
371584c2eae4cf827fd406ba26c14f021adaaa70Yuri Pankov<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov<html xmlns="http://www.w3.org/1999/xhtml">
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <head>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <meta name="generator" content="HTML Tidy, see www.w3.org" />
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <title>Dynamically configured mass virtual hosting</title>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore </head>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore vlink="#000080" alink="#FF0000">
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <!--#include virtual="header.html" -->
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <h1 align="CENTER">Dynamically configured mass virtual
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore hosting</h1>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <p>This document describes how to efficiently serve an
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore arbitrary number of virtual hosts with Apache 1.3. <!--
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore Written by Tony Finch (fanf@demon.net) (dot@dotat.at).
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore Some examples were derived from Ralf S. Engleschall's document
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore http://www.engelschall.com/pw/apache/rewriteguide/
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore Some suggestions were made by Brian Behlendorf.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore -->
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore </p>
698f87a48e2e945bfe5493ce168e0d0ae1cedd5cGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <h2><a id="contents" name="contents">Contents:</a></h2>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <ul>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <li><a href="#motivation">Motivation</a></li>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <li><a href="#overview">Overview</a></li>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <li><a href="#simple">Simple dynamic virtual hosts</a></li>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov <li><a href="#homepages">A virtually hosted homepages
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov system</a></li>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov <li><a href="#combinations">Using more than one virtual
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov hosting system on the same server</a></li>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov <li><a href="#ipbased">More efficient IP-based virtual
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov hosting</a></li>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov <li><a href="#oldversion">Using older versions of
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov Apache</a></li>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov <li><a href="#simple.rewrite">Simple dynamic virtual hosts
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore using <code>mod_rewrite</code></a></li>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <li><a href="#homepages.rewrite">A homepages system using
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <code>mod_rewrite</code></a></li>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <li><a href="#xtra-conf">Using a separate virtual host
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore configuration file</a></li>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore </ul>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <hr />
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <h2><a id="motivation" name="motivation">Motivation</a></h2>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <p>The techniques described here are of interest if your
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore <code>httpd.conf</code> contains many
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov <code>&lt;VirtualHost&gt;</code> sections that are
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov substantially the same, for example:</p>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov<pre>
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri PankovNameVirtualHost 111.22.33.44
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov&lt;VirtualHost 111.22.33.44&gt;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore ServerName www.customer-1.com
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov DocumentRoot /www/hosts/www.customer-1.com/docs
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov ScriptAlias /cgi-bin/ /www/hosts/www.customer-1.com/cgi-bin
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore&lt;/VirtualHost&gt;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore&lt;VirtualHost 111.22.33.44&gt;
260e9a87725c090ba5835b1f9f0b62fa2f96036fYuri Pankov ServerName www.customer-2.com
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore DocumentRoot /www/hosts/www.customer-2.com/docs
ScriptAlias /cgi-bin/ /www/hosts/www.customer-2.com/cgi-bin
&lt;/VirtualHost&gt;
# blah blah blah
&lt;VirtualHost 111.22.33.44&gt;
ServerName www.customer-N.com
DocumentRoot /www/hosts/www.customer-N.com/docs
ScriptAlias /cgi-bin/ /www/hosts/www.customer-N.com/cgi-bin
&lt;/VirtualHost&gt;
</pre>
<br />
<br />
<p>The basic idea is to replace all of the static
<code>&lt;VirtualHost&gt;</code> configuration with a mechanism
that works it out dynamically. This has a number of
advantages:</p>
<ol>
<li>Your configuration file is smaller so Apache starts
faster and uses less memory.</li>
<li>Adding virtual hosts is simply a matter of creating the
appropriate directories in the filesystem and entries in the
DNS - you don't need to reconfigure or restart Apache.</li>
</ol>
<br />
<br />
<p>The main disadvantage is that you cannot have a different
log file for each virtual host; however if you have very many
virtual hosts then doing this is dubious anyway because it eats
file descriptors. It is better to log to a pipe or a fifo and
arrange for the process at the other end to distribute the logs
to the customers (it can also accumulate statistics, etc.).</p>
<hr />
<h2><a id="overview" name="overview">Overview</a></h2>
<p>A virtual host is defined by two pieces of information: its
IP address, and the contents of the <code>Host:</code> header
in the HTTP request. The dynamic mass virtual hosting technique
is based on automatically inserting this information into the
pathname of the file that is used to satisfy the request. This
is done most easily using <a
href="/mod/mod_vhost_alias.html"><code>mod_vhost_alias</code></a>,
but if you are using a version of Apache up to 1.3.6 then you
must use <a
href="/mod/mod_rewrite.html"><code>mod_rewrite</code></a>.
Both of these modules are disabled by default; you must enable
one of them when configuring and building Apache if you want to
use this technique.</p>
<p>A couple of things need to be `faked' to make the dynamic
virtual host look like a normal one. The most important is the
server name which is used by Apache to generate
self-referential URLs, etc. It is configured with the
<code>ServerName</code> directive, and it is available to CGIs
via the <code>SERVER_NAME</code> environment variable. The
actual value used at run time is controlled by the <a
href="/mod/core.html#usecanonicalname"><code>UseCanonicalName</code></a>
setting. With <code>UseCanonicalName Off</code> the server name
comes from the contents of the <code>Host:</code> header in the
request. With <code>UseCanonicalName DNS</code> it comes from a
reverse DNS lookup of the virtual host's IP address. The former
setting is used for name-based dynamic virtual hosting, and the
latter is used for IP-based hosting. If Apache cannot work out
the server name because there is no <code>Host:</code> header
or the DNS lookup fails then the value configured with
<code>ServerName</code> is used instead.</p>
<p>The other thing to `fake' is the document root (configured
with <code>DocumentRoot</code> and available to CGIs via the
<code>DOCUMENT_ROOT</code> environment variable). In a normal
configuration this setting is used by the core module when
mapping URIs to filenames, but when the server is configured to
do dynamic virtual hosting that job is taken over by another
module (either <code>mod_vhost_alias</code> or
<code>mod_rewrite</code>) which has a different way of doing
the mapping. Neither of these modules is responsible for
setting the <code>DOCUMENT_ROOT</code> environment variable so
if any CGIs or SSI documents make use of it they will get a
misleading value.</p>
<hr />
<h2><a id="simple" name="simple">Simple dynamic virtual
hosts</a></h2>
<p>This extract from <code>httpd.conf</code> implements the
virtual host arrangement outlined in the <a
href="#motivation">Motivation</a> section above, but in a
generic fashion using <code>mod_vhost_alias</code>.</p>
<pre>
# get the server name from the Host: header
UseCanonicalName Off
# this log format can be split per-virtual-host based on the first field
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /www/hosts/%0/docs
VirtualScriptAlias /www/hosts/%0/cgi-bin
</pre>
<p>This configuration can be changed into an IP-based virtual
hosting solution by just turning <code>UseCanonicalName
Off</code> into <code>UseCanonicalName DNS</code>. The server
name that is inserted into the filename is then derived from
the IP address of the virtual host.</p>
<hr />
<h2><a id="homepages" name="homepages">A virtually hosted
homepages system</a></h2>
<p>This is an adjustment of the above system tailored for an
ISP's homepages server. Using a slightly more complicated
configuration we can select substrings of the server name to
use in the filename so that e.g. the documents for
<samp>www.user.isp.com</samp> are found in
<code>/home/user/</code>. It uses a single <code>cgi-bin</code>
directory instead of one per virtual host.</p>
<pre>
# all the preliminary stuff is the same as above, then
# include part of the server name in the filenames
VirtualDocumentRoot /www/hosts/%2/docs
# single cgi-bin directory
ScriptAlias /cgi-bin/ /www/std-cgi/
</pre>
<p>There are examples of more complicated
<code>VirtualDocumentRoot</code> settings in <a
href="/mod/mod_vhost_alias.html">the
<code>mod_vhost_alias</code> documentation</a>.</p>
<hr />
<h2><a id="combinations" name="combinations">Using more than
one virtual hosting system on the same server</a></h2>
<p>With more complicated setups you can use Apache's normal
<code>&lt;VirtualHost&gt;</code> directives to control the
scope of the various virtual hosting configurations. For
example, you could have one IP address for homepages customers
and another for commercial customers with the following setup.
This can of course be combined with conventional
<code>&lt;VirtualHost&gt;</code> configuration sections.</p>
<pre>
UseCanonicalName Off
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
&lt;Directory /www/commercial&gt;
Options FollowSymLinks
AllowOverride All
&lt;/Directory&gt;
&lt;Directory /www/homepages&gt;
Options FollowSymLinks
AllowOverride None
&lt;/Directory&gt;
&lt;VirtualHost 111.22.33.44&gt;
ServerName www.commercial.isp.com
CustomLog logs/access_log.commercial vcommon
VirtualDocumentRoot /www/commercial/%0/docs
VirtualScriptAlias /www/commercial/%0/cgi-bin
&lt;/VirtualHost&gt;
&lt;VirtualHost 111.22.33.45&gt;
ServerName www.homepages.isp.com
CustomLog logs/access_log.homepages vcommon
VirtualDocumentRoot /www/homepages/%0/docs
ScriptAlias /cgi-bin/ /www/std-cgi/
&lt;/VirtualHost&gt;
</pre>
<hr />
<h2><a id="ipbased" name="ipbased">More efficient IP-based
virtual hosting</a></h2>
<p>After <a href="#simple">the first example</a> I noted that
it is easy to turn it into an IP-based virtual hosting setup.
Unfortunately that configuration is not very efficient because
it requires a DNS lookup for every request. This can be avoided
by laying out the filesystem according to the IP addresses
themselves rather than the corresponding names and changing the
logging similarly. Apache will then usually not need to work
out the server name and so incur a DNS lookup.</p>
<pre>
# get the server name from the reverse DNS of the IP address
UseCanonicalName DNS
# include the IP address in the logs so they may be split
LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
# include the IP address in the filenames
VirtualDocumentRootIP /www/hosts/%0/docs
VirtualScriptAliasIP /www/hosts/%0/cgi-bin
</pre>
<hr />
<h2><a id="oldversion" name="oldversion">Using older versions
of Apache</a></h2>
<p>The examples above rely on <code>mod_vhost_alias</code>
which appeared after version 1.3.6. If you are using a version
of Apache without <code>mod_vhost_alias</code> then you can
implement this technique with <code>mod_rewrite</code> as
illustrated below, but only for Host:-header-based virtual
hosts.</p>
<p>In addition there are some things to beware of with logging.
Apache 1.3.6 is the first version to include the
<code>%V</code> log format directive; in versions 1.3.0 - 1.3.3
the <code>%v</code> option did what <code>%V</code> does;
version 1.3.4 has no equivalent. In all these versions of
Apache the <code>UseCanonicalName</code> directive can appear
in <code>.htaccess</code> files which means that customers can
cause the wrong thing to be logged. Therefore the best thing to
do is use the <code>%{Host}i</code> directive which logs the
<code>Host:</code> header directly; note that this may include
<code>:port</code> on the end which is not the case for
<code>%V</code>.</p>
<hr />
<h2><a id="simple.rewrite" name="simple.rewrite">Simple dynamic
virtual hosts using <code>mod_rewrite</code></a></h2>
<p>This extract from <code>httpd.conf</code> does the same
thing as <a href="#simple">the first example</a>. The first
half is very similar to the corresponding part above but with
some changes for backward compatibility and to make the
<code>mod_rewrite</code> part work properly; the second half
configures <code>mod_rewrite</code> to do the actual work.</p>
<p>There are a couple of especially tricky bits: By default,
<code>mod_rewrite</code> runs before the other URI translation
modules (<code>mod_alias</code> etc.) so if they are used then
<code>mod_rewrite</code> must be configured to accommodate
them. Also, mome magic must be performed to do a
per-dynamic-virtual-host equivalent of
<code>ScriptAlias</code>.</p>
<pre>
# get the server name from the Host: header
UseCanonicalName Off
# splittable logs
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
&lt;Directory /www/hosts&gt;
# ExecCGI is needed here because we can't force
# CGI execution in the way that ScriptAlias does
Options FollowSymLinks ExecCGI
&lt;/Directory&gt;
# now for the hard bit
RewriteEngine On
# a ServerName derived from a Host: header may be any case at all
RewriteMap lowercase int:tolower
## deal with normal documents first:
# allow Alias /icons/ to work - repeat for other aliases
RewriteCond %{REQUEST_URI} !^/icons/
# allow CGIs to work
RewriteCond %{REQUEST_URI} !^/cgi-bin/
# do the magic
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1
## and now deal with CGIs - we have to force a MIME type
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [T=application/x-httpd-cgi]
# that's it!
</pre>
<hr />
<h2><a id="homepages.rewrite" name="homepages.rewrite">A
homepages system using <code>mod_rewrite</code></a></h2>
<p>This does the same thing as <a href="#homepages">the second
example</a>.</p>
<pre>
RewriteEngine on
RewriteMap lowercase int:tolower
# allow CGIs to work
RewriteCond %{REQUEST_URI} !^/cgi-bin/
# check the hostname is right so that the RewriteRule works
RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.isp\.com$
# concatenate the virtual host name onto the start of the URI
# the [C] means do the next rewrite on the result of this one
RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
# now create the real file name
RewriteRule ^www\.([a-z-]+)\.isp\.com/(.*) /home/$1/$2
# define the global CGI directory
ScriptAlias /cgi-bin/ /www/std-cgi/
</pre>
<hr />
<h2><a id="xtra-conf" name="xtra-conf">Using a separate virtual
host configuration file</a></h2>
<p>This arrangement uses more advanced <code>mod_rewrite</code>
features to get the translation from virtual host to document
root from a separate configuration file. This provides more
flexibility but requires more complicated configuration.</p>
<p>The <code>vhost.map</code> file contains something like
this:</p>
<pre>
www.customer-1.com /www/customers/1
www.customer-2.com /www/customers/2
# ...
www.customer-N.com /www/customers/N
</pre>
<br />
<br />
<p>The <code>http.conf</code> contains this:</p>
<pre>
RewriteEngine on
RewriteMap lowercase int:tolower
# define the map file
RewriteMap vhost txt:/www/conf/vhost.map
# deal with aliases as above
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
# this does the file-based remap
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/docs/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/cgi-bin/$1
</pre>
<br />
<br />
<!--#include virtual="footer.html" -->
</body>
</html>