details.html.en revision 52fff662005b1866a3ff09bb6c902800c5cc6ded
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess This file is generated from xml source: DO NOT EDIT
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5a58787efeb02a1c3f06569d019ad81fd2efa06end<title>An In-Depth Discussion of Virtual Host Matching - Apache HTTP Server</title>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
5a58787efeb02a1c3f06569d019ad81fd2efa06end<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
5a58787efeb02a1c3f06569d019ad81fd2efa06end<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
5a58787efeb02a1c3f06569d019ad81fd2efa06end<link href="/images/favicon.ico" rel="shortcut icon" /></head>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<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>
52fff662005b1866a3ff09bb6c902800c5cc6dedjerenkrantz<p class="apache">Apache HTTP Server Version 2.3</p>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div>
52fff662005b1866a3ff09bb6c902800c5cc6dedjerenkrantz<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs-project/">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>
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd<p><span>Available Languages: </span><a href="/en/vhosts/details.html" title="English"> en </a> |
d05d0eb4ae6d2a5e513fc3bf2555ce33da416634nd<a href="/fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a> |
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd<a href="/ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The virtual host code was completely rewritten in
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <strong>Apache 1.3</strong>. This document attempts to explain
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess exactly what Apache does when deciding what virtual host to
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess serve a hit from. With the help of the new
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#namevirtualhost">NameVirtualHost</a></code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess directive virtual host configuration should be a lot easier and
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess safer than with versions prior to 1.3.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If you just want to <cite>make it work</cite> without
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess understanding how, here are <a href="examples.html">some
5a58787efeb02a1c3f06569d019ad81fd2efa06end<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#configparsing">Config File Parsing</a></li>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<li><img alt="" src="/images/down.gif" /> <a href="#hostmatching">Virtual Host Matching</a></li>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<li><img alt="" src="/images/down.gif" /> <a href="#tips">Tips</a></li>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<h2><a name="configparsing" id="configparsing">Config File Parsing</a></h2>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>There is a <em>main_server</em> which consists of all the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess definitions appearing outside of
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code><VirtualHost></code> sections. There are virtual
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#virtualhost"><VirtualHost></a></code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess sections.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The directives
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/mpm_common.html#listen">Listen</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#serverpath">ServerPath</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess and <code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess can appear anywhere within the definition of a server. However,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess each appearance overrides the previous appearance (within that
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess server).</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The default value of the <code>Listen</code> field for
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess main_server is 80. The main_server has no default
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>ServerPath</code>, or <code>ServerAlias</code>. The
378b5789dab3a2cd1b4b96ac8e6636ee61024c82trawick default <code>ServerName</code> is deduced from the server's IP
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess address.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The main_server Listen directive has two functions. One
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess function is to determine the default network port Apache will
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess bind to. The second function is to specify the port number
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess which is used in absolute URIs during redirects.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Unlike the main_server, vhost ports <em>do not</em> affect
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess what ports Apache listens for connections on.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Each address appearing in the <code>VirtualHost</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess directive can have an optional port. If the port is unspecified
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess it defaults to the value of the main_server's most recent
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>Listen</code> statement. The special port <code>*</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess indicates a wildcard that matches any port. Collectively the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess entire set of addresses (including multiple <code>A</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess record results from DNS lookups) are called the vhost's
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Unless a <code class="directive"><a href="/mod/core.html#namevirtualhost">NameVirtualHost</a></code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess directive is used for a specific IP address the first vhost
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess with that address is treated as an IP-based vhost. The IP
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If name-based vhosts should be used a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>NameVirtualHost</code> directive <em>must</em> appear
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess with the IP address set to be used for the name-based vhosts.
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess In other words, you must specify the IP address that holds the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess hostname aliases (CNAMEs) for your name-based vhosts via a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>NameVirtualHost</code> directive in your configuration
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Multiple <code>NameVirtualHost</code> directives can be used
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess each with a set of <code>VirtualHost</code> directives but only
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess one <code>NameVirtualHost</code> directive should be used for
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess each specific IP:port pair.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>VirtualHost</code> directives is not important which
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess makes the following two examples identical (only the order of
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess the <code>VirtualHost</code> directives for <em>one</em>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess address set is important, see below):</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess NameVirtualHost 111.22.33.44<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.44><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server A<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.44><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server B<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess NameVirtualHost 111.22.33.55<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.55><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server C<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.55><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server D<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.44><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server A<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.55><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server C<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.44><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server B<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <VirtualHost 111.22.33.55><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server D<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess </VirtualHost><br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess NameVirtualHost 111.22.33.44<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess NameVirtualHost 111.22.33.55<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>(To aid the readability of your configuration you should
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess prefer the left variant.)</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>After parsing the <code>VirtualHost</code> directive, the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost server is given a default <code>Listen</code> equal to the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess port assigned to the first name in its <code>VirtualHost</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess directive.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The complete list of names in the <code>VirtualHost</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess directive are treated just like a <code>ServerAlias</code> (but
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess are not overridden by any <code>ServerAlias</code> statement)
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess if all names resolve to the same address set. Note that
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess subsequent <code>Listen</code> statements for this vhost will not
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess affect the ports assigned in the address set.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>During initialization a list for each IP address is
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess generated and inserted into an hash table. If the IP address is
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess used in a <code>NameVirtualHost</code> directive the list
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess contains all name-based vhosts for the given IP address. If
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess there are no vhosts defined for that address the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>NameVirtualHost</code> directive is ignored and an error
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess is logged. For an IP-based vhost the list in the hash table is
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess empty.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Due to a fast hashing function the overhead of hashing an IP
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess address during a request is minimal and almost not existent.
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess Additionally the table is optimized for IP addresses which vary
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess in the last octet.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>For every vhost various default values are set. In
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess particular:</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>If a vhost has no <code class="directive"><a href="/mod/core.html#serveradmin">ServerAdmin</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#resourceconfig">ResourceConfig</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#accessconfig">AccessConfig</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#timeout">Timeout</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#keepalivetimeout">KeepAliveTimeout</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#keepalive">KeepAlive</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#maxkeepaliverequests">MaxKeepAliveRequests</a></code>,
2bc7f1cf720973a67f8ff7a8d523e40569ae5b6cnd <code class="directive"><a href="/mod/core.html#receivebuffersize">ReceiveBufferSize</a></code>,
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess or <code class="directive"><a href="/mod/core.html#sendbuffersize">SendBufferSize</a></code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess directive then the respective value is inherited from the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess main_server. (That is, inherited from whatever the final
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess setting of that value is in the main_server.)</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>The "lookup defaults" that define the default directory
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess permissions for a vhost are merged with those of the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess main_server. This includes any per-directory configuration
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess information for any module.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>The per-server configs for each module from the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess main_server are merged into the vhost server.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Essentially, the main_server is treated as "defaults" or a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess "base" on which to build each vhost. But the positioning of
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess these main_server definitions in the config file is largely
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess irrelevant -- the entire config of the main_server has been
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess parsed when this final merging occurs. So even if a main_server
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess definition appears after a vhost definition it might affect the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost definition.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the main_server has no <code>ServerName</code> at this
9bcfc3697a91b5215893a7d0206865b13fc72148nd point, then the hostname of the machine that <code class="program"><a href="/programs/httpd.html">httpd</a></code>
9bcfc3697a91b5215893a7d0206865b13fc72148nd is running on is used instead. We will call the <em>main_server address
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess set</em> those IP addresses returned by a DNS lookup on the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess name-based vhost defaults to the address given first in the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>VirtualHost</code> statement defining the vhost.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Any vhost that includes the magic <code>_default_</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess wildcard is given the same <code>ServerName</code> as the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess main_server.</p>
5a58787efeb02a1c3f06569d019ad81fd2efa06end</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<h2><a name="hostmatching" id="hostmatching">Virtual Host Matching</a></h2>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The server determines which vhost to use for a request as
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess follows:</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="hashtable" id="hashtable">Hash table lookup</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>When the connection is first made by a client, the IP
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess address to which the client connected is looked up in the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess internal IP hash table.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the lookup fails (the IP address wasn't found) the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess request is served from the <code>_default_</code> vhost if
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess there is such a vhost for the port to which the client sent the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess request. If there is no matching <code>_default_</code> vhost
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess the request is served from the main_server.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the IP address is not found in the hash table then the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess match against the port number may also result in an entry
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess corresponding to a <code>NameVirtualHost *</code>, which is
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess subsequently handled like other name-based vhosts.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the lookup succeeded (a corresponding list for the IP
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess address was found) the next step is to decide if we have to
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess deal with an IP-based or a name-base vhost.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="ipbased" id="ipbased">IP-based vhost</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the entry we found has an empty name list then we have
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess found an IP-based vhost, no further actions are performed and
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess the request is served from that vhost.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="namebased" id="namebased">Name-based vhost</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the entry corresponds to a name-based vhost the name list
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess contains one or more vhost structures. This list contains the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhosts in the same order as the <code>VirtualHost</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess directives appear in the config file.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The first vhost on this list (the first vhost in the config
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess file with the specified IP address) has the highest priority
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess and catches any request to an unknown server name or a request
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the client provided a <code>Host:</code> header field the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess list is searched for a matching vhost and the first hit on a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>ServerName</code> or <code>ServerAlias</code> is taken
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess and the request is served from that vhost. A <code>Host:</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess header field can contain a port number, but Apache always
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess matches against the real port to which the client sent the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess request.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>Host:</code> header field we don't know to what server
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess the client tried to connect and any existing
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>ServerPath</code> is matched against the URI from the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess request. The first matching path on the list is used and the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess request is served from that vhost.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If no matching vhost could be found the request is served
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess from the first vhost with a matching port number that is on the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess list for the IP to which the client connected (as already
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess mentioned before).</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="persistent" id="persistent">Persistent connections</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The IP lookup described above is only done <em>once</em> for a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess particular TCP/IP session while the name lookup is done on
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess connection. In other words a client may request pages from
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess different name-based vhosts during a single persistent
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess connection.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="absoluteURI" id="absoluteURI">Absolute URI</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the URI from the request is an absolute URI, and its
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess hostname and port match the main server or one of the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess configured virtual hosts <em>and</em> match the address and
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess port to which the client sent the request, then the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess scheme/hostname/port prefix is stripped off and the remaining
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess relative URI is served by the corresponding main server or
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess virtual host. If it does not match, then the URI remains
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess untouched and the request is taken to be a proxy request.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<h3><a name="observations" id="observations">Observations</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>A name-based vhost can never interfere with an IP-base
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost and vice versa. IP-based vhosts can only be reached
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess through an IP address of its own address set and never
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess through any other address. The same applies to name-based
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhosts, they can only be reached through an IP address of the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess corresponding address set which must be defined with a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li><code>ServerAlias</code> and <code>ServerPath</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess checks are never performed for an IP-based vhost.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>The order of name-/IP-based, the <code>_default_</code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost and the <code>NameVirtualHost</code> directive within
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess the config file is not important. Only the ordering of
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess name-based vhosts for a specific address set is significant.
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess The one name-based vhosts that comes first in the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess configuration file has the highest priority for its
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess corresponding address set.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>For security reasons the port number given in a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>Host:</code> header field is never used during the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess matching process. Apache always uses the real port to which
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess the client sent the request.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>If a <code>ServerPath</code> directive exists which is a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess prefix of another <code>ServerPath</code> directive that
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess appears later in the configuration file, then the former will
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess always be matched and the latter will never be matched. (That
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess is assuming that no <code>Host:</code> header field was
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess available to disambiguate the two.)</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>If two IP-based vhosts have an address in common, the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost appearing first in the config file is always matched.
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess Such a thing might happen inadvertently. The server will give
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess a warning in the error logfile when it detects this.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>A <code>_default_</code> vhost catches a request only if
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess there is no other vhost with a matching IP address
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <em>and</em> a matching port number for the request. The
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess request is only caught if the port number to which the client
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess sent the request matches the port number of your
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess specified (<em>i.e.</em>, <code>_default_:*</code>) to catch
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess requests to any available port. This also applies to
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>The main_server is only used to serve a request if the IP
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess address and port number to which the client connected is
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess unspecified and does not match any other vhost (including a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>_default_</code> vhost). In other words the main_server
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess combination (unless there is a <code>_default_</code> vhost
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess which matches that port).</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>A <code>_default_</code> vhost or the main_server is
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <em>never</em> matched for a request with an unknown or
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess connected to an address (and port) which is used for
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>You should never specify DNS names in
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>VirtualHost</code> directives because it will force
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess your server to rely on DNS to boot. Furthermore it poses a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess security threat if you do not control the DNS for all the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess domains listed. There's <a href="/dns-caveats.html">more
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess information</a> available on this and the next two
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess topics.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li><code>ServerName</code> should always be set for each
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost. Otherwise A DNS lookup is required for each
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost.</li>
5a58787efeb02a1c3f06569d019ad81fd2efa06end</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>In addition to the tips on the <a href="/dns-caveats.html#tips">DNS Issues</a> page, here are
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess some further tips:</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>Place all main_server definitions before any
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>VirtualHost</code> definitions. (This is to aid the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess readability of the configuration -- the post-config merging
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess process makes it non-obvious that definitions mixed in around
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess virtual hosts might affect all virtual hosts.)</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>Group corresponding <code>NameVirtualHost</code> and
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>VirtualHost</code> definitions in your configuration to
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess ensure better readability.</li>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <li>Avoid <code>ServerPaths</code> which are prefixes of
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess other <code>ServerPaths</code>. If you cannot avoid this then
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess you have to ensure that the longer (more specific) prefix
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhost appears earlier in the configuration file than the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess shorter (less specific) prefix (<em>i.e.</em>, "ServerPath
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd<p><span>Available Languages: </span><a href="/en/vhosts/details.html" title="English"> en </a> |
d05d0eb4ae6d2a5e513fc3bf2555ce33da416634nd<a href="/fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a> |
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd<a href="/ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
b95ae799514ad86a15610ad75808d7065e9847c9kess<p class="apache">Copyright 1995-2005 The Apache Software Foundation or its licensors, as applicable.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<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>