details.html revision f0bf3abb8ede7148b583a94f65cd149c2e1eb5e2
335N/A<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
0N/A "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
0N/A
0N/A<html xmlns="http://www.w3.org/1999/xhtml">
335N/A <head>
0N/A <meta name="generator" content="HTML Tidy, see www.w3.org" />
0N/A
0N/A <title>An In-Depth Discussion of Virtual Host Matching</title>
0N/A </head>
0N/A <!-- Background white, links blue (unvisited), navy (visited), red (active) -->
0N/A
0N/A <body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
0N/A vlink="#000080" alink="#FF0000">
0N/A <!--#include virtual="header.html" -->
0N/A
0N/A <h1 align="CENTER">An In-Depth Discussion of Virtual Host
0N/A Matching</h1>
0N/A
0N/A <p>The virtual host code was completely rewritten in
0N/A <strong>Apache 1.3</strong>. This document attempts to explain
0N/A exactly what Apache does when deciding what virtual host to
0N/A serve a hit from. With the help of the new <a
0N/A href="/mod/core.html#namevirtualhost"><samp>NameVirtualHost</samp></a>
0N/A directive virtual host configuration should be a lot easier and
0N/A safer than with versions prior to 1.3.</p>
335N/A
477N/A <p>If you just want to <cite>make it work</cite> without
477N/A understanding how, here are <a href="examples.html">some
0N/A examples</a>.</p>
123N/A
0N/A <h3>Config File Parsing</h3>
0N/A
0N/A <p>There is a <em>main_server</em> which consists of all the
0N/A definitions appearing outside of
0N/A <code>&lt;VirtualHost&gt;</code> sections. There are virtual
0N/A servers, called <em>vhosts</em>, which are defined by <a
0N/A href="/mod/core.html#virtualhost"><samp>&lt;VirtualHost&gt;</samp></a>
0N/A sections.</p>
335N/A
335N/A <p>The directives <a
335N/A href="/mod/mpm_common.html#listen"><samp>Listen</samp></a>, <a
335N/A href="/mod/core.html#servername"><samp>ServerName</samp></a>,
335N/A <a
0N/A href="/mod/core.html#serverpath"><samp>ServerPath</samp></a>,
0N/A and <a
0N/A href="/mod/core.html#serveralias"><samp>ServerAlias</samp></a>
0N/A can appear anywhere within the definition of a server. However,
335N/A each appearance overrides the previous appearance (within that
123N/A server).</p>
123N/A
335N/A <p>The default value of the <code>Listen</code> field for
123N/A main_server is 80. The main_server has no default
123N/A <code>ServerPath</code>, or <code>ServerAlias</code>. The
335N/A default <code>ServerName</code> is deduced from the servers IP
123N/A address.</p>
123N/A
123N/A <p>The main_server Listen directive has two functions. One
123N/A function is to determine the default network port Apache will
123N/A bind to. The second function is to specify the port number
123N/A which is used in absolute URIs during redirects.</p>
123N/A
123N/A <p>Unlike the main_server, vhost ports <em>do not</em> affect
123N/A what ports Apache listens for connections on.</p>
123N/A
242N/A <p>Each address appearing in the <code>VirtualHost</code>
123N/A directive can have an optional port. If the port is unspecified
123N/A it defaults to the value of the main_server's most recent
123N/A <code>Listen</code> statement. The special port <samp>*</samp>
123N/A indicates a wildcard that matches any port. Collectively the
123N/A entire set of addresses (including multiple <samp>A</samp>
123N/A record results from DNS lookups) are called the vhost's
123N/A <em>address set</em>.</p>
123N/A
123N/A <p>Unless a <a
242N/A href="/mod/core.html#namevirtualhost">NameVirtualHost</a>
123N/A directive is used for a specific IP address the first vhost
123N/A with that address is treated as an IP-based vhost. The IP
242N/A address can also be the wildcard <code>*</code>.</p>
123N/A
335N/A <p>If name-based vhosts should be used a
242N/A <code>NameVirtualHost</code> directive <em>must</em> appear
242N/A with the IP address set to be used for the name-based vhosts.
123N/A In other words, you must specify the IP address that holds the
123N/A hostname aliases (CNAMEs) for your name-based vhosts via a
123N/A <code>NameVirtualHost</code> directive in your configuration
123N/A file.</p>
335N/A
123N/A <p>Multiple <code>NameVirtualHost</code> directives can be used
47N/A each with a set of <code>VirtualHost</code> directives but only
0N/A one <code>NameVirtualHost</code> directive should be used for
0N/A each specific IP:port pair.</p>
0N/A
470N/A <p>The ordering of <code>NameVirtualHost</code> and
0N/A <code>VirtualHost</code> directives is not important which
58N/A makes the following two examples identical (only the order of
477N/A the <code>VirtualHost</code> directives for <em>one</em>
0N/A address set is important, see below):</p>
470N/A<pre>
477N/A |
477N/A NameVirtualHost 111.22.33.44 | &lt;VirtualHost 111.22.33.44&gt;
477N/A &lt;VirtualHost 111.22.33.44&gt; | # server A
477N/A # server A | &lt;/VirtualHost&gt;
477N/A ... | &lt;VirtualHost 111.22.33.55&gt;
477N/A &lt;/VirtualHost&gt; | # server C
477N/A &lt;VirtualHost 111.22.33.44&gt; | ...
477N/A # server B | &lt;/VirtualHost&gt;
477N/A ... | &lt;VirtualHost 111.22.33.44&gt;
477N/A &lt;/VirtualHost&gt; | # server B
477N/A | ...
477N/A NameVirtualHost 111.22.33.55 | &lt;/VirtualHost&gt;
477N/A &lt;VirtualHost 111.22.33.55&gt; | &lt;VirtualHost 111.22.33.55&gt;
477N/A # server C | # server D
477N/A ... | ...
477N/A &lt;/VirtualHost&gt; | &lt;/VirtualHost&gt;
477N/A &lt;VirtualHost 111.22.33.55&gt; |
477N/A # server D | NameVirtualHost 111.22.33.44
477N/A ... | NameVirtualHost 111.22.33.55
477N/A &lt;/VirtualHost&gt; |
477N/A |
477N/A</pre>
477N/A
477N/A <p>(To aid the readability of your configuration you should
477N/A prefer the left variant.)</p>
335N/A
477N/A <p>After parsing the <code>VirtualHost</code> directive, the
477N/A vhost server is given a default <code>Listen</code> equal to the
477N/A port assigned to the first name in its <code>VirtualHost</code>
0N/A directive.</p>
477N/A
0N/A <p>The complete list of names in the <code>VirtualHost</code>
0N/A directive are treated just like a <code>ServerAlias</code> (but
0N/A are not overridden by any <code>ServerAlias</code> statement)
0N/A if all names resolve to the same address set. Note that
0N/A subsequent <code>Listen</code> statements for this vhost will not
335N/A affect the ports assigned in the address set.</p>
335N/A
335N/A <p>During initialization a list for each IP address is
283N/A generated and inserted into an hash table. If the IP address is
202N/A used in a <code>NameVirtualHost</code> directive the list
0N/A contains all name-based vhosts for the given IP address. If
0N/A there are no vhosts defined for that address the
0N/A <code>NameVirtualHost</code> directive is ignored and an error
335N/A is logged. For an IP-based vhost the list in the hash table is
0N/A empty.</p>
0N/A
0N/A <p>Due to a fast hashing function the overhead of hashing an IP
27N/A address during a request is minimal and almost not existent.
0N/A Additionally the table is optimized for IP addresses which vary
335N/A in the last octet.</p>
0N/A
0N/A <p>For every vhost various default values are set. In
0N/A particular:</p>
0N/A
0N/A <ol>
0N/A <li>If a vhost has no <a
335N/A href="/mod/core.html#serveradmin"><code>ServerAdmin</code></a>,
0N/A <a
335N/A href="/mod/core.html#resourceconfig"><code>ResourceConfig</code></a>,
335N/A <a
335N/A href="/mod/core.html#accessconfig"><code>AccessConfig</code></a>,
335N/A <a href="/mod/core.html#timeout"><code>Timeout</code></a>,
335N/A <a
335N/A href="/mod/core.html#keepalivetimeout"><code>KeepAliveTimeout</code></a>,
335N/A <a
335N/A href="/mod/core.html#keepalive"><code>KeepAlive</code></a>,
335N/A <a
335N/A href="/mod/core.html#maxkeepaliverequests"><code>MaxKeepAliveRequests</code></a>,
335N/A or <a
335N/A href="/mod/core.html#sendbuffersize"><code>SendBufferSize</code></a>
335N/A directive then the respective value is inherited from the
335N/A main_server. (That is, inherited from whatever the final
335N/A setting of that value is in the main_server.)</li>
335N/A
0N/A <li>The "lookup defaults" that define the default directory
0N/A permissions for a vhost are merged with those of the
335N/A main_server. This includes any per-directory configuration
0N/A information for any module.</li>
335N/A
335N/A <li>The per-server configs for each module from the
0N/A main_server are merged into the vhost server.</li>
335N/A </ol>
0N/A Essentially, the main_server is treated as "defaults" or a
0N/A "base" on which to build each vhost. But the positioning of
335N/A these main_server definitions in the config file is largely
0N/A irrelevant -- the entire config of the main_server has been
335N/A parsed when this final merging occurs. So even if a main_server
0N/A definition appears after a vhost definition it might affect the
0N/A vhost definition.
0N/A
335N/A <p>If the main_server has no <code>ServerName</code> at this
335N/A point, then the hostname of the machine that httpd is running
335N/A on is used instead. We will call the <em>main_server address
335N/A set</em> those IP addresses returned by a DNS lookup on the
335N/A <code>ServerName</code> of the main_server.</p>
335N/A
335N/A <p>For any undefined <code>ServerName</code> fields, a
335N/A name-based vhost defaults to the address given first in the
335N/A <code>VirtualHost</code> statement defining the vhost.</p>
335N/A
335N/A <p>Any vhost that includes the magic <samp>_default_</samp>
335N/A wildcard is given the same <code>ServerName</code> as the
335N/A main_server.</p>
335N/A
335N/A <h3>Virtual Host Matching</h3>
0N/A
335N/A <p>The server determines which vhost to use for a request as
0N/A follows:</p>
335N/A
335N/A <h4>Hash table lookup</h4>
0N/A
335N/A <p>When the connection is first made by a client, the IP
335N/A address to which the client connected is looked up in the
335N/A internal IP hash table.</p>
0N/A
0N/A <p>If the lookup fails (the IP address wasn't found) the
202N/A request is served from the <samp>_default_</samp> vhost if
0N/A there is such a vhost for the port to which the client sent the
0N/A request. If there is no matching <samp>_default_</samp> vhost
0N/A the request is served from the main_server.</p>
0N/A
0N/A <p>If the IP address is not found in the hash table then the
0N/A match against the port number may also result in an entry
0N/A corresponding to a <code>NameVirtualHost *</code>, which is
0N/A subsequently handled like other name-based vhosts.</p>
0N/A
0N/A <p>If the lookup succeeded (a corresponding list for the IP
0N/A address was found) the next step is to decide if we have to
0N/A deal with an IP-based or a name-base vhost.</p>
0N/A
0N/A <h4>IP-based vhost</h4>
0N/A
335N/A <p>If the entry we found has an empty name list then we have
335N/A found an IP-based vhost, no further actions are performed and
335N/A the request is served from that vhost.</p>
335N/A
0N/A <h4>Name-based vhost</h4>
335N/A
335N/A <p>If the entry corresponds to a name-based vhost the name list
0N/A contains one or more vhost structures. This list contains the
0N/A vhosts in the same order as the <code>VirtualHost</code>
66N/A directives appear in the config file.</p>
0N/A
335N/A <p>The first vhost on this list (the first vhost in the config
0N/A file with the specified IP address) has the highest priority
0N/A and catches any request to an unknown server name or a request
0N/A without a <code>Host:</code> header field.</p>
0N/A
0N/A <p>If the client provided a <code>Host:</code> header field the
0N/A list is searched for a matching vhost and the first hit on a
<code>ServerName</code> or <code>ServerAlias</code> is taken
and the request is served from that vhost. A <code>Host:</code>
header field can contain a port number, but Apache always
matches against the real port to which the client sent the
request.</p>
<p>If the client submitted a HTTP/1.0 request without
<code>Host:</code> header field we don't know to what server
the client tried to connect and any existing
<code>ServerPath</code> 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.</p>
<p>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).</p>
<h4>Persistent connections</h4>
The IP lookup described above is only done <em>once</em> for a
particular TCP/IP session while the name lookup is done on
<em>every</em> request during a KeepAlive/persistent
connection. In other words a client may request pages from
different name-based vhosts during a single persistent
connection.
<h4>Absolute URI</h4>
<p>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 <em>and</em> 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.</p>
<h3>Observations</h3>
<ul>
<li>A name-based vhost can never interfere with an IP-base
vhost and vice versa. IP-based vhosts can only be reached
through an IP address of its own address set and never
through any other address. The same applies to name-based
vhosts, they can only be reached through an IP address of the
corresponding address set which must be defined with a
<code>NameVirtualHost</code> directive.</li>
<li><code>ServerAlias</code> and <code>ServerPath</code>
checks are never performed for an IP-based vhost.</li>
<li>The order of name-/IP-based, the <samp>_default_</samp>
vhost and the <code>NameVirtualHost</code> directive 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.</li>
<li>For security reasons the port number given in a
<code>Host:</code> header field is never used during the
matching process. Apache always uses the real port to which
the client sent the request.</li>
<li>If a <code>ServerPath</code> directive exists which is a
prefix of another <code>ServerPath</code> directive 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 <code>Host:</code> header field was
available to disambiguate the two.)</li>
<li>If two IP-based vhosts have an address in common, the
vhost appearing first in the config file is always matched.
Such a thing might happen inadvertently. The server will give
a warning in the error logfile when it detects this.</li>
<li>A <code>_default_</code> vhost catches a request only if
there is no other vhost with a matching IP address
<em>and</em> a matching port number for the request. The
request is only caught if the port number to which the client
sent the request matches the port number of your
<code>_default_</code> vhost which is your standard
<code>Listen</code> by default. A wildcard port can be
specified (<em>i.e.</em>, <code>_default_:*</code>) to catch
requests to any available port. This also applies to
<code>NameVirtualHost *</code> vhosts.</li>
<li>The main_server is only used to serve a request if the IP
address and port number to which the client connected is
unspecified and does not match any other vhost (including a
<code>_default_</code> vhost). In other words the main_server
only catches a request for an unspecified address/port
combination (unless there is a <code>_default_</code> vhost
which matches that port).</li>
<li>A <code>_default_</code> vhost or the main_server is
<em>never</em> matched for a request with an unknown or
missing <code>Host:</code> header field if the client
connected to an address (and port) which is used for
name-based vhosts, <em>e.g.</em>, in a
<code>NameVirtualHost</code> directive.</li>
<li>You should never specify DNS names in
<code>VirtualHost</code> directives 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 <a href="/dns-caveats.html">more
information</a> available on this and the next two
topics.</li>
<li><code>ServerName</code> should always be set for each
vhost. Otherwise A DNS lookup is required for each
vhost.</li>
</ul>
<h3>Tips</h3>
<p>In addition to the tips on the <a
href="/dns-caveats.html#tips">DNS Issues</a> page, here are
some further tips:</p>
<ul>
<li>Place all main_server definitions before any
<code>VirtualHost</code> definitions. (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.)</li>
<li>Group corresponding <code>NameVirtualHost</code> and
<code>VirtualHost</code> definitions in your configuration to
ensure better readability.</li>
<li>Avoid <code>ServerPaths</code> which are prefixes of
other <code>ServerPaths</code>. 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 (<em>i.e.</em>, "ServerPath
/abc" should appear after "ServerPath /abc/def").</li>
</ul>
<!--#include virtual="footer.html" -->
</body>
</html>