[APACHE DOCUMENTATION]

Apache HTTP Server Version 1.3

An In-Depth Discussion of Virtual Host Matching

The virtual host code was completely rewritten inApache 1.3.This document attempts to explain exactly what Apache does whendeciding what virtual host to serve a hit from. With the help of thenew NameVirtualHostdirective virtual host configuration should be a lot easier and saferthan with versions prior to 1.3.

If you just want to make it work without understandinghow, here are some examples.

Config File Parsing

There is a main_server which consists of allthe definitions appearing outside of <VirtualHost> sections.There are virtual servers, called vhosts, which are defined by<VirtualHost>sections.

The directivesPort,ServerName,ServerPath,andServerAliascan appear anywhere within the definition ofa server. However, each appearance overrides the previous appearance(within that server).

The default value of the Port field for main_serveris 80. The main_server has no default ServerPath, orServerAlias. The default ServerName isdeduced from the servers IP address.

The main_server Port directive has two functions due to legacycompatibility with NCSA configuration files. One function isto determine the default network port Apache will bind to. Thisdefault is overridden by the existence of anyListen directives.The second function is to specify the port number which is usedin absolute URIs during redirects.

Unlike the main_server, vhost ports do not affect whatports Apache listens for connections on.

Each address appearing in the VirtualHost directivecan have an optional port. If the port is unspecified it defaults tothe 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 multipleA recordresults from DNS lookups) are called the vhost's address set.

Unless a NameVirtualHostdirective is used for a specific IP address the first vhost withthat address is treated as an IP-based vhost.

If name-based vhosts should be used a NameVirtualHostdirective must appear with the IP address set to be used for thename-based vhosts. In other words, you must specify the IP address thatholds the hostname aliases (CNAMEs) for your name-based vhosts via aNameVirtualHost directive in your configuration file.

Multiple NameVirtualHost directives can be used eachwith a set of VirtualHost directives but only oneNameVirtualHost directive should be used for eachspecific IP:port pair.

The ordering of NameVirtualHost and VirtualHost directives is not important which makes thefollowing two examples identical (only the order of theVirtualHost directives for one address setis 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 theleft variant.)

After parsing the VirtualHost directive, the vhost serveris given a default Port equal to the port assigned to thefirst name in its VirtualHost directive.

The complete list of names in the VirtualHost directiveare treated just like a ServerAlias (but are not overridden by anyServerAlias statement) if all names resolve to the same addressset. Note that subsequent Port statements for this vhost will notaffect the ports assigned in the address set.

During initialization a list for each IP addressis generated an inserted into an hash table. If the IP address isused in a NameVirtualHost directive the list containsall name-based vhosts for the given IP address. If there are novhosts defined for that address the NameVirtualHost directiveis ignored and an error is logged. For an IP-based vhost the list in thehash table is empty.

Due to a fast hashing function the overhead of hashing an IP addressduring a request is minimal and almost not existent. Additionallythe table is optimized for IP addresses which vary in the last octet.

For every vhost various default values are set. In particular:

  1. If a vhost has no 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.)
  2. The "lookup defaults" that define the default directory permissions for a vhost are merged with those of the main_server. This includes any per-directory configuration information for any module.
  3. The per-server configs for each module from the main_server are merged into the vhost server.
Essentially, the main_server is treated as "defaults" or a"base" on which to build each vhost.But the positioning of these main_serverdefinitions in the config file is largely irrelevant -- the entireconfig of the main_server has been parsed when this final merging occurs.So even if a main_server definition appears after a vhost definitionit might affect the vhost definition.

If the main_server has no ServerName at this point,then the hostname of the machine that httpd is running on is usedinstead. We will call the main_server address set those IPaddresses returned by a DNS lookup on the ServerName ofthe main_server.

For any undefined ServerName fields, a name-based vhostdefaults to the address given first in the VirtualHoststatement defining the vhost.

Any vhost that includes the magic _default_ wildcardis given the same ServerNameaccommodation in Paris as the main_server.

Virtual Host Matching

The server determines which vhost to use for a request as follows:

Hash table lookup

When the connection is first made by a client, the IP address towhich the client connected is looked up in the internal IP hash table.

If the lookup fails (the IP address wasn't found) the request isserved from the _default_ vhost if there is such a vhostfor the port to which the client sent the request. If there is nomatching _default_ vhost the request is served from themain_server.

If the lookup succeeded (a corresponding list for the IP address wasfound) the next step is to decide if we have to deal with an IP-basedor a name-base vhost.

IP-based vhost

If the entry we found has an empty name list then we have found anIP-based vhost, no further actions are performed and the request isserved from that vhost.

Name-based vhost

If the entry corresponds to a name-based vhost the name list containsone or more vhost structures. This list contains the vhosts in the sameorder as the VirtualHost directives appear in the configfile.

The first vhost on this list (the first vhost in the config file withthe specified IP address) has the highest priority and catches any requestto an unknown server name or a request without a Host:header field.

If the client provided a Host: header field the list issearched for a matching vhost and the first hit on a ServerNameor ServerAlias is taken and the request is served fromthat vhost. A Host: header field can contain a port number, butApache always matches against the real port to which the client sentthe request.

If the client submitted a HTTP/1.0 request without Host:header field we don't know to what server the client tried to connect andany existing ServerPath is matched against the URIfrom the request. The first matching path on the list is used and therequest is served from that vhost.

If no matching vhost could be found the request is served from thefirst vhost with a matching port number that is on the list for the IPto which the client connected (as already mentioned before).

Persistent connections

The IP lookup described above is only done once for a particularTCP/IP session while the name lookup is done on every requestduring a KeepAlive/persistent connection. In other words a client mayrequest pages from different name-based vhosts during a singlepersistent connection.

Absolute URI

If the URI from the request is an absolute URI, and its hostname andport match the main server or one of the configured virtual hostsand match the address and port to which the client sent the request,then the scheme/hostname/port prefix is stripped off and the remainingrelative URI is served by the corresponding main server or virtual host.If it does not match, then the URI remains untouched and the request istaken to be a proxy request.

Observations

Tips

In addition to the tips on the DNSIssues page, here are some further tips:


Apache HTTP Server Version 1.3

IndexHome - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |