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
There are virtual servers, called vhosts, which are defined by
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
ServerAlias. The default
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
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
can have an optional port. If the port is unspecified it defaults to
the value of the main_server's most recent
The special port * indicates a wildcard that matches any port.
Collectively the entire set of addresses (including multiple
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
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.
NameVirtualHost directives can be used each
with a set of
The ordering of
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 126.96.36.199 | <VirtualHost 188.8.131.52> <VirtualHost 184.108.40.206> | # server A # server A | </VirtualHost> ... | <VirtualHost 220.127.116.11> </VirtualHost> | # server C <VirtualHost 18.104.22.168> | ... # server B | </VirtualHost> ... | <VirtualHost 22.214.171.124> </VirtualHost> | # server B | ... NameVirtualHost 126.96.36.199 | </VirtualHost> <VirtualHost 188.8.131.52> | <VirtualHost 184.108.40.206> # server C | # server D ... | ... </VirtualHost> | </VirtualHost> <VirtualHost 220.127.116.11> | # server D | NameVirtualHost 18.104.22.168 ... | NameVirtualHost 22.214.171.124 </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
The complete list of names in the
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
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:
SendBufferSizedirective 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
For any undefined
ServerName fields, a name-based vhost
defaults to the address given first in the
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 address to which the client connected is looked up in the internal IP hash table.
If the lookup fails (the IP address wasn't found) the request is served from the _default_ vhost if there is such a vhost for the port to which the client sent the request. If there is no matching _default_ vhost the request is served from the main_server.
If the lookup succeeded (a corresponding list for the IP address was found) the next step is to decide if we have to deal with an IP-based or a name-base vhost.
If the entry we found has an empty name list then we have found an IP-based vhost, no further actions are performed and the request is served from that vhost.
If the entry corresponds to a name-based vhost the name list contains
one or more vhost structures. This list contains the vhosts in the same
order as the
VirtualHost directives appear in the config
The first vhost on this list (the first vhost that appears after the
NameVirtualHost directive in the config file)
has the highest priority and catches any request to an unknown
server name or a request without a
If the client provided a
Host: header the list is
searched for a matching vhost and the first hit on a
ServerAlias is taken and the request is served from
that vhost. A
Host: header can contain a port number, but
Apache always matches against the real port to which the client sent
If the client submitted a HTTP/1.0 request without
header we don't know to what server the client tried to connect and
ServerPath is matched against the URI
from the request. The first matching path on the list is used and the
request is served from that vhost.
If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected (as already mentioned before).
If the URI from the request is an absolute URI, and its hostname and port match the main server or one of the configured virtual hosts and match the address and port to which the client sent the request, then the scheme/hostname/port prefix is stripped off and the remaining relative URI is served by the corresponding main server or virtual host. If it does not match, then the URI remains untouched and the request is taken to be a proxy request.
ServerPathchecks are never performed for an IP-based vhost.
NameVirtualHostdirective within the config file is not important. Only the ordering of name-based vhosts for a specific address set is significant. The one name-based vhosts that comes first in the configuration file has the highest priority for its corresponding address set.
Host:header is never used during the matching process. Apache always uses the real port to which the client sent the request.
ServerPathdirective exists which is a prefix of another
ServerPathdirective that appears later in the configuration file, then the former will always be matched and the latter will never be matched. (That is assuming that no Host header was available to disambiguate the two.)
_default_vhost catches a request only if there is no other vhost with a matching IP address and a matching port number for the request. The request is only catched if the port number to which the client sent the request matches the port number of your
_default_vhost which is your standard
Portby default. A wildcard port can be specified (i.e.
_default_:*) to catch requests to any available port.
_default_vhost). In other words the main_server only catches a request for an unspecified address/port combination (unless there is a
_default_vhost which matches that port).
_default_vhost or the main_server is never matched for a request with an unknown or missing
Host:header if the client connected to an address (and port) which is used for name-based vhosts, e.g. in a
VirtualHostdirectives because it will force your server to rely on DNS to boot. Furthermore it poses a security threat if you do not control the DNS for all the domains listed. There's more information available on this and the next two topics.
ServerNameshould always be set for each vhost. Otherwise A DNS lookup is required for each vhost.
In addition to the tips on the DNS Issues page, here are some further tips:
VirtualHostdefinitions. (This is to aid the readability of the configuration -- the post-config merging process makes it non-obvious that definitions mixed in around virtual hosts might affect all virtual hosts.)
VirtualHostdefinitions in your configuration to ensure better readability.
ServerPathswhich are prefixes of other
ServerPaths. If you cannot avoid this then you have to ensure that the longer (more specific) prefix vhost appears earlier in the configuration file than the shorter (less specific) prefix (i.e., "ServerPath /abc" should appear after "ServerPath /abc/def").