1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="details.xml.meta">
24 <parentdocument href="./">Virtual Hosts</parentdocument>
25 <title>An In-Depth Discussion of Virtual Host Matching</title>
29 <p>This document attempts to explain
30 exactly what Apache HTTP Server does when deciding what virtual host to
31 serve a request from.</p>
33 <p>Most users should read about <a href="name-based.html#namevip">
34 Name-based vs. IP-based Virtual Hosts</a> to decide which type they
35 want to use, then read more about <a href="name-based.html">name-based</a>
36 or <a href="ip-based.html">IP-based</a> virtualhosts, and then see
37 <a href="examples.html">some examples</a>.</p>
39 <p>If you want to understand all the details, then you can
40 come back to this page.</p>
44 <seealso><a href="ip-based.html">IP-based Virtual Host Support</a></seealso>
45 <seealso><a href="name-based.html">Name-based Virtual Hosts Support</a></seealso>
46 <seealso><a href="examples.html">Virtual Host examples for common setups</a></seealso>
47 <seealso><a href="mass.html">Dynamically configured mass virtual hosting</a></seealso>
50 <section id="configparsing"><title>Configuration File</title>
52 <p>There is a <em>main server</em> which consists of all the
53 definitions appearing outside of
54 <code><VirtualHost></code> sections.</p>
57 servers, called <em>vhosts</em>, which are defined by
58 <directive type="section" module="core">VirtualHost</directive>
61 <p>Each <code>VirtualHost</code> directive includes one
62 or more addresses and optional ports.</p>
64 <p>Hostnames can be used in place of IP addresses in a virtual
65 host definition, but they are resolved at startup and if any name
66 resolutions fail, those virtual host definitions are ignored.
67 This is, therefore, not recommended.</p>
69 <p>The address can be specified as
70 <code>*</code>, which will match a request if no
71 other vhost has the explicit address on which the request was
74 <p>The address appearing in the <code>VirtualHost</code>
75 directive can have an optional port. If the port is unspecified,
76 it is treated as a wildcard port, which can also be indicated
77 explicitly using <code>*</code>.
78 The wildcard port matches any port.</p>
80 <p>(Port numbers specified in the <code>VirtualHost</code> directive do
81 not influence what port numbers Apache will listen on, they only control
82 which <code>VirtualHost</code> will be selected to handle a request.
83 Use the <directive module="mpm_common">Listen</directive> directive to
84 control the addresses and ports on which the server listens.)
88 entire set of addresses (including multiple
89 results from DNS lookups) are called the vhost's
90 <em>address set</em>.</p>
92 <p>Apache automatically discriminates on the
93 basis of the HTTP <code>Host</code> header supplied by the client
94 whenever the most specific match for an IP address and port combination
95 is listed in multiple virtual hosts.</p>
98 <directive module="core">ServerName</directive> directive
99 may appear anywhere within the definition of a server. However,
100 each appearance overrides the previous appearance (within that
101 server). If no <code>ServerName</code> is specified, the server
102 attempts to deduce it from the server's IP address.</p>
104 <p>The first name-based vhost in the configuration file for a
105 given IP:port pair is significant because it is used for all
106 requests received on that address and port for which no other
107 vhost for that IP:port pair has a matching ServerName or
108 ServerAlias. It is also used for all SSL connections if the
109 server does not support <glossary
110 ref="servernameindication">Server Name Indication</glossary>.</p>
112 <p>The complete list of names in the <code>VirtualHost</code>
113 directive are treated just like a (non wildcard) <code>ServerAlias</code>
114 (but are not overridden by any <code>ServerAlias</code> statement).</p>
116 <p>For every vhost various default values are set. In
120 <li>If a vhost has no <directive module="core">ServerAdmin</directive>,
121 <directive module="core">Timeout</directive>,
122 <directive module="core">KeepAliveTimeout</directive>,
123 <directive module="core">KeepAlive</directive>,
124 <directive module="core">MaxKeepAliveRequests</directive>,
125 <directive module="mpm_common">ReceiveBufferSize</directive>,
126 or <directive module="mpm_common">SendBufferSize</directive>
127 directive then the respective value is inherited from the
128 main server. (That is, inherited from whatever the final
129 setting of that value is in the main server.)</li>
131 <li>The "lookup defaults" that define the default directory
132 permissions for a vhost are merged with those of the
133 main server. This includes any per-directory configuration
134 information for any module.</li>
136 <li>The per-server configs for each module from the
137 main server are merged into the vhost server.</li>
140 <p>Essentially, the main server is treated as "defaults" or a
141 "base" on which to build each vhost. But the positioning of
142 these main server definitions in the config file is largely
143 irrelevant -- the entire config of the main server has been
144 parsed when this final merging occurs. So even if a main server
145 definition appears after a vhost definition it might affect the
146 vhost definition.</p>
148 <p>If the main server has no <code>ServerName</code> at this
149 point, then the hostname of the machine that <program>httpd</program>
150 is running on is used instead. We will call the <em>main server address
151 set</em> those IP addresses returned by a DNS lookup on the
152 <code>ServerName</code> of the main server.</p>
154 <p>For any undefined <code>ServerName</code> fields, a
155 name-based vhost defaults to the address given first in the
156 <code>VirtualHost</code> statement defining the vhost.</p>
158 <p>Any vhost that includes the magic <code>_default_</code>
159 wildcard is given the same <code>ServerName</code> as the
164 <section id="hostmatching"><title>Virtual Host Matching</title>
166 <p>The server determines which vhost to use for a request as
169 <section id="hashtable"><title>IP address lookup</title>
171 <p>When the connection is first received on some address and port,
172 the server looks for all the <code>VirtualHost</code> definitions
173 that have the same IP address and port.</p>
175 <p>If there are no exact matches for the address and port, then
176 wildcard (<code>*</code>) matches are considered.</p>
178 <p>If no matches are found, the request is served by the
181 <p>If there are <code>VirtualHost</code> definitions for
182 the IP address, the next step is to decide if we have to
183 deal with an IP-based or a name-based vhost.</p>
187 <section id="ipbased"><title>IP-based vhost</title>
189 <p>If there is exactly one <code>VirtualHost</code> directive
190 listing the IP address and port combination that was determined
191 to be the best match, no further actions are performed and
192 the request is served from the matching vhost.</p>
196 <section id="namebased"><title>Name-based vhost</title>
198 <p>If there are multiple <code>VirtualHost</code> directives listing
199 the IP address and port combination that was determined to be the
200 best match, the "list" in the remaining steps refers to the list of vhosts
201 that matched, in the order they were in the configuration file.</p>
203 <p>If the connection is using SSL, the server supports <glossary
204 ref="servernameindication">Server Name Indication</glossary>, and
205 the SSL client handshake includes the TLS extension with the
206 requested hostname, then that hostname is used below just like the
207 <code>Host:</code> header would be used on a non-SSL connection.
208 Otherwise, the first name-based vhost whose address matched is
209 used for SSL connections. This is significant because the
210 vhost determines which certificate the server will use for the
213 <p>If the request contains a <code>Host:</code> header field, the
214 list is searched for the first vhost with a matching
215 <code>ServerName</code> or <code>ServerAlias</code>, and the
216 request is served from that vhost. A <code>Host:</code> header
217 field can contain a port number, but Apache always ignores it and
218 matches against the real port to which the client sent the
221 <p>The first vhost in the config
222 file with the specified IP address has the highest priority
223 and catches any request to an unknown server name, or a request
224 without a <code>Host:</code> header field (such as a HTTP/1.0
229 <section id="persistent"><title>Persistent connections</title>
231 <p>The <em>IP lookup</em> described above is only done <em>once</em> for a
232 particular TCP/IP session while the <em>name lookup</em> is done on
233 <em>every</em> request during a KeepAlive/persistent
234 connection. In other words, a client may request pages from
235 different name-based vhosts during a single persistent
240 <section id="absoluteURI"><title>Absolute URI</title>
242 <p>If the URI from the request is an absolute URI, and its
243 hostname and port match the main server or one of the
244 configured virtual hosts <em>and</em> match the address and
245 port to which the client sent the request, then the
246 scheme/hostname/port prefix is stripped off and the remaining
247 relative URI is served by the corresponding main server or
248 virtual host. If it does not match, then the URI remains
249 untouched and the request is taken to be a proxy request.</p>
252 <section id="observations"><title>Observations</title>
255 <li>Name-based virtual hosting is a process applied after
256 the server has selected the best matching IP-based virtual
259 <li>If you don't care what IP address the client has connected to, use a
260 "*" as the address of every virtual host, and name-based virtual hosting
261 is applied across all configured virtual hosts.</li>
263 <li><code>ServerName</code> and <code>ServerAlias</code>
264 checks are never performed for an IP-based vhost.</li>
266 <li>Only the ordering of
267 name-based vhosts for a specific address set is significant.
268 The one name-based vhosts that comes first in the
269 configuration file has the highest priority for its
270 corresponding address set.</li>
272 <li>Any port in the <code>Host:</code> header field is never used during the
273 matching process. Apache always uses the real port to which
274 the client sent the request.</li>
276 <li>If two vhosts have an address in common, those common addresses
277 act as name-based virtual hosts implicitly. This is new behavior as of
280 <li>The main server is only used to serve a request if the IP
281 address and port number to which the client connected
282 does not match any vhost (including a
283 <code>*</code> vhost). In other words, the main server
284 only catches a request for an unspecified address/port
285 combination (unless there is a <code>_default_</code> vhost
286 which matches that port).</li>
288 <li>You should never specify DNS names in
289 <code>VirtualHost</code> directives because it will force
290 your server to rely on DNS to boot. Furthermore it poses a
291 security threat if you do not control the DNS for all the
292 domains listed. There's <a href="../dns-caveats.html">more
293 information</a> available on this and the next two
296 <li><code>ServerName</code> should always be set for each
297 vhost. Otherwise a DNS lookup is required for each
304 <section id="tips"><title>Tips</title>
306 <p>In addition to the tips on the <a
307 href="../dns-caveats.html#tips">DNS Issues</a> page, here are
308 some further tips:</p>
311 <li>Place all main server definitions before any
312 <code>VirtualHost</code> definitions. (This is to aid the
313 readability of the configuration -- the post-config merging
314 process makes it non-obvious that definitions mixed in around
315 virtual hosts might affect all virtual hosts.)</li>