1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5
This file is generated from xml source: DO NOT EDIT
6
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8
<title>An In-Depth Discussion of Virtual Host Matching - Apache HTTP Server</title>
9
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
13
<body id="manual-page"><div id="page-header">
14
<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>
15
<p class="apache">Apache HTTP Server Version 2.2</p>
16
<img alt="" src="../images/feather.gif" /></div>
17
<div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
19
<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.2</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>An In-Depth Discussion of Virtual Host Matching</h1>
21
<p><span>Available Languages: </span><a href="../en/vhosts/details.html" title="English"> en </a> |
22
<a href="../fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a> |
23
<a href="../ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
27
<p>The virtual host code was completely rewritten in
28
<strong>Apache 1.3</strong>. This document attempts to explain
29
exactly what Apache does when deciding what virtual host to
30
serve a hit from. With the help of the new
31
<code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code>
32
directive virtual host configuration should be a lot easier and
33
safer than with versions prior to 1.3.</p>
35
<p>If you just want to <cite>make it work</cite> without
36
understanding how, here are <a href="examples.html">some
40
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#configparsing">Config File Parsing</a></li>
41
<li><img alt="" src="../images/down.gif" /> <a href="#hostmatching">Virtual Host Matching</a></li>
42
<li><img alt="" src="../images/down.gif" /> <a href="#tips">Tips</a></li>
44
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
46
<h2><a name="configparsing" id="configparsing">Config File Parsing</a></h2>
48
<p>There is a <em>main_server</em> which consists of all the
49
definitions appearing outside of
50
<code><VirtualHost></code> sections. There are virtual
51
servers, called <em>vhosts</em>, which are defined by
52
<code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>
56
<code class="directive"><a href="../mod/mpm_common.html#listen">Listen</a></code>,
57
<code class="directive"><a href="../mod/core.html#servername">ServerName</a></code>,
58
<code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>,
59
and <code class="directive"><a href="../mod/core.html#serveralias">ServerAlias</a></code>
60
can appear anywhere within the definition of a server. However,
61
each appearance overrides the previous appearance (within that
64
<p>The default value of the <code>Listen</code> field for
65
main_server is 80. The main_server has no default
66
<code>ServerPath</code>, or <code>ServerAlias</code>. The
67
default <code>ServerName</code> is deduced from the server's IP
70
<p>The main_server Listen directive has two functions. One
71
function is to determine the default network port Apache will
72
bind to. The second function is to specify the port number
73
which is used in absolute URIs during redirects.</p>
75
<p>Unlike the main_server, vhost ports <em>do not</em> affect
76
what ports Apache listens for connections on.</p>
78
<p>Each address appearing in the <code>VirtualHost</code>
79
directive can have an optional port. If the port is unspecified
80
it defaults to the value of the main_server's most recent
81
<code>Listen</code> statement. The special port <code>*</code>
82
indicates a wildcard that matches any port. Collectively the
83
entire set of addresses (including multiple <code>A</code>
84
record results from DNS lookups) are called the vhost's
85
<em>address set</em>.</p>
87
<p>Unless a <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code>
88
directive is used for a specific IP address the first vhost
89
with that address is treated as an IP-based vhost. The IP
90
address can also be the wildcard <code>*</code>.</p>
92
<p>If name-based vhosts should be used a
93
<code>NameVirtualHost</code> directive <em>must</em> appear
94
with the IP address set to be used for the name-based vhosts.
95
In other words, you must specify the IP address that holds the
96
hostname aliases (CNAMEs) for your name-based vhosts via a
97
<code>NameVirtualHost</code> directive in your configuration
100
<p>Multiple <code>NameVirtualHost</code> directives can be used
101
each with a set of <code>VirtualHost</code> directives but only
102
one <code>NameVirtualHost</code> directive should be used for
103
each specific IP:port pair.</p>
105
<p>The ordering of <code>NameVirtualHost</code> and
106
<code>VirtualHost</code> directives is not important which
107
makes the following two examples identical (only the order of
108
the <code>VirtualHost</code> directives for <em>one</em>
109
address set is important, see below):</p>
112
<td><div class="example"><p><code>
113
NameVirtualHost 111.22.33.44<br />
114
<VirtualHost 111.22.33.44><br />
117
</VirtualHost><br />
118
<VirtualHost 111.22.33.44><br />
121
</VirtualHost><br />
123
NameVirtualHost 111.22.33.55<br />
124
<VirtualHost 111.22.33.55><br />
127
</VirtualHost><br />
128
<VirtualHost 111.22.33.55><br />
132
</code></p></div></td>
133
<td><div class="example"><p><code>
134
<VirtualHost 111.22.33.44><br />
136
</VirtualHost><br />
137
<VirtualHost 111.22.33.55><br />
140
</VirtualHost><br />
141
<VirtualHost 111.22.33.44><br />
144
</VirtualHost><br />
145
<VirtualHost 111.22.33.55><br />
148
</VirtualHost><br />
150
NameVirtualHost 111.22.33.44<br />
151
NameVirtualHost 111.22.33.55<br />
153
</code></p></div></td>
157
<p>(To aid the readability of your configuration you should
158
prefer the left variant.)</p>
160
<p>After parsing the <code>VirtualHost</code> directive, the
161
vhost server is given a default <code>Listen</code> equal to the
162
port assigned to the first name in its <code>VirtualHost</code>
165
<p>The complete list of names in the <code>VirtualHost</code>
166
directive are treated just like a <code>ServerAlias</code> (but
167
are not overridden by any <code>ServerAlias</code> statement)
168
if all names resolve to the same address set. Note that
169
subsequent <code>Listen</code> statements for this vhost will not
170
affect the ports assigned in the address set.</p>
172
<p>During initialization a list for each IP address is
173
generated and inserted into an hash table. If the IP address is
174
used in a <code>NameVirtualHost</code> directive the list
175
contains all name-based vhosts for the given IP address. If
176
there are no vhosts defined for that address the
177
<code>NameVirtualHost</code> directive is ignored and an error
178
is logged. For an IP-based vhost the list in the hash table is
181
<p>Due to a fast hashing function the overhead of hashing an IP
182
address during a request is minimal and almost not existent.
183
Additionally the table is optimized for IP addresses which vary
184
in the last octet.</p>
186
<p>For every vhost various default values are set. In
190
<li>If a vhost has no <code class="directive"><a href="../mod/core.html#serveradmin">ServerAdmin</a></code>,
191
<code class="directive"><a href="../mod/core.html#resourceconfig">ResourceConfig</a></code>,
192
<code class="directive"><a href="../mod/core.html#accessconfig">AccessConfig</a></code>,
193
<code class="directive"><a href="../mod/core.html#timeout">Timeout</a></code>,
194
<code class="directive"><a href="../mod/core.html#keepalivetimeout">KeepAliveTimeout</a></code>,
195
<code class="directive"><a href="../mod/core.html#keepalive">KeepAlive</a></code>,
196
<code class="directive"><a href="../mod/core.html#maxkeepaliverequests">MaxKeepAliveRequests</a></code>,
197
<code class="directive"><a href="../mod/core.html#receivebuffersize">ReceiveBufferSize</a></code>,
198
or <code class="directive"><a href="../mod/core.html#sendbuffersize">SendBufferSize</a></code>
199
directive then the respective value is inherited from the
200
main_server. (That is, inherited from whatever the final
201
setting of that value is in the main_server.)</li>
203
<li>The "lookup defaults" that define the default directory
204
permissions for a vhost are merged with those of the
205
main_server. This includes any per-directory configuration
206
information for any module.</li>
208
<li>The per-server configs for each module from the
209
main_server are merged into the vhost server.</li>
212
<p>Essentially, the main_server is treated as "defaults" or a
213
"base" on which to build each vhost. But the positioning of
214
these main_server definitions in the config file is largely
215
irrelevant -- the entire config of the main_server has been
216
parsed when this final merging occurs. So even if a main_server
217
definition appears after a vhost definition it might affect the
218
vhost definition.</p>
220
<p>If the main_server has no <code>ServerName</code> at this
221
point, then the hostname of the machine that <code class="program"><a href="../programs/httpd.html">httpd</a></code>
222
is running on is used instead. We will call the <em>main_server address
223
set</em> those IP addresses returned by a DNS lookup on the
224
<code>ServerName</code> of the main_server.</p>
226
<p>For any undefined <code>ServerName</code> fields, a
227
name-based vhost defaults to the address given first in the
228
<code>VirtualHost</code> statement defining the vhost.</p>
230
<p>Any vhost that includes the magic <code>_default_</code>
231
wildcard is given the same <code>ServerName</code> as the
234
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
235
<div class="section">
236
<h2><a name="hostmatching" id="hostmatching">Virtual Host Matching</a></h2>
238
<p>The server determines which vhost to use for a request as
241
<h3><a name="hashtable" id="hashtable">Hash table lookup</a></h3>
243
<p>When the connection is first made by a client, the IP
244
address to which the client connected is looked up in the
245
internal IP hash table.</p>
247
<p>If the lookup fails (the IP address wasn't found) the
248
request is served from the <code>_default_</code> vhost if
249
there is such a vhost for the port to which the client sent the
250
request. If there is no matching <code>_default_</code> vhost
251
the request is served from the main_server.</p>
253
<p>If the IP address is not found in the hash table then the
254
match against the port number may also result in an entry
255
corresponding to a <code>NameVirtualHost *</code>, which is
256
subsequently handled like other name-based vhosts.</p>
258
<p>If the lookup succeeded (a corresponding list for the IP
259
address was found) the next step is to decide if we have to
260
deal with an IP-based or a name-base vhost.</p>
264
<h3><a name="ipbased" id="ipbased">IP-based vhost</a></h3>
266
<p>If the entry we found has an empty name list then we have
267
found an IP-based vhost, no further actions are performed and
268
the request is served from that vhost.</p>
272
<h3><a name="namebased" id="namebased">Name-based vhost</a></h3>
274
<p>If the entry corresponds to a name-based vhost the name list
275
contains one or more vhost structures. This list contains the
276
vhosts in the same order as the <code>VirtualHost</code>
277
directives appear in the config file.</p>
279
<p>The first vhost on this list (the first vhost in the config
280
file with the specified IP address) has the highest priority
281
and catches any request to an unknown server name or a request
282
without a <code>Host:</code> header field.</p>
284
<p>If the client provided a <code>Host:</code> header field the
285
list is searched for a matching vhost and the first hit on a
286
<code>ServerName</code> or <code>ServerAlias</code> is taken
287
and the request is served from that vhost. A <code>Host:</code>
288
header field can contain a port number, but Apache always
289
matches against the real port to which the client sent the
292
<p>If the client submitted a HTTP/1.0 request without
293
<code>Host:</code> header field we don't know to what server
294
the client tried to connect and any existing
295
<code>ServerPath</code> is matched against the URI from the
296
request. The first matching path on the list is used and the
297
request is served from that vhost.</p>
299
<p>If no matching vhost could be found the request is served
300
from the first vhost with a matching port number that is on the
301
list for the IP to which the client connected (as already
302
mentioned before).</p>
306
<h3><a name="persistent" id="persistent">Persistent connections</a></h3>
308
<p>The IP lookup described above is only done <em>once</em> for a
309
particular TCP/IP session while the name lookup is done on
310
<em>every</em> request during a KeepAlive/persistent
311
connection. In other words a client may request pages from
312
different name-based vhosts during a single persistent
317
<h3><a name="absoluteURI" id="absoluteURI">Absolute URI</a></h3>
319
<p>If the URI from the request is an absolute URI, and its
320
hostname and port match the main server or one of the
321
configured virtual hosts <em>and</em> match the address and
322
port to which the client sent the request, then the
323
scheme/hostname/port prefix is stripped off and the remaining
324
relative URI is served by the corresponding main server or
325
virtual host. If it does not match, then the URI remains
326
untouched and the request is taken to be a proxy request.</p>
329
<h3><a name="observations" id="observations">Observations</a></h3>
332
<li>A name-based vhost can never interfere with an IP-base
333
vhost and vice versa. IP-based vhosts can only be reached
334
through an IP address of its own address set and never
335
through any other address. The same applies to name-based
336
vhosts, they can only be reached through an IP address of the
337
corresponding address set which must be defined with a
338
<code>NameVirtualHost</code> directive.</li>
340
<li><code>ServerAlias</code> and <code>ServerPath</code>
341
checks are never performed for an IP-based vhost.</li>
343
<li>The order of name-/IP-based, the <code>_default_</code>
344
vhost and the <code>NameVirtualHost</code> directive within
345
the config file is not important. Only the ordering of
346
name-based vhosts for a specific address set is significant.
347
The one name-based vhosts that comes first in the
348
configuration file has the highest priority for its
349
corresponding address set.</li>
351
<li>For security reasons the port number given in a
352
<code>Host:</code> header field is never used during the
353
matching process. Apache always uses the real port to which
354
the client sent the request.</li>
356
<li>If a <code>ServerPath</code> directive exists which is a
357
prefix of another <code>ServerPath</code> directive that
358
appears later in the configuration file, then the former will
359
always be matched and the latter will never be matched. (That
360
is assuming that no <code>Host:</code> header field was
361
available to disambiguate the two.)</li>
363
<li>If two IP-based vhosts have an address in common, the
364
vhost appearing first in the config file is always matched.
365
Such a thing might happen inadvertently. The server will give
366
a warning in the error logfile when it detects this.</li>
368
<li>A <code>_default_</code> vhost catches a request only if
369
there is no other vhost with a matching IP address
370
<em>and</em> a matching port number for the request. The
371
request is only caught if the port number to which the client
372
sent the request matches the port number of your
373
<code>_default_</code> vhost which is your standard
374
<code>Listen</code> by default. A wildcard port can be
375
specified (<em>i.e.</em>, <code>_default_:*</code>) to catch
376
requests to any available port. This also applies to
377
<code>NameVirtualHost *</code> vhosts.</li>
379
<li>The main_server is only used to serve a request if the IP
380
address and port number to which the client connected is
381
unspecified and does not match any other vhost (including a
382
<code>_default_</code> vhost). In other words the main_server
383
only catches a request for an unspecified address/port
384
combination (unless there is a <code>_default_</code> vhost
385
which matches that port).</li>
387
<li>A <code>_default_</code> vhost or the main_server is
388
<em>never</em> matched for a request with an unknown or
389
missing <code>Host:</code> header field if the client
390
connected to an address (and port) which is used for
391
name-based vhosts, <em>e.g.</em>, in a
392
<code>NameVirtualHost</code> directive.</li>
394
<li>You should never specify DNS names in
395
<code>VirtualHost</code> directives because it will force
396
your server to rely on DNS to boot. Furthermore it poses a
397
security threat if you do not control the DNS for all the
398
domains listed. There's <a href="../dns-caveats.html">more
399
information</a> available on this and the next two
402
<li><code>ServerName</code> should always be set for each
403
vhost. Otherwise A DNS lookup is required for each
408
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
409
<div class="section">
410
<h2><a name="tips" id="tips">Tips</a></h2>
412
<p>In addition to the tips on the <a href="../dns-caveats.html#tips">DNS Issues</a> page, here are
413
some further tips:</p>
416
<li>Place all main_server definitions before any
417
<code>VirtualHost</code> definitions. (This is to aid the
418
readability of the configuration -- the post-config merging
419
process makes it non-obvious that definitions mixed in around
420
virtual hosts might affect all virtual hosts.)</li>
422
<li>Group corresponding <code>NameVirtualHost</code> and
423
<code>VirtualHost</code> definitions in your configuration to
424
ensure better readability.</li>
426
<li>Avoid <code>ServerPaths</code> which are prefixes of
427
other <code>ServerPaths</code>. If you cannot avoid this then
428
you have to ensure that the longer (more specific) prefix
429
vhost appears earlier in the configuration file than the
430
shorter (less specific) prefix (<em>i.e.</em>, "ServerPath
431
/abc" should appear after "ServerPath /abc/def").</li>
435
<div class="bottomlang">
436
<p><span>Available Languages: </span><a href="../en/vhosts/details.html" title="English"> en </a> |
437
<a href="../fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a> |
438
<a href="../ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
439
</div><div id="footer">
440
<p class="apache">Copyright 2006 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>
441
<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>
b'\\ No newline at end of file'