details.html.en revision 378b5789dab3a2cd1b4b96ac8e6636ee61024c82
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<?xml version="1.0" encoding="ISO-8859-1"?>
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 -->
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" />
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<link href="/images/favicon.ico" rel="shortcut icon" /></head>
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<body id="manual-page"><div id="page-header">
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<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>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<p class="apache">Apache HTTP Server Version 2.1</p>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<img alt="" src="/images/feather.gif" /></div>
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
3f08db06526d6901aa08c110b5bc7dde6bc39905nd<div id="path">
5a58787efeb02a1c3f06569d019ad81fd2efa06end<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs-project/">Documentation</a> &gt; <a href="../">Version 2.1</a> &gt; <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>An In-Depth Discussion of Virtual Host Matching</h1>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<div class="toplang">
5a58787efeb02a1c3f06569d019ad81fd2efa06end<p><span>Available Languages: </span><a href="/en/vhosts/details.html" title="English">&nbsp;en&nbsp;</a> |
3f08db06526d6901aa08c110b5bc7dde6bc39905nd<a href="/ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd</div>
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
d05d0eb4ae6d2a5e513fc3bf2555ce33da416634nd
4b3a8afbfcea8b265d179a122bf40dfedd1ce280takashi <p>The virtual host code was completely rewritten in
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung <strong>Apache 1.3</strong>. This document attempts to explain
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd exactly what Apache does when deciding what virtual host to
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd serve a hit from. With the help of the new
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code class="directive"><a href="/mod/core.html#namevirtualhost">NameVirtualHost</a></code>
15107611640cec4e666dd7e2e1dee15c270e9e98rbowen directive virtual host configuration should be a lot easier and
15107611640cec4e666dd7e2e1dee15c270e9e98rbowen safer than with versions prior to 1.3.</p>
15107611640cec4e666dd7e2e1dee15c270e9e98rbowen
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If you just want to <cite>make it work</cite> without
60a4b2c422dcbb08a554fb193105c08da592718bpoirier understanding how, here are <a href="examples.html">some
60a4b2c422dcbb08a554fb193105c08da592718bpoirier examples</a>.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier</div>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#configparsing">Config File Parsing</a></li>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier<li><img alt="" src="/images/down.gif" /> <a href="#hostmatching">Virtual Host Matching</a></li>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier<li><img alt="" src="/images/down.gif" /> <a href="#tips">Tips</a></li>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier</ul></div>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
5a58787efeb02a1c3f06569d019ad81fd2efa06end<div class="section">
60a4b2c422dcbb08a554fb193105c08da592718bpoirier<h2><a name="configparsing" id="configparsing">Config File Parsing</a></h2>
5a58787efeb02a1c3f06569d019ad81fd2efa06end
5a58787efeb02a1c3f06569d019ad81fd2efa06end <p>There is a <em>main_server</em> which consists of all the
60a4b2c422dcbb08a554fb193105c08da592718bpoirier definitions appearing outside of
5a58787efeb02a1c3f06569d019ad81fd2efa06end <code>&lt;VirtualHost&gt;</code> sections. There are virtual
5a58787efeb02a1c3f06569d019ad81fd2efa06end servers, called <em>vhosts</em>, which are defined by
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess sections.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The directives
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <code class="directive"><a href="/mod/mpm_common.html#listen">Listen</a></code>,
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code>,
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <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>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>The default value of the <code>Listen</code> field for
60a4b2c422dcbb08a554fb193105c08da592718bpoirier main_server is 80. The main_server has no default
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <code>ServerPath</code>, or <code>ServerAlias</code>. The
60a4b2c422dcbb08a554fb193105c08da592718bpoirier default <code>ServerName</code> is deduced from the server's IP
60a4b2c422dcbb08a554fb193105c08da592718bpoirier address.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>The main_server Listen directive has two functions. One
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf function is to determine the default network port Apache will
60a4b2c422dcbb08a554fb193105c08da592718bpoirier bind to. The second function is to specify the port number
60a4b2c422dcbb08a554fb193105c08da592718bpoirier which is used in absolute URIs during redirects.</p>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>Unlike the main_server, vhost ports <em>do not</em> affect
60a4b2c422dcbb08a554fb193105c08da592718bpoirier what ports Apache listens for connections on.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>Each address appearing in the <code>VirtualHost</code>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier directive can have an optional port. If the port is unspecified
60a4b2c422dcbb08a554fb193105c08da592718bpoirier it defaults to the value of the main_server's most recent
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <code>Listen</code> statement. The special port <code>*</code>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier indicates a wildcard that matches any port. Collectively the
60a4b2c422dcbb08a554fb193105c08da592718bpoirier entire set of addresses (including multiple <code>A</code>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier record results from DNS lookups) are called the vhost's
ad79e6242f42c1915d14628f8bf44f04b129716arbowen <em>address set</em>.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>Unless a <code class="directive"><a href="/mod/core.html#namevirtualhost">NameVirtualHost</a></code>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf directive is used for a specific IP address the first vhost
60a4b2c422dcbb08a554fb193105c08da592718bpoirier with that address is treated as an IP-based vhost. The IP
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf address can also be the wildcard <code>*</code>.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If name-based vhosts should be used a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>NameVirtualHost</code> directive <em>must</em> appear
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf with the IP address set to be used for the name-based vhosts.
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf In other words, you must specify the IP address that holds the
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf hostname aliases (CNAMEs) for your name-based vhosts via a
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf <code>NameVirtualHost</code> directive in your configuration
1696993fbee984b093ae8b823e3ea3fa80eae486covener file.</p>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>Multiple <code>NameVirtualHost</code> directives can be used
60a4b2c422dcbb08a554fb193105c08da592718bpoirier each with a set of <code>VirtualHost</code> directives but only
60a4b2c422dcbb08a554fb193105c08da592718bpoirier one <code>NameVirtualHost</code> directive should be used for
60a4b2c422dcbb08a554fb193105c08da592718bpoirier each specific IP:port pair.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The ordering of <code>NameVirtualHost</code> and
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <code>VirtualHost</code> directives is not important which
60a4b2c422dcbb08a554fb193105c08da592718bpoirier makes the following two examples identical (only the order of
60a4b2c422dcbb08a554fb193105c08da592718bpoirier the <code>VirtualHost</code> directives for <em>one</em>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier address set is important, see below):</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier<table><tr>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier<td><div class="example"><p><code>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess NameVirtualHost 111.22.33.44<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;VirtualHost 111.22.33.44&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server A<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess ...<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;/VirtualHost&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;VirtualHost 111.22.33.44&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server B<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess ...<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;/VirtualHost&gt;<br />
ad79e6242f42c1915d14628f8bf44f04b129716arbowen <br />
ad79e6242f42c1915d14628f8bf44f04b129716arbowen NameVirtualHost 111.22.33.55<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;VirtualHost 111.22.33.55&gt;<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier # server C<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier ...<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;/VirtualHost&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;VirtualHost 111.22.33.55&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server D<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier ...<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;/VirtualHost&gt;
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess</code></p></div></td>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<td><div class="example"><p><code>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier &lt;VirtualHost 111.22.33.44&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server A<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;/VirtualHost&gt;<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier &lt;VirtualHost 111.22.33.55&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server C<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier ...<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier &lt;/VirtualHost&gt;<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier &lt;VirtualHost 111.22.33.44&gt;<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess # server B<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess ...<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;/VirtualHost&gt;<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier &lt;VirtualHost 111.22.33.55&gt;<br />
9bcfc3697a91b5215893a7d0206865b13fc72148nd # server D<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier ...<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess &lt;/VirtualHost&gt;<br />
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess NameVirtualHost 111.22.33.44<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess NameVirtualHost 111.22.33.55<br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <br />
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess</code></p></div></td>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess</tr></table>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>(To aid the readability of your configuration you should
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess prefer the left variant.)</p>
5a58787efeb02a1c3f06569d019ad81fd2efa06end
5a58787efeb02a1c3f06569d019ad81fd2efa06end <p>After parsing the <code>VirtualHost</code> directive, the
5a58787efeb02a1c3f06569d019ad81fd2efa06end 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
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The complete list of names in the <code>VirtualHost</code>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier directive are treated just like a <code>ServerAlias</code> (but
60a4b2c422dcbb08a554fb193105c08da592718bpoirier are not overridden by any <code>ServerAlias</code> statement)
60a4b2c422dcbb08a554fb193105c08da592718bpoirier if all names resolve to the same address set. Note that
60a4b2c422dcbb08a554fb193105c08da592718bpoirier subsequent <code>Listen</code> statements for this vhost will not
60a4b2c422dcbb08a554fb193105c08da592718bpoirier affect the ports assigned in the address set.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>During initialization a list for each IP address is
60a4b2c422dcbb08a554fb193105c08da592718bpoirier generated and inserted into an hash table. If the IP address is
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess used in a <code>NameVirtualHost</code> directive the list
60a4b2c422dcbb08a554fb193105c08da592718bpoirier contains all name-based vhosts for the given IP address. If
60a4b2c422dcbb08a554fb193105c08da592718bpoirier there are no vhosts defined for that address the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <code>NameVirtualHost</code> directive is ignored and an error
60a4b2c422dcbb08a554fb193105c08da592718bpoirier is logged. For an IP-based vhost the list in the hash table is
60a4b2c422dcbb08a554fb193105c08da592718bpoirier empty.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
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
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf <p>For every vhost various default values are set. In
4cb65c31bc681540ea623e1cb2bdd09749fb8d7esf particular:</p>
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf <ol>
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>,
4cb65c31bc681540ea623e1cb2bdd09749fb8d7esf <code class="directive"><a href="/mod/core.html#keepalive">KeepAlive</a></code>,
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf <code class="directive"><a href="/mod/core.html#maxkeepaliverequests">MaxKeepAliveRequests</a></code>,
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf or <code class="directive"><a href="/mod/core.html#sendbuffersize">SendBufferSize</a></code>
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf directive then the respective value is inherited from the
60a4b2c422dcbb08a554fb193105c08da592718bpoirier main_server. (That is, inherited from whatever the final
60a4b2c422dcbb08a554fb193105c08da592718bpoirier setting of that value is in the main_server.)</li>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <li>The "lookup defaults" that define the default directory
60a4b2c422dcbb08a554fb193105c08da592718bpoirier permissions for a vhost are merged with those of the
60a4b2c422dcbb08a554fb193105c08da592718bpoirier main_server. This includes any per-directory configuration
60a4b2c422dcbb08a554fb193105c08da592718bpoirier information for any module.</li>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <li>The per-server configs for each module from the
60a4b2c422dcbb08a554fb193105c08da592718bpoirier main_server are merged into the vhost server.</li>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier </ol>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>Essentially, the main_server is treated as "defaults" or a
60a4b2c422dcbb08a554fb193105c08da592718bpoirier "base" on which to build each vhost. But the positioning of
60a4b2c422dcbb08a554fb193105c08da592718bpoirier 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
60a4b2c422dcbb08a554fb193105c08da592718bpoirier vhost definition.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>If the main_server has no <code>ServerName</code> at this
60a4b2c422dcbb08a554fb193105c08da592718bpoirier point, then the hostname of the machine that httpd is running
60a4b2c422dcbb08a554fb193105c08da592718bpoirier 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 <code>ServerName</code> of the main_server.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>For any undefined <code>ServerName</code> fields, a
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess name-based vhost defaults to the address given first in the
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <code>VirtualHost</code> statement defining the vhost.</p>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>Any vhost that includes the magic <code>_default_</code>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier wildcard is given the same <code>ServerName</code> as the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess main_server.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<div class="section">
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess<h2><a name="hostmatching" id="hostmatching">Virtual Host Matching</a></h2>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>The server determines which vhost to use for a request as
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess follows:</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="hashtable" id="hashtable">Hash table lookup</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
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
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>
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf <p>If the IP address is not found in the hash table then the
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf match against the port number may also result in an entry
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf corresponding to a <code>NameVirtualHost *</code>, which is
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf subsequently handled like other name-based vhosts.</p>
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf <p>If the lookup succeeded (a corresponding list for the IP
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf address was found) the next step is to decide if we have to
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf deal with an IP-based or a name-base vhost.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="ipbased" id="ipbased">IP-based vhost</a></h3>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
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>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="namebased" id="namebased">Name-based vhost</a></h3>
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf <p>If the entry corresponds to a name-based vhost the name list
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf contains one or more vhost structures. This list contains the
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess vhosts in the same order as the <code>VirtualHost</code>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier directives appear in the config file.</p>
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
60a4b2c422dcbb08a554fb193105c08da592718bpoirier <p>The first vhost on this list (the first vhost in the config
6e14faf37935e36804b8bad802bc9dd58f3cf65dsf file with the specified IP address) has the highest priority
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess and catches any request to an unknown server name or a request
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess without a <code>Host:</code> header field.</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
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
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <p>If the client submitted a HTTP/1.0 request without
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
5a58787efeb02a1c3f06569d019ad81fd2efa06end <p>If no matching vhost could be found the request is served
5a58787efeb02a1c3f06569d019ad81fd2efa06end from the first vhost with a matching port number that is on the
5a58787efeb02a1c3f06569d019ad81fd2efa06end list for the IP to which the client connected (as already
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess mentioned before).</p>
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess <h3><a name="persistent" id="persistent">Persistent connections</a></h3>
60a4b2c422dcbb08a554fb193105c08da592718bpoirier
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 <em>every</em> request during a KeepAlive/persistent
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess connection. In other words a client may request pages from
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess different name-based vhosts during a single persistent
fefb8b844b6286bfc41bb2e0c4cc003b8e7d4ff2kess connection.</p>
5a58787efeb02a1c3f06569d019ad81fd2efa06end
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
d05d0eb4ae6d2a5e513fc3bf2555ce33da416634nd <h3><a name="absoluteURI" id="absoluteURI">Absolute URI</a></h3>
4b3a8afbfcea8b265d179a122bf40dfedd1ce280takashi
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung <p>If the URI from the request is an absolute URI, and its
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd hostname and port match the main server or one of the
5effc8b39fae5cd169d17f342bfc265705840014rbowen configured virtual hosts <em>and</em> match the address and
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen port to which the client sent the request, then the
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd scheme/hostname/port prefix is stripped off and the remaining
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd relative URI is served by the corresponding main server or
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd virtual host. If it does not match, then the URI remains
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd untouched and the request is taken to be a proxy request.</p>
5a58787efeb02a1c3f06569d019ad81fd2efa06end
<h3><a name="observations" id="observations">Observations</a></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 <code>_default_</code>
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>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="tips" id="tips">Tips</a></h2>
<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>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/vhosts/details.html" title="English">&nbsp;en&nbsp;</a> |
<a href="/ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
</div><div id="footer">
<p class="apache">Copyright 1999-2004 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>