details.html.en revision 4cb65c31bc681540ea623e1cb2bdd09749fb8d7e
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
This file is generated from xml source: DO NOT EDIT
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
<title>An In-Depth Discussion of Virtual Host Matching - Apache HTTP Server</title>
<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<body id="manual-page"><div id="page-header">
<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
<p class="apache">Apache HTTP Server Version 2.3</p>
<div id="path">
<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.3</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>An In-Depth Discussion of Virtual Host Matching</h1>
<div class="toplang">
<p><span>Available Languages: </span><a href="/en/vhosts/details.html" title="English"> en </a> |
<a href="/fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a> |
<a href="/tr/vhosts/details.html" hreflang="tr" rel="alternate" title="T�rk�e"> tr </a></p>
</div>
<p>This document attempts to explain
exactly what Apache HTTP Server does when deciding what virtual host to
serve a request from.</p>
Name-based vs. IP-based Virtual Hosts</a> to decide which type they
<p>If you want to understand all the details, then you can
come back to this page.</p>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#configparsing">Configuration File</a></li>
</ul><h3>See also</h3><ul class="seealso"><li><a href="ip-based.html">IP-based Virtual Host Support</a></li><li><a href="name-based.html">Name-based Virtual Hosts Support</a></li><li><a href="examples.html">Virtual Host examples for common setups</a></li><li><a href="mass.html">Dynamically configured mass virtual hosting</a></li></ul></div>
<div class="section">
<h2><a name="configparsing" id="configparsing">Configuration File</a></h2>
<p>There is a <em>main server</em> which consists of all the
definitions appearing outside of
<code><VirtualHost></code> sections.</p>
<p>There are virtual
servers, called <em>vhosts</em>, which are defined by
sections.</p>
<p>Each <code>VirtualHost</code> directive includes one
or more addresses and optional ports.</p>
<p>Hostnames can be used in place of IP addresses in a virtual
host definition, but they are resolved at startup and if any name
resolutions fail, those virtual host definitions are ignored.
This is, therefore, not recommended.</p>
<p>The address can be specified as
<code>*</code>, which will match a request if no
other vhost has the explicit address on which the request was
received. </p>
<p>The address appearing in the <code>VirtualHost</code>
directive can have an optional port. If the port is unspecified,
it is treated as a wildcard port, which can also be indicated
explicitly using <code>*</code>.
The wildcard port matches any port.</p>
<p>(Port numbers specified in the <code>VirtualHost</code> directive do
not influence what port numbers Apache will listen on, they only control
which <code>VirtualHost</code> will be selected to handle a request.
control the addresses and ports on which the server listens.)
</p>
<p>Collectively the
entire set of addresses (including multiple
results from DNS lookups) are called the vhost's
<em>address set</em>.</p>
<p>Apache automatically discriminates on the
basis of the HTTP <code>Host</code> header supplied by the client
whenever the most specific match for an IP address and port combination
is listed in multiple virtual hosts.</p>
<p>The
may appear anywhere within the definition of a server. However,
each appearance overrides the previous appearance (within that
server). If no <code>ServerName</code> is specified, the server
attempts to deduce it from the server's IP address.</p>
<p>The first name-based vhost in the configuration file for a
given IP:port pair is significant because it is used for all
requests received on that address and port for which no other
vhost for that IP:port pair has a matching ServerName or
ServerAlias. It is also used for all SSL connections if the
server does not support <a class="glossarylink" href="/glossary.html#servernameindication" title="see glossary">Server Name Indication</a>.</p>
<p>For every vhost various default values are set. In
particular:</p>
<ol>
<li>If a vhost has no <code class="directive"><a href="/mod/core.html#serveradmin">ServerAdmin</a></code>,
<code class="directive"><a href="/mod/core.html#maxkeepaliverequests">MaxKeepAliveRequests</a></code>,
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.)</li>
<li>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.</li>
<li>The per-server configs for each module from the
main server are merged into the vhost server.</li>
</ol>
<p>Essentially, the main server is treated as "defaults" or a
"base" on which to build each vhost. But the positioning of
these main server definitions in the config file is largely
irrelevant -- the entire config of the main server has been
parsed when this final merging occurs. So even if a main server
definition appears after a vhost definition it might affect the
vhost definition.</p>
<p>If the main server has no <code>ServerName</code> at this
point, then the hostname of the machine that <code class="program"><a href="/programs/httpd.html">httpd</a></code>
is running on is used instead. We will call the <em>main server address
set</em> those IP addresses returned by a DNS lookup on the
<code>ServerName</code> of the main server.</p>
<p>For any undefined <code>ServerName</code> fields, a
name-based vhost defaults to the address given first in the
<code>VirtualHost</code> statement defining the vhost.</p>
<p>Any vhost that includes the magic <code>_default_</code>
wildcard is given the same <code>ServerName</code> as the
main server.</p>
<div class="section">
<h2><a name="hostmatching" id="hostmatching">Virtual Host Matching</a></h2>
<p>The server determines which vhost to use for a request as
follows:</p>
<h3><a name="hashtable" id="hashtable">IP address lookup</a></h3>
<p>When the connection is first received on some address and port,
the server looks for all the <code>VirtualHost</code> definitions
that have the same IP address and port.</p>
<p>If there are no exact matches for the address and port, then
wildcard (<code>*</code>) matches are considered.</p>
<p>If no matches are found, the request is served by the
main server.</p>
<p>If there are <code>VirtualHost</code> definitions for
the IP address, the next step is to decide if we have to
deal with an IP-based or a name-based vhost.</p>
<h3><a name="ipbased" id="ipbased">IP-based vhost</a></h3>
<p>If there is exactly one <code>VirtualHost</code> directive
listing the IP address and port combination that was determined
to be the best match, no further actions are performed and
the request is served from the matching vhost.</p>
<h3><a name="namebased" id="namebased">Name-based vhost</a></h3>
<p>If there are multiple <code>VirtualHost</code> directives listing
the IP address and port combination that was determined to be the
best match, the "list" in the remaining steps refers to the list of vhosts
that matched, in the order they were in the configuration file.</p>
<p>If the connection is using SSL, the server supports <a class="glossarylink" href="/glossary.html#servernameindication" title="see glossary">Server Name Indication</a>, and
the SSL client handshake includes the TLS extension with the
requested hostname, then that hostname is used below just like the
<code>Host:</code> header would be used on a non-SSL connection.
Otherwise, the first name-based vhost whose address matched is
used for SSL connections. This is significant because the
vhost determines which certificate the server will use for the
connection.</p>
<p>If the request contains a <code>Host:</code> header field, the
list is searched for the first vhost with a matching
<code>ServerName</code> or <code>ServerAlias</code>, and the
request is served from that vhost. A <code>Host:</code> header
field can contain a port number, but Apache always ignores it and
matches against the real port to which the client sent the
request.</p>
<p>The first vhost in the config
file with the specified IP address has the highest priority
and catches any request to an unknown server name, or a request
request).</p>
<h3><a name="persistent" id="persistent">Persistent connections</a></h3>
<p>The <em>IP lookup</em> described above is only done <em>once</em> for a
connection. In other words, a client may request pages from
different name-based vhosts during a single persistent
connection.</p>
<h3><a name="absoluteURI" id="absoluteURI">Absolute URI</a></h3>
<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
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><a name="observations" id="observations">Observations</a></h3>
<ul>
<li>Name-based virtual hosting is a process applied after
the server has selected the best matching IP-based virtual
host.</li>
<li>If you don't care what IP address the client has connected to, use a
"*" as the address of every virtual host, and name-based virtual hosting
is applied across all configured virtual hosts.</li>
<li><code>ServerName</code> and <code>ServerAlias</code>
checks are never performed for an IP-based vhost.</li>
<li>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>Any port in the <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 two vhosts have an address in common, those common addresses
act as name-based virtual hosts implicitly. This is new behavior as of
2.3.11.</li>
<li>The main server is only used to serve a request if the IP
address and port number to which the client connected
does not match any vhost (including a
<code>*</code> vhost). In other words, the main server
combination (unless there is a <code>_default_</code> vhost
which matches that port).</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>
<div class="section">
<h2><a name="tips" id="tips">Tips</a></h2>
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>
</ul>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/vhosts/details.html" title="English"> en </a> |
<a href="/fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a> |
<a href="/tr/vhosts/details.html" hreflang="tr" rel="alternate" title="T�rk�e"> tr </a></p>
</div><div id="footer">
<p class="apache">Copyright 2010 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div>
</body></html>