📄 details.html.en
字号:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
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" />
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
<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.0</p>
<img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
<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.0</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="../ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
</div>
<p>The virtual host code was completely rewritten in
<strong>Apache 1.3</strong>. This document attempts to explain
exactly what Apache does when deciding what virtual host to
serve a hit from. With the help of the new
<code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code>
directive virtual host configuration should be a lot easier and
safer than with versions prior to 1.3.</p>
<p>If you just want to <cite>make it work</cite> without
understanding how, here are <a href="examples.html">some
examples</a>.</p>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#configparsing">Config File Parsing</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#hostmatching">Virtual Host Matching</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#tips">Tips</a></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="configparsing" id="configparsing">Config File Parsing</a></h2>
<p>There is a <em>main_server</em> which consists of all the
definitions appearing outside of
<code><VirtualHost></code> sections. There are virtual
servers, called <em>vhosts</em>, which are defined by
<code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>
sections.</p>
<p>The directives
<code class="directive"><a href="../mod/mpm_common.html#listen">Listen</a></code>,
<code class="directive"><a href="../mod/core.html#servername">ServerName</a></code>,
<code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>,
and <code class="directive"><a href="../mod/core.html#serveralias">ServerAlias</a></code>
can appear anywhere within the definition of a server. However,
each appearance overrides the previous appearance (within that
server).</p>
<p>The default value of the <code>Listen</code> field for
main_server is 80. The main_server has no default
<code>ServerPath</code>, or <code>ServerAlias</code>. The
default <code>ServerName</code> is deduced from the server's IP
address.</p>
<p>The main_server Listen directive has two functions. One
function is to determine the default network port Apache will
bind to. The second function is to specify the port number
which is used in absolute URIs during redirects.</p>
<p>Unlike the main_server, vhost ports <em>do not</em> affect
what ports Apache listens for connections on.</p>
<p>Each address appearing in the <code>VirtualHost</code>
directive can have an optional port. If the port is unspecified
it defaults to the value of the main_server's most recent
<code>Listen</code> statement. The special port <code>*</code>
indicates a wildcard that matches any port. Collectively the
entire set of addresses (including multiple <code>A</code>
record results from DNS lookups) are called the vhost's
<em>address set</em>.</p>
<p>Unless a <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code>
directive is used for a specific IP address the first vhost
with that address is treated as an IP-based vhost. The IP
address can also be the wildcard <code>*</code>.</p>
<p>If name-based vhosts should be used a
<code>NameVirtualHost</code> directive <em>must</em> appear
with the IP address set to be used for the name-based vhosts.
In other words, you must specify the IP address that holds the
hostname aliases (CNAMEs) for your name-based vhosts via a
<code>NameVirtualHost</code> directive in your configuration
file.</p>
<p>Multiple <code>NameVirtualHost</code> directives can be used
each with a set of <code>VirtualHost</code> directives but only
one <code>NameVirtualHost</code> directive should be used for
each specific IP:port pair.</p>
<p>The ordering of <code>NameVirtualHost</code> and
<code>VirtualHost</code> directives is not important which
makes the following two examples identical (only the order of
the <code>VirtualHost</code> directives for <em>one</em>
address set is important, see below):</p>
<table><tr>
<td><div class="example"><p><code>
NameVirtualHost 111.22.33.44<br />
<VirtualHost 111.22.33.44><br />
# server A<br />
...<br />
</VirtualHost><br />
<VirtualHost 111.22.33.44><br />
# server B<br />
...<br />
</VirtualHost><br />
<br />
NameVirtualHost 111.22.33.55<br />
<VirtualHost 111.22.33.55><br />
# server C<br />
...<br />
</VirtualHost><br />
<VirtualHost 111.22.33.55><br />
# server D<br />
...<br />
</VirtualHost>
</code></p></div></td>
<td><div class="example"><p><code>
<VirtualHost 111.22.33.44><br />
# server A<br />
</VirtualHost><br />
<VirtualHost 111.22.33.55><br />
# server C<br />
...<br />
</VirtualHost><br />
<VirtualHost 111.22.33.44><br />
# server B<br />
...<br />
</VirtualHost><br />
<VirtualHost 111.22.33.55><br />
# server D<br />
...<br />
</VirtualHost><br />
<br />
NameVirtualHost 111.22.33.44<br />
NameVirtualHost 111.22.33.55<br />
<br />
</code></p></div></td>
</tr></table>
<p>(To aid the readability of your configuration you should
prefer the left variant.)</p>
<p>After parsing the <code>VirtualHost</code> directive, the
vhost server is given a default <code>Listen</code> equal to the
port assigned to the first name in its <code>VirtualHost</code>
directive.</p>
<p>The complete list of names in the <code>VirtualHost</code>
directive are treated just like a <code>ServerAlias</code> (but
are not overridden by any <code>ServerAlias</code> statement)
if all names resolve to the same address set. Note that
subsequent <code>Listen</code> statements for this vhost will not
affect the ports assigned in the address set.</p>
<p>During initialization a list for each IP address is
generated and inserted into an hash table. If the IP address is
used in a <code>NameVirtualHost</code> directive the list
contains all name-based vhosts for the given IP address. If
there are no vhosts defined for that address the
<code>NameVirtualHost</code> directive is ignored and an error
is logged. For an IP-based vhost the list in the hash table is
empty.</p>
<p>Due to a fast hashing function the overhead of hashing an IP
address during a request is minimal and almost not existent.
Additionally the table is optimized for IP addresses which vary
in the last octet.</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#resourceconfig">ResourceConfig</a></code>,
<code class="directive"><a href="../mod/core.html#accessconfig">AccessConfig</a></code>,
<code class="directive"><a href="../mod/core.html#timeout">Timeout</a></code>,
<code class="directive"><a href="../mod/core.html#keepalivetimeout">KeepAliveTimeout</a></code>,
<code class="directive"><a href="../mod/core.html#keepalive">KeepAlive</a></code>,
<code class="directive"><a href="../mod/core.html#maxkeepaliverequests">MaxKeepAliveRequests</a></code>,
<code class="directive"><a href="../mod/core.html#receivebuffersize">ReceiveBufferSize</a></code>,
or <code class="directive"><a href="../mod/core.html#sendbuffersize">SendBufferSize</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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -