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>The virtual host code was completely rewritten in
30 <strong>Apache 1.3</strong>. This document attempts to explain
31 exactly what Apache does when deciding what virtual host to
32 serve a hit from. With the help of the new
33 <directive module="core">NameVirtualHost</directive>
34 directive virtual host configuration should be a lot easier and
35 safer than with versions prior to 1.3.</p>
37 <p>If you just want to <cite>make it work</cite> without
38 understanding how, here are <a href="examples.html">some
43 <section id="configparsing"><title>Config File Parsing</title>
45 <p>There is a <em>main_server</em> which consists of all the
46 definitions appearing outside of
47 <code><VirtualHost></code> sections. There are virtual
48 servers, called <em>vhosts</em>, which are defined by
49 <directive type="section" module="core">VirtualHost</directive>
53 <directive module="mpm_common">Listen</directive>,
54 <directive module="core">ServerName</directive>,
55 <directive module="core">ServerPath</directive>,
56 and <directive module="core">ServerAlias</directive>
57 can appear anywhere within the definition of a server. However,
58 each appearance overrides the previous appearance (within that
61 <p>The default value of the <code>Listen</code> field for
62 main_server is 80. The main_server has no default
63 <code>ServerPath</code>, or <code>ServerAlias</code>. The
64 default <code>ServerName</code> is deduced from the server's IP
67 <p>The main_server Listen directive has two functions. One
68 function is to determine the default network port Apache will
69 bind to. The second function is to specify the port number
70 which is used in absolute URIs during redirects.</p>
72 <p>Unlike the main_server, vhost ports <em>do not</em> affect
73 what ports Apache listens for connections on.</p>
75 <p>Each address appearing in the <code>VirtualHost</code>
76 directive can have an optional port. If the port is unspecified
77 it defaults to the value of the main_server's most recent
78 <code>Listen</code> statement. The special port <code>*</code>
79 indicates a wildcard that matches any port. Collectively the
80 entire set of addresses (including multiple <code>A</code>
81 record results from DNS lookups) are called the vhost's
82 <em>address set</em>.</p>
84 <p>Unless a <directive module="core">NameVirtualHost</directive>
85 directive is used for a specific IP address the first vhost
86 with that address is treated as an IP-based vhost. The IP
87 address can also be the wildcard <code>*</code>.</p>
89 <p>If name-based vhosts should be used a
90 <code>NameVirtualHost</code> directive <em>must</em> appear
91 with the IP address set to be used for the name-based vhosts.
92 In other words, you must specify the IP address that holds the
93 hostname aliases (CNAMEs) for your name-based vhosts via a
94 <code>NameVirtualHost</code> directive in your configuration
97 <p>Multiple <code>NameVirtualHost</code> directives can be used
98 each with a set of <code>VirtualHost</code> directives but only
99 one <code>NameVirtualHost</code> directive should be used for
100 each specific IP:port pair.</p>
102 <p>The ordering of <code>NameVirtualHost</code> and
103 <code>VirtualHost</code> directives is not important which
104 makes the following two examples identical (only the order of
105 the <code>VirtualHost</code> directives for <em>one</em>
106 address set is important, see below):</p>
110 NameVirtualHost 111.22.33.44<br />
111 <VirtualHost 111.22.33.44><br />
114 </VirtualHost><br />
115 <VirtualHost 111.22.33.44><br />
118 </VirtualHost><br />
120 NameVirtualHost 111.22.33.55<br />
121 <VirtualHost 111.22.33.55><br />
124 </VirtualHost><br />
125 <VirtualHost 111.22.33.55><br />
131 <VirtualHost 111.22.33.44><br />
133 </VirtualHost><br />
134 <VirtualHost 111.22.33.55><br />
137 </VirtualHost><br />
138 <VirtualHost 111.22.33.44><br />
141 </VirtualHost><br />
142 <VirtualHost 111.22.33.55><br />
145 </VirtualHost><br />
147 NameVirtualHost 111.22.33.44<br />
148 NameVirtualHost 111.22.33.55<br />
154 <p>(To aid the readability of your configuration you should
155 prefer the left variant.)</p>
157 <p>After parsing the <code>VirtualHost</code> directive, the
158 vhost server is given a default <code>Listen</code> equal to the
159 port assigned to the first name in its <code>VirtualHost</code>
162 <p>The complete list of names in the <code>VirtualHost</code>
163 directive are treated just like a <code>ServerAlias</code> (but
164 are not overridden by any <code>ServerAlias</code> statement)
165 if all names resolve to the same address set. Note that
166 subsequent <code>Listen</code> statements for this vhost will not
167 affect the ports assigned in the address set.</p>
169 <p>During initialization a list for each IP address is
170 generated and inserted into an hash table. If the IP address is
171 used in a <code>NameVirtualHost</code> directive the list
172 contains all name-based vhosts for the given IP address. If
173 there are no vhosts defined for that address the
174 <code>NameVirtualHost</code> directive is ignored and an error
175 is logged. For an IP-based vhost the list in the hash table is
178 <p>Due to a fast hashing function the overhead of hashing an IP
179 address during a request is minimal and almost not existent.
180 Additionally the table is optimized for IP addresses which vary
181 in the last octet.</p>
183 <p>For every vhost various default values are set. In
187 <li>If a vhost has no <directive module="core">ServerAdmin</directive>,
188 <directive module="core">ResourceConfig</directive>,
189 <directive module="core">AccessConfig</directive>,
190 <directive module="core">Timeout</directive>,
191 <directive module="core">KeepAliveTimeout</directive>,
192 <directive module="core">KeepAlive</directive>,
193 <directive module="core">MaxKeepAliveRequests</directive>,
194 <directive module="core">ReceiveBufferSize</directive>,
195 or <directive module="core">SendBufferSize</directive>
196 directive then the respective value is inherited from the
197 main_server. (That is, inherited from whatever the final
198 setting of that value is in the main_server.)</li>
200 <li>The "lookup defaults" that define the default directory
201 permissions for a vhost are merged with those of the
202 main_server. This includes any per-directory configuration
203 information for any module.</li>
205 <li>The per-server configs for each module from the
206 main_server are merged into the vhost server.</li>
209 <p>Essentially, the main_server is treated as "defaults" or a
210 "base" on which to build each vhost. But the positioning of
211 these main_server definitions in the config file is largely
212 irrelevant -- the entire config of the main_server has been
213 parsed when this final merging occurs. So even if a main_server
214 definition appears after a vhost definition it might affect the
215 vhost definition.</p>
217 <p>If the main_server has no <code>ServerName</code> at this
218 point, then the hostname of the machine that <program>httpd</program>
219 is running on is used instead. We will call the <em>main_server address
220 set</em> those IP addresses returned by a DNS lookup on the
221 <code>ServerName</code> of the main_server.</p>
223 <p>For any undefined <code>ServerName</code> fields, a
224 name-based vhost defaults to the address given first in the
225 <code>VirtualHost</code> statement defining the vhost.</p>
227 <p>Any vhost that includes the magic <code>_default_</code>
228 wildcard is given the same <code>ServerName</code> as the
233 <section id="hostmatching"><title>Virtual Host Matching</title>
235 <p>The server determines which vhost to use for a request as
238 <section id="hashtable"><title>Hash table lookup</title>
240 <p>When the connection is first made by a client, the IP
241 address to which the client connected is looked up in the
242 internal IP hash table.</p>
244 <p>If the lookup fails (the IP address wasn't found) the
245 request is served from the <code>_default_</code> vhost if
246 there is such a vhost for the port to which the client sent the
247 request. If there is no matching <code>_default_</code> vhost
248 the request is served from the main_server.</p>
250 <p>If the IP address is not found in the hash table then the
251 match against the port number may also result in an entry
252 corresponding to a <code>NameVirtualHost *</code>, which is
253 subsequently handled like other name-based vhosts.</p>
255 <p>If the lookup succeeded (a corresponding list for the IP
256 address was found) the next step is to decide if we have to
257 deal with an IP-based or a name-base vhost.</p>
261 <section id="ipbased"><title>IP-based vhost</title>
263 <p>If the entry we found has an empty name list then we have
264 found an IP-based vhost, no further actions are performed and
265 the request is served from that vhost.</p>
269 <section id="namebased"><title>Name-based vhost</title>
271 <p>If the entry corresponds to a name-based vhost the name list
272 contains one or more vhost structures. This list contains the
273 vhosts in the same order as the <code>VirtualHost</code>
274 directives appear in the config file.</p>
276 <p>The first vhost on this list (the first vhost in the config
277 file with the specified IP address) has the highest priority
278 and catches any request to an unknown server name or a request
279 without a <code>Host:</code> header field.</p>
281 <p>If the client provided a <code>Host:</code> header field the
282 list is searched for a matching vhost and the first hit on a
283 <code>ServerName</code> or <code>ServerAlias</code> is taken
284 and the request is served from that vhost. A <code>Host:</code>
285 header field can contain a port number, but Apache always
286 matches against the real port to which the client sent the
289 <p>If the client submitted a HTTP/1.0 request without
290 <code>Host:</code> header field we don't know to what server
291 the client tried to connect and any existing
292 <code>ServerPath</code> is matched against the URI from the
293 request. The first matching path on the list is used and the
294 request is served from that vhost.</p>
296 <p>If no matching vhost could be found the request is served
297 from the first vhost with a matching port number that is on the
298 list for the IP to which the client connected (as already
299 mentioned before).</p>
303 <section id="persistent"><title>Persistent connections</title>
305 <p>The IP lookup described above is only done <em>once</em> for a
306 particular TCP/IP session while the name lookup is done on
307 <em>every</em> request during a KeepAlive/persistent
308 connection. In other words a client may request pages from
309 different name-based vhosts during a single persistent
314 <section id="absoluteURI"><title>Absolute URI</title>
316 <p>If the URI from the request is an absolute URI, and its
317 hostname and port match the main server or one of the
318 configured virtual hosts <em>and</em> match the address and
319 port to which the client sent the request, then the
320 scheme/hostname/port prefix is stripped off and the remaining
321 relative URI is served by the corresponding main server or
322 virtual host. If it does not match, then the URI remains
323 untouched and the request is taken to be a proxy request.</p>
326 <section id="observations"><title>Observations</title>
329 <li>A name-based vhost can never interfere with an IP-base
330 vhost and vice versa. IP-based vhosts can only be reached
331 through an IP address of its own address set and never
332 through any other address. The same applies to name-based
333 vhosts, they can only be reached through an IP address of the
334 corresponding address set which must be defined with a
335 <code>NameVirtualHost</code> directive.</li>
337 <li><code>ServerAlias</code> and <code>ServerPath</code>
338 checks are never performed for an IP-based vhost.</li>
340 <li>The order of name-/IP-based, the <code>_default_</code>
341 vhost and the <code>NameVirtualHost</code> directive within
342 the config file is not important. Only the ordering of
343 name-based vhosts for a specific address set is significant.
344 The one name-based vhosts that comes first in the
345 configuration file has the highest priority for its
346 corresponding address set.</li>
348 <li>For security reasons the port number given in a
349 <code>Host:</code> header field is never used during the
350 matching process. Apache always uses the real port to which
351 the client sent the request.</li>
353 <li>If a <code>ServerPath</code> directive exists which is a
354 prefix of another <code>ServerPath</code> directive that
355 appears later in the configuration file, then the former will
356 always be matched and the latter will never be matched. (That
357 is assuming that no <code>Host:</code> header field was
358 available to disambiguate the two.)</li>
360 <li>If two IP-based vhosts have an address in common, the
361 vhost appearing first in the config file is always matched.
362 Such a thing might happen inadvertently. The server will give
363 a warning in the error logfile when it detects this.</li>
365 <li>A <code>_default_</code> vhost catches a request only if
366 there is no other vhost with a matching IP address
367 <em>and</em> a matching port number for the request. The
368 request is only caught if the port number to which the client
369 sent the request matches the port number of your
370 <code>_default_</code> vhost which is your standard
371 <code>Listen</code> by default. A wildcard port can be
372 specified (<em>i.e.</em>, <code>_default_:*</code>) to catch
373 requests to any available port. This also applies to
374 <code>NameVirtualHost *</code> vhosts.</li>
376 <li>The main_server is only used to serve a request if the IP
377 address and port number to which the client connected is
378 unspecified and does not match any other vhost (including a
379 <code>_default_</code> vhost). In other words the main_server
380 only catches a request for an unspecified address/port
381 combination (unless there is a <code>_default_</code> vhost
382 which matches that port).</li>
384 <li>A <code>_default_</code> vhost or the main_server is
385 <em>never</em> matched for a request with an unknown or
386 missing <code>Host:</code> header field if the client
387 connected to an address (and port) which is used for
388 name-based vhosts, <em>e.g.</em>, in a
389 <code>NameVirtualHost</code> directive.</li>
391 <li>You should never specify DNS names in
392 <code>VirtualHost</code> directives because it will force
393 your server to rely on DNS to boot. Furthermore it poses a
394 security threat if you do not control the DNS for all the
395 domains listed. There's <a href="../dns-caveats.html">more
396 information</a> available on this and the next two
399 <li><code>ServerName</code> should always be set for each
400 vhost. Otherwise A DNS lookup is required for each
407 <section id="tips"><title>Tips</title>
409 <p>In addition to the tips on the <a
410 href="../dns-caveats.html#tips">DNS Issues</a> page, here are
411 some further tips:</p>
414 <li>Place all main_server definitions before any
415 <code>VirtualHost</code> definitions. (This is to aid the
416 readability of the configuration -- the post-config merging
417 process makes it non-obvious that definitions mixed in around
418 virtual hosts might affect all virtual hosts.)</li>
420 <li>Group corresponding <code>NameVirtualHost</code> and
421 <code>VirtualHost</code> definitions in your configuration to
422 ensure better readability.</li>
424 <li>Avoid <code>ServerPaths</code> which are prefixes of
425 other <code>ServerPaths</code>. If you cannot avoid this then
426 you have to ensure that the longer (more specific) prefix
427 vhost appears earlier in the configuration file than the
428 shorter (less specific) prefix (<em>i.e.</em>, "ServerPath
429 /abc" should appear after "ServerPath /abc/def").</li>