details.xml revision 9967f2fc7dda1d28667d039ad2c85fe9eda080ac
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<?xml version='1.0' encoding='UTF-8' ?>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<!DOCTYPE manualpage SYSTEM "/style/manualpage.dtd">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<!-- $LastChangedRevision$ -->
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<!--
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd Licensed to the Apache Software Foundation (ASF) under one or more
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd contributor license agreements. See the NOTICE file distributed with
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd this work for additional information regarding copyright ownership.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd The ASF licenses this file to You under the Apache License, Version 2.0
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd (the "License"); you may not use this file except in compliance with
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd the License. You may obtain a copy of the License at
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd http://www.apache.org/licenses/LICENSE-2.0
27e52281f1522522b170cafc76b08b58aa70ccaand
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd Unless required by applicable law or agreed to in writing, software
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd distributed under the License is distributed on an "AS IS" BASIS,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4b5981e276e93df97c34e4da05ca5cf8bbd937dand See the License for the specific language governing permissions and
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd limitations under the License.
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd-->
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
f3ec420152ca921e4c1ce77782f51b53f659018dnd<manualpage metafile="details.xml.meta">
f3ec420152ca921e4c1ce77782f51b53f659018dnd<parentdocument href="./">Virtual Hosts</parentdocument>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <title>An In-Depth Discussion of Virtual Host Matching</title>
e4ca72aa494fed7b6948012734b9c9c098fbba07nd
e4ca72aa494fed7b6948012734b9c9c098fbba07nd<summary>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>This document attempts to explain
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd exactly what Apache HTTP Server does when deciding what virtual host to
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd serve a request from.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>Most users should read about <a href="name-based.html#namevip">
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd Name-based vs. IP-based Virtual Hosts</a> to decide which type they
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd want to use, then read more about <a href="name-based.html">name-based</a>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd or <a href="ip-based.html">IP-based</a> virtualhosts, and then see
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <a href="examples.html">some examples</a>.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>If you want to understand all the details, then you can
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd come back to this page.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd</summary>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<seealso><a href="ip-based.html">IP-based Virtual Host Support</a></seealso>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<seealso><a href="name-based.html">Name-based Virtual Hosts Support</a></seealso>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<seealso><a href="examples.html">Virtual Host examples for common setups</a></seealso>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<seealso><a href="mass.html">Dynamically configured mass virtual hosting</a></seealso>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd<section id="configparsing"><title>Configuration File</title>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>There is a <em>main server</em> which consists of all the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd definitions appearing outside of
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <code>&lt;VirtualHost&gt;</code> sections.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>There are virtual
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd servers, called <em>vhosts</em>, which are defined by
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <directive type="section" module="core">VirtualHost</directive>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd sections.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>Each <code>VirtualHost</code> directive includes one
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd or more addresses and optional ports.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>Hostnames can be used in place of IP addresses in a virtual
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd host definition, but they are resolved at startup and if any name
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd resolutions fail, those virtual host definitions are ignored.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd This is, therefore, not recommended.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>The address can be specified as
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <code>*</code>, which will match a request if no
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd other vhost has the explicit address on which the request was
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd received. </p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>The address appearing in the <code>VirtualHost</code>
71fccc298df6a1540d408151a26aa22beed55d0bnd directive can have an optional port. If the port is unspecified,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd it is treated as a wildcard port, which can also be indicated
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd explicitly using <code>*</code>.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd The wildcard port matches any port.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>(Port numbers specified in the <code>VirtualHost</code> directive do
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd not influence what port numbers Apache will listen on, they only control
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd which <code>VirtualHost</code> will be selected to handle a request.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd Use the <directive module="mpm_common">Listen</directive> directive to
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd control the addresses and ports on which the server listens.)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd </p>
71fccc298df6a1540d408151a26aa22beed55d0bnd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>Collectively the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd entire set of addresses (including multiple
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd results from DNS lookups) are called the vhost's
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd <em>address set</em>.</p>
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
f3ec420152ca921e4c1ce77782f51b53f659018dnd <p>Apache automatically discriminates on the
f3ec420152ca921e4c1ce77782f51b53f659018dnd basis of the HTTP <code>Host</code> header supplied by the client
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd whenever the most specific match for an IP address and port combination
6eed902e5b4d3e016e220bfbf8769a87c4cb242enoodl is listed in multiple virtual hosts.</p>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd <p>The
<directive module="core">ServerName</directive> directive
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 <glossary
ref="servernameindication">Server Name Indication</glossary>.</p>
<p>The complete list of names in the <code>VirtualHost</code>
directive are treated just like a (non wildcard) <code>ServerAlias</code>
(but are not overridden by any <code>ServerAlias</code> statement).</p>
<p>For every vhost various default values are set. In
particular:</p>
<ol>
<li>If a vhost has no <directive module="core">ServerAdmin</directive>,
<directive module="core">Timeout</directive>,
<directive module="core">KeepAliveTimeout</directive>,
<directive module="core">KeepAlive</directive>,
<directive module="core">MaxKeepAliveRequests</directive>,
<directive module="mpm_common">ReceiveBufferSize</directive>,
or <directive module="mpm_common">SendBufferSize</directive>
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 <program>httpd</program>
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>
</section>
<section id="hostmatching"><title>Virtual Host Matching</title>
<p>The server determines which vhost to use for a request as
follows:</p>
<section id="hashtable"><title>IP address lookup</title>
<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>
</section>
<section id="ipbased"><title>IP-based vhost</title>
<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>
</section>
<section id="namebased"><title>Name-based vhost</title>
<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 <glossary
ref="servernameindication">Server Name Indication</glossary>, 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
without a <code>Host:</code> header field (such as a HTTP/1.0
request).</p>
</section>
<section id="persistent"><title>Persistent connections</title>
<p>The <em>IP lookup</em> described above is only done <em>once</em> for a
particular TCP/IP session while the <em>name lookup</em> 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.</p>
</section>
<section id="absoluteURI"><title>Absolute URI</title>
<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>
</section>
<section id="observations"><title>Observations</title>
<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
only catches a request for an unspecified address/port
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>
</section>
</section>
<section id="tips"><title>Tips</title>
<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>
</ul>
</section>
</manualpage>