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>For every vhost various default values are set. In
116 <li>If a vhost has no <directive module="core">ServerAdmin</directive>,
117 <directive module="core">Timeout</directive>,
118 <directive module="core">KeepAliveTimeout</directive>,
119 <directive module="core">KeepAlive</directive>,
120 <directive module="core">MaxKeepAliveRequests</directive>,
121 <directive module="mpm_common">ReceiveBufferSize</directive>,
122 or <directive module="mpm_common">SendBufferSize</directive>
123 directive then the respective value is inherited from the
124 main server. (That is, inherited from whatever the final
125 setting of that value is in the main server.)</li>
127 <li>The "lookup defaults" that define the default directory
128 permissions for a vhost are merged with those of the
129 main server. This includes any per-directory configuration
130 information for any module.</li>
132 <li>The per-server configs for each module from the
133 main server are merged into the vhost server.</li>
136 <p>Essentially, the main server is treated as "defaults" or a
137 "base" on which to build each vhost. But the positioning of
138 these main server definitions in the config file is largely
139 irrelevant -- the entire config of the main server has been
140 parsed when this final merging occurs. So even if a main server
141 definition appears after a vhost definition it might affect the
142 vhost definition.</p>
144 <p>If the main server has no <code>ServerName</code> at this
145 point, then the hostname of the machine that <program>httpd</program>
146 is running on is used instead. We will call the <em>main server address
147 set</em> those IP addresses returned by a DNS lookup on the
148 <code>ServerName</code> of the main server.</p>
150 <p>For any undefined <code>ServerName</code> fields, a
151 name-based vhost defaults to the address given first in the
152 <code>VirtualHost</code> statement defining the vhost.</p>
154 <p>Any vhost that includes the magic <code>_default_</code>
155 wildcard is given the same <code>ServerName</code> as the
160 <section id="hostmatching"><title>Virtual Host Matching</title>
162 <p>The server determines which vhost to use for a request as
165 <section id="hashtable"><title>IP address lookup</title>
167 <p>When the connection is first received on some address and port,
168 the server looks for all the <code>VirtualHost</code> definitions
169 that have the same IP address and port.</p>
171 <p>If there are no exact matches for the address and port, then
172 wildcard (<code>*</code>) matches are considered.</p>
174 <p>If no matches are found, the request is served by the
177 <p>If there are <code>VirtualHost</code> definitions for
178 the IP address, the next step is to decide if we have to
179 deal with an IP-based or a name-based vhost.</p>
183 <section id="ipbased"><title>IP-based vhost</title>
185 <p>If there is exactly one <code>VirtualHost</code> directive
186 listing the IP address and port combination that was determined
187 to be the best match, no further actions are performed and
188 the request is served from the matching vhost.</p>
192 <section id="namebased"><title>Name-based vhost</title>
194 <p>If there are multiple <code>VirtualHost</code> directives listing
195 the IP address and port combination that was determined to be the
196 best match, the "list" in the remaining steps refers to the list of vhosts
197 that matched, in the order they were in the configuration file.</p>
199 <p>If the connection is using SSL, the server supports <glossary
200 ref="servernameindication">Server Name Indication</glossary>, and
201 the SSL client handshake includes the TLS extension with the
202 requested hostname, then that hostname is used below just like the
203 <code>Host:</code> header would be used on a non-SSL connection.
204 Otherwise, the first name-based vhost whose address matched is
205 used for SSL connections. This is significant because the
206 vhost determines which certificate the server will use for the
209 <p>If the request contains a <code>Host:</code> header field, the
210 list is searched for the first vhost with a matching
211 <code>ServerName</code> or <code>ServerAlias</code>, and the
212 request is served from that vhost. A <code>Host:</code> header
213 field can contain a port number, but Apache always ignores it and
214 matches against the real port to which the client sent the
217 <p>The first vhost in the config
218 file with the specified IP address has the highest priority
219 and catches any request to an unknown server name, or a request
220 without a <code>Host:</code> header field (such as a HTTP/1.0
225 <section id="persistent"><title>Persistent connections</title>
227 <p>The <em>IP lookup</em> described above is only done <em>once</em> for a
228 particular TCP/IP session while the <em>name lookup</em> is done on
229 <em>every</em> request during a KeepAlive/persistent
230 connection. In other words, a client may request pages from
231 different name-based vhosts during a single persistent
236 <section id="absoluteURI"><title>Absolute URI</title>
238 <p>If the URI from the request is an absolute URI, and its
239 hostname and port match the main server or one of the
240 configured virtual hosts <em>and</em> match the address and
241 port to which the client sent the request, then the
242 scheme/hostname/port prefix is stripped off and the remaining
243 relative URI is served by the corresponding main server or
244 virtual host. If it does not match, then the URI remains
245 untouched and the request is taken to be a proxy request.</p>
248 <section id="observations"><title>Observations</title>
251 <li>Name-based virtual hosting is a process applied after
252 the server has selected the best matching IP-based virtual
255 <li>If you don't care what IP address the client has connected to, use a
256 "*" as the address of every virtual host, and name-based virtual hosting
257 is applied across all configured virtual hosts.</li>
259 <li><code>ServerName</code> and <code>ServerAlias</code>
260 checks are never performed for an IP-based vhost.</li>
262 <li>Only the ordering of
263 name-based vhosts for a specific address set is significant.
264 The one name-based vhosts that comes first in the
265 configuration file has the highest priority for its
266 corresponding address set.</li>
268 <li>Any port in the <code>Host:</code> header field is never used during the
269 matching process. Apache always uses the real port to which
270 the client sent the request.</li>
272 <li>If two vhosts have an address in common, those common addresses
273 act as name-based virtual hosts implicitly. This is new behavior as of
276 <li>The main server is only used to serve a request if the IP
277 address and port number to which the client connected
278 does not match any vhost (including a
279 <code>*</code> vhost). In other words, the main server
280 only catches a request for an unspecified address/port
281 combination (unless there is a <code>_default_</code> vhost
282 which matches that port).</li>
284 <li>You should never specify DNS names in
285 <code>VirtualHost</code> directives because it will force
286 your server to rely on DNS to boot. Furthermore it poses a
287 security threat if you do not control the DNS for all the
288 domains listed. There's <a href="../dns-caveats.html">more
289 information</a> available on this and the next two
292 <li><code>ServerName</code> should always be set for each
293 vhost. Otherwise A DNS lookup is required for each
300 <section id="tips"><title>Tips</title>
302 <p>In addition to the tips on the <a
303 href="../dns-caveats.html#tips">DNS Issues</a> page, here are
304 some further tips:</p>
307 <li>Place all main server definitions before any
308 <code>VirtualHost</code> definitions. (This is to aid the
309 readability of the configuration -- the post-config merging
310 process makes it non-obvious that definitions mixed in around
311 virtual hosts might affect all virtual hosts.)</li>