This document describes how to efficiently serve an arbitrary number of virtual hosts with Apache.
The techniques described here are of interest if your
httpd.conf contains many
<VirtualHost> sections that are
substantially the same, for example:
The basic idea is to replace all of the static
<VirtualHost> configuration with a mechanism
that works it out dynamically. This has a number of
advantages:
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.).
A virtual host is defined by two pieces of information: its
IP address, and the contents of the Host: 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 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
ServerName directive, and it is available to CGIs
via the SERVER_NAME environment variable. The
actual value used at run time is controlled by the UseCanonicalName Off the server name
comes from the contents of the Host: header in the
request. With UseCanonicalName DNS 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 Host: header
or the DNS lookup fails then the value configured with
ServerName is used instead.
The other thing to `fake' is the document root (configured
with DocumentRoot and available to CGIs via the
DOCUMENT_ROOT 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 mod_vhost_alias or
mod_rewrite) which has a different way of doing
the mapping. Neither of these modules is responsible for
setting the DOCUMENT_ROOT environment variable so
if any CGIs or SSI documents make use of it they will get a
misleading value.
This extract from httpd.conf implements the
virtual host arrangement outlined in the Motivation section above, but in a
generic fashion using mod_vhost_alias.
This configuration can be changed into an IP-based virtual
hosting solution by just turning UseCanonicalName
Off into UseCanonicalName DNS. The server
name that is inserted into the filename is then derived from
the IP address of the virtual host.
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
www.user.isp.com are found in
/home/user/. It uses a single cgi-bin
directory instead of one per virtual host.
There are examples of more complicated
VirtualDocumentRoot settings in the
With more complicated setups you can use Apache's normal
<VirtualHost> 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
<VirtualHost> configuration sections.
If the first VirtualHost block does not include a
ServerName
none.example.com) can be added to get around this
behaviour.
After the first example 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.
mod_rewriteThis extract from httpd.conf does the same
thing as the first example. The first
half is very similar to the corresponding part above but with
some changes for backward compatibility and to make the
mod_rewrite part work properly; the second half
configures mod_rewrite to do the actual work.
There are a couple of especially tricky bits: By default,
mod_rewrite runs before the other URI translation
modules (mod_alias etc.) so if they are used then
mod_rewrite must be configured to accommodate
them. Also, some magic must be performed to do a
per-dynamic-virtual-host equivalent of
ScriptAlias.
mod_rewriteThis does the same thing as the second example.
This arrangement uses more advanced mod_rewrite
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.
The vhost.map file contains something like
this:
The http.conf contains this: