The virtual host code was completely rewritten in Apache 1.3. This document attempts to explain exactly what Apache does when deciding what virtual host to serve a hit from. With the help of the new NameVirtualHost directive virtual host configuration should be a lot easier and safer than with versions prior to 1.3.
If you just want to make it work without understanding how, here are some examples.
There is a main_server which consists of all
the definitions appearing outside of <VirtualHost>
sections.
There are virtual servers, called vhosts, which are defined by
<VirtualHost>
sections.
The directives Port, ServerName, ServerPath, and ServerAlias can appear anywhere within the definition of a server. However, each appearance overrides the previous appearance (within that server).
The default value of the Port
field for main_server
is 80. The main_server has no default ServerPath
, or
ServerAlias
. The default ServerName
is
deduced from the servers IP address.
The main_server Port directive has two functions due to legacy
compatibility with NCSA configuration files. One function is
to determine the default network port Apache will bind to. This
default is overridden by the existence of any
Listen
directives.
The second function is to specify the port number which is used
in absolute URIs during redirects.
Unlike the main_server, vhost ports do not affect what ports Apache listens for connections on.
Each address appearing in the VirtualHost
directive
can have an optional port. If the port is unspecified it defaults to
the value of the main_server's most recent Port
statement.
The special port * indicates a wildcard that matches any port.
Collectively the entire set of addresses (including multiple
A record
results from DNS lookups) are called the vhost's address set.
Unless a NameVirtualHost directive is used for a specific IP address the first vhost with that address is treated as an IP-based vhost.
If name-based vhosts should be used a NameVirtualHost
directive must appear with the IP address set to be used for the
name-based vhosts. In other words, you must specify the IP address that
holds the hostname aliases (CNAMEs) for your name-based vhosts via a
NameVirtualHost
directive in your configuration file.
Multiple NameVirtualHost
directives can be used each
with a set of VirtualHost
directives but only one
NameVirtualHost
directive should be used for each
specific IP:port pair.
The ordering of NameVirtualHost
and
VirtualHost
directives is not important which makes the
following two examples identical (only the order of the
VirtualHost
directives for one address set
is important, see below):
| NameVirtualHost 111.22.33.44 | <VirtualHost 111.22.33.44> <VirtualHost 111.22.33.44> | # server A # server A | </VirtualHost> ... | <VirtualHost 111.22.33.55> </VirtualHost> | # server C <VirtualHost 111.22.33.44> | ... # server B | </VirtualHost> ... | <VirtualHost 111.22.33.44> </VirtualHost> | # server B | ... NameVirtualHost 111.22.33.55 | </VirtualHost> <VirtualHost 111.22.33.55> | <VirtualHost 111.22.33.55> # server C | # server D ... | ... </VirtualHost> | </VirtualHost> <VirtualHost 111.22.33.55> | # server D | NameVirtualHost 111.22.33.44 ... | NameVirtualHost 111.22.33.55 </VirtualHost> | |
(To aid the readability of your configuration you should prefer the left variant.)
After parsing the VirtualHost
directive, the vhost server
is given a default Port
equal to the port assigned to the
first name in its VirtualHost
directive.
The complete list of names in the VirtualHost
directive
are treated just like a ServerAlias
(but are not overridden by any
ServerAlias
statement) if all names resolve to the same address
set. Note that subsequent Port
statements for this vhost will not
affect the ports assigned in the address set.
During initialization a list for each IP address
is generated an inserted into an hash table. If the IP address is
used in a NameVirtualHost
directive the list contains
all name-based vhosts for the given IP address. If there are no
vhosts defined for that address the NameVirtualHost
directive
is ignored and an error is logged. For an IP-based vhost the list in the
hash table is empty.
Due to a fast hashing function the overhead of hashing an IP address during a request is minimal and almost not existent. Additionally the table is optimized for IP addresses which vary in the last octet.
For every vhost various default values are set. In particular:
ServerAdmin
,
ResourceConfig
,
AccessConfig
,
Timeout
,
KeepAliveTimeout
,
KeepAlive
,
MaxKeepAliveRequests
,
or
SendBufferSize
directive then the respective value is
inherited from the main_server. (That is, inherited from whatever
the final setting of that value is in the main_server.)
If the main_server has no ServerName
at this point,
then the hostname of the machine that httpd is running on is used
instead. We will call the main_server address set those IP
addresses returned by a DNS lookup on the ServerName
of
the main_server.
For any undefined ServerName
fields, a name-based vhost
defaults to the address given first in the VirtualHost
statement defining the vhost.
Any vhost that includes the magic _default_ wildcard
is given the same ServerName
as the main_server.
The server determines which vhost to use for a request as follows:
When the connection is first made by a client, the IP addr