1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 4<html xmlns="http://www.w3.org/1999/xhtml"> 5 <head> 6 <meta name="generator" content="HTML Tidy, see www.w3.org" /> 7 8 <title>An In-Depth Discussion of Virtual Host Matching</title> 9 </head> 10 <!-- Background white, links blue (unvisited), navy (visited), red (active) --> 11 12 <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" 13 vlink="#000080" alink="#FF0000"> 14 <div align="CENTER"> 15 <img src="../images/sub.gif" alt="[APACHE DOCUMENTATION]" /> 16 17 <h3>Apache HTTP Server Version 1.3</h3> 18 </div> 19 20 21 <h1 align="CENTER">An In-Depth Discussion of Virtual Host 22 Matching</h1> 23 24 <p>The virtual host code was completely rewritten in 25 <strong>Apache 1.3</strong>. This document attempts to explain 26 exactly what Apache does when deciding what virtual host to 27 serve a hit from. With the help of the new <a 28 href="../mod/core.html#namevirtualhost"><samp>NameVirtualHost</samp></a> 29 directive virtual host configuration should be a lot easier and 30 safer than with versions prior to 1.3.</p> 31 32 <p>If you just want to <cite>make it work</cite> without 33 understanding how, here are <a href="examples.html">some 34 examples</a>.</p> 35 36 <h3>Config File Parsing</h3> 37 38 <p>There is a <em>main_server</em> which consists of all the 39 definitions appearing outside of 40 <code><VirtualHost></code> sections. There are virtual 41 servers, called <em>vhosts</em>, which are defined by <a 42 href="../mod/core.html#virtualhost"><samp><VirtualHost></samp></a> 43 sections.</p> 44 45 <p>The directives <a 46 href="../mod/core.html#port"><samp>Port</samp></a>, <a 47 href="../mod/core.html#servername"><samp>ServerName</samp></a>, 48 <a 49 href="../mod/core.html#serverpath"><samp>ServerPath</samp></a>, 50 and <a 51 href="../mod/core.html#serveralias"><samp>ServerAlias</samp></a> 52 can appear anywhere within the definition of a server. However, 53 each appearance overrides the previous appearance (within that 54 server).</p> 55 56 <p>The default value of the <code>Port</code> field for 57 main_server is 80. The main_server has no default 58 <code>ServerPath</code>, or <code>ServerAlias</code>. The 59 default <code>ServerName</code> is deduced from the servers IP 60 address.</p> 61 62 <p>The main_server Port directive has two functions due to 63 legacy compatibility with NCSA configuration files. One 64 function is to determine the default network port Apache will 65 bind to. This default is overridden by the existence of any <a 66 href="../mod/core.html#listen"><code>Listen</code></a> 67 directives. The second function is to specify the port number 68 which is used in absolute URIs during redirects.</p> 69 70 <p>Unlike the main_server, vhost ports <em>do not</em> affect 71 what ports Apache listens for connections on.</p> 72 73 <p>Each address appearing in the <code>VirtualHost</code> 74 directive can have an optional port. If the port is unspecified 75 it defaults to the value of the main_server's most recent 76 <code>Port</code> statement. The special port <samp>*</samp> 77 indicates a wildcard that matches any port. Collectively the 78 entire set of addresses (including multiple <samp>A</samp> 79 record results from DNS lookups) are called the vhost's 80 <em>address set</em>.</p> 81 82 <p>Unless a <a 83 href="../mod/core.html#namevirtualhost">NameVirtualHost</a> 84 directive is used for a specific IP address the first vhost 85 with that address is treated as an IP-based vhost. In 1.3.13 86 and later that includes the IP address <code>*</code>.</p> 87 88 <p>If name-based vhosts should be used a 89 <code>NameVirtualHost</code> directive <em>must</em> appear 90 with the IP address set to be used for the name-based vhosts. 91 In other words, you must specify the IP address that holds the 92 hostname aliases (CNAMEs) for your name-based vhosts via a 93 <code>NameVirtualHost</code> directive in your configuration 94 file.</p> 95 96 <p>Multiple <code>NameVirtualHost</code> directives can be used 97 each with a set of <code>VirtualHost</code> directives but only 98 one <code>NameVirtualHost</code> directive should be used for 99 each specific IP:port pair.</p> 100 101 <p>The ordering of <code>NameVirtualHost</code> and 102 <code>VirtualHost</code> directives is not important which 103 makes the following two examples identical (only the order of 104 the <code>VirtualHost</code> directives for <em>one</em> 105 address set is important, see below):</p> 106<pre> 107 | 108 NameVirtualHost 111.22.33.44 | <VirtualHost 111.22.33.44> 109 <VirtualHost 111.22.33.44> | # server A 110 # server A | </VirtualHost> 111 ... | <VirtualHost 111.22.33.55> 112 </VirtualHost> | # server C 113 <VirtualHost 111.22.33.44> | ... 114 # server B | </VirtualHost> 115 ... | <VirtualHost 111.22.33.44> 116 </VirtualHost> | # server B 117 | ... 118 NameVirtualHost 111.22.33.55 | </VirtualHost> 119 <VirtualHost 111.22.33.55> | <VirtualHost 111.22.33.55> 120 # server C | # server D 121 ... | ... 122 </VirtualHost> | </VirtualHost> 123 <VirtualHost 111.22.33.55> | 124 # server D | NameVirtualHost 111.22.33.44 125 ... | NameVirtualHost 111.22.33.55 126 </VirtualHost> | 127 | 128</pre> 129 130 <p>(To aid the readability of your configuration you should 131 prefer the left variant.)</p> 132 133 <p>After parsing the <code>VirtualHost</code> directive, the 134 vhost server is given a default <code>Port</code> equal to the 135 port assigned to the first name in its <code>VirtualHost</code> 136 directive.</p> 137 138 <p>The complete list of names in the <code>VirtualHost</code> 139 directive are treated just like a <code>ServerAlias</code> (but 140 are not overridden by any <code>ServerAlias</code> statement) 141 if all names resolve to the same address set. Note that 142 subsequent <code>Port</code> statements for this vhost will not 143 affect the ports assigned in the address set.</p> 144 145 <p>During initialization a list for each IP address is 146 generated and inserted into an hash table. If the IP address is 147 used in a <code>NameVirtualHost</code> directive the list 148 contains all name-based vhosts for the given IP address. If 149 there are no vhosts defined for that address the 150 <code>NameVirtualHost</code> directive is ignored and an error 151 is logged. For an IP-based vhost the list in the hash table is 152 empty.</p> 153 154 <p>Due to a fast hashing function the overhead of hashing an IP 155 address during a request is minimal and almost not existent. 156 Additionally the table is optimized for IP addresses which vary 157 in the last octet.</p> 158 159 <p>For every vhost various default values are set. In 160 particular:</p> 161 162 <ol> 163 <li>If a vhost has no <a 164 href="../mod/core.html#serveradmin"><code>ServerAdmin</code></a>, 165 <a 166 href="../mod/core.html#resourceconfig"><code>ResourceConfig</code></a>, 167 <a 168 href="../mod/core.html#accessconfig"><code>AccessConfig</code></a>, 169 <a href="../mod/core.html#timeout"><code>Timeout</code></a>, 170 <a 171 href="../mod/core.html#keepalivetimeout"><code>KeepAliveTimeout</code></a>, 172 <a 173 href="../mod/core.html#keepalive"><code>KeepAlive</code></a>, 174 <a 175 href="../mod/core.html#maxkeepaliverequests"><code>MaxKeepAliveRequests</code></a>, 176 or <a 177 href="../mod/core.html#sendbuffersize"><code>SendBufferSize</code></a> 178 directive then the respective value is inherited from the 179 main_server. (That is, inherited from whatever the final 180 setting of that value is in the main_server.)</li> 181 182 <li>The "lookup defaults" that define the default directory 183 permissions for a vhost are merged with those of the 184 main_server. This includes any per-directory configuration 185 information for any module.</li> 186 187 <li>The per-server configs for each module from the 188 main_server are merged into the vhost server.</li> 189 </ol> 190 Essentially, the main_server is treated as "defaults" or a 191 "base" on which to build each vhost. But the positioning of 192 these main_server definitions in the config file is largely 193 irrelevant -- the entire config of the main_server has been 194 parsed when this final merging occurs. So even if a main_server 195 definition appears after a vhost definition it might affect the 196 vhost definition. 197 198 <p>If the main_server has no <code>ServerName</code> at this 199 point, then the hostname of the machine that httpd is running 200 on is used instead. We will call the <em>main_server address 201 set</em> those IP addresses returned by a DNS lookup on the 202 <code>ServerName</code> of the main_server.</p> 203 204 <p>For any undefined <code>ServerName</code> fields, a 205 name-based vhost defaults to the address given first in the 206 <code>VirtualHost</code> statement defining the vhost.</p> 207 208 <p>Any vhost that includes the magic <samp>_default_</samp> 209 wildcard is given the same <code>ServerName</code> as the 210 main_server.</p> 211 212 <h3>Virtual Host Matching</h3> 213 214 <p>The server determines which vhost to use for a request as 215 follows:</p> 216 217 <h4>Hash table lookup</h4> 218 219 <p>When the connection is first made by a client, the IP 220 address to which the client connected is looked up in the 221 internal IP hash table.</p> 222 223 <p>If the lookup fails (the IP address wasn't found) the 224 request is served from the <samp>_default_</samp> vhost if 225 there is such a vhost for the port to which the client sent the 226 request. If there is no matching <samp>_default_</samp> vhost 227 the request is served from the main_server.</p> 228 229 <p>In Apache 1.3.13 and later, if the IP address is not found 230 in the hash table then the match against the port number may 231 also result in an entry corresponding to a 232 <code>NameVirtualHost *</code>, which is subsequently handled 233 like other name-based vhosts.</p> 234 235 <p>If the lookup succeeded (a corresponding list for the IP 236 address was found) the next step is to decide if we have to 237 deal with an IP-based or a name-base vhost.</p> 238 239 <h4>IP-based vhost</h4> 240 241 <p>If the entry we found has an empty name list then we have 242 found an IP-based vhost, no further actions are performed and 243 the request is served from that vhost.</p> 244 245 <h4>Name-based vhost</h4> 246 247 <p>If the entry corresponds to a name-based vhost the name list 248 contains one or more vhost structures. This list contains the 249 vhosts in the same order as the <code>VirtualHost</code> 250 directives appear in the config file.</p> 251 252 <p>The first vhost on this list (the first vhost in the config 253 file with the specified IP address) has the highest priority 254 and catches any request to an unknown server name or a request 255 without a <code>Host:</code> header field.</p> 256 257 <p>If the client provided a <code>Host:</code> header field the 258 list is searched for a matching vhost and the first hit on a 259 <code>ServerName</code> or <code>ServerAlias</code> is taken 260 and the request is served from that vhost. A <code>Host:</code> 261 header field can contain a port number, but Apache always 262 matches against the real port to which the client sent the 263 request.</p> 264 265 <p>If the client submitted a HTTP/1.0 request without 266 <code>Host:</code> header field we don't know to what server 267 the client tried to connect and any existing 268 <code>ServerPath</code> is matched against the URI from the 269 request. The first matching path on the list is used and the 270 request is served from that vhost.</p> 271 272 <p>If no matching vhost could be found the request is served 273 from the first vhost with a matching port number that is on the 274 list for the IP to which the client connected (as already 275 mentioned before).</p> 276 277 <h4>Persistent connections</h4> 278 The IP lookup described above is only done <em>once</em> for a 279 particular TCP/IP session while the name lookup is done on 280 <em>every</em> request during a KeepAlive/persistent 281 connection. In other words a client may request pages from 282 different name-based vhosts during a single persistent 283 connection. 284 285 <h4>Absolute URI</h4> 286 287 <p>If the URI from the request is an absolute URI, and its 288 hostname and port match the main server or one of the 289 configured virtual hosts <em>and</em> match the address and 290 port to which the client sent the request, then the 291 scheme/hostname/port prefix is stripped off and the remaining 292 relative URI is served by the corresponding main server or 293 virtual host. If it does not match, then the URI remains 294 untouched and the request is taken to be a proxy request.</p> 295 296 <h3>Observations</h3> 297 298 <ul> 299 <li>A name-based vhost can never interfere with an IP-base 300 vhost and vice versa. IP-based vhosts can only be reached 301 through an IP address of its own address set and never 302 through any other address. The same applies to name-based 303 vhosts, they can only be reached through an IP address of the 304 corresponding address set which must be defined with a 305 <code>NameVirtualHost</code> directive.</li> 306 307 <li><code>ServerAlias</code> and <code>ServerPath</code> 308 checks are never performed for an IP-based vhost.</li> 309 310 <li>The order of name-/IP-based, the <samp>_default_</samp> 311 vhost and the <code>NameVirtualHost</code> directive within 312 the config file is not important. Only the ordering of 313 name-based vhosts for a specific address set is significant. 314 The one name-based vhosts that comes first in the 315 configuration file has the highest priority for its 316 corresponding address set.</li> 317 318 <li>For security reasons the port number given in a 319 <code>Host:</code> header field is never used during the 320 matching process. Apache always uses the real port to which 321 the client sent the request.</li> 322 323 <li>If a <code>ServerPath</code> directive exists which is a 324 prefix of another <code>ServerPath</code> directive that 325 appears later in the configuration file, then the former will 326 always be matched and the latter will never be matched. (That 327 is assuming that no <code>Host:</code> header field was 328 available to disambiguate the two.)</li> 329 330 <li>If two IP-based vhosts have an address in common, the 331 vhost appearing first in the config file is always matched. 332 Such a thing might happen inadvertently. The server will give 333 a warning in the error logfile when it detects this.</li> 334 335 <li>A <code>_default_</code> vhost catches a request only if 336 there is no other vhost with a matching IP address 337 <em>and</em> a matching port number for the request. The 338 request is only caught if the port number to which the client 339 sent the request matches the port number of your 340 <code>_default_</code> vhost which is your standard 341 <code>Port</code> by default. A wildcard port can be 342 specified (<em>i.e.</em>, <code>_default_:*</code>) to catch 343 requests to any available port. In Apache 1.3.13 and later 344 this also applies to <code>NameVirtualHost *</code> 345 vhosts.</li> 346 347 <li>The main_server is only used to serve a request if the IP 348 address and port number to which the client connected is 349 unspecified and does not match any other vhost (including a 350 <code>_default_</code> vhost). In other words the main_server 351 only catches a request for an unspecified address/port 352 combination (unless there is a <code>_default_</code> vhost 353 which matches that port).</li> 354 355 <li>A <code>_default_</code> vhost or the main_server is 356 <em>never</em> matched for a request with an unknown or 357 missing <code>Host:</code> header field if the client 358 connected to an address (and port) which is used for 359 name-based vhosts, <em>e.g.</em>, in a 360 <code>NameVirtualHost</code> directive.</li> 361 362 <li>You should never specify DNS names in 363 <code>VirtualHost</code> directives because it will force 364 your server to rely on DNS to boot. Furthermore it poses a 365 security threat if you do not control the DNS for all the 366 domains listed. There's <a href="../dns-caveats.html">more 367 information</a> available on this and the next two 368 topics.</li> 369 370 <li><code>ServerName</code> should always be set for each 371 vhost. Otherwise A DNS lookup is required for each 372 vhost.</li> 373 </ul> 374 375 <h3>Tips</h3> 376 377 <p>In addition to the tips on the <a 378 href="../dns-caveats.html#tips">DNS Issues</a> page, here are 379 some further tips:</p> 380 381 <ul> 382 <li>Place all main_server definitions before any 383 <code>VirtualHost</code> definitions. (This is to aid the 384 readability of the configuration -- the post-config merging 385 process makes it non-obvious that definitions mixed in around 386 virtual hosts might affect all virtual hosts.)</li> 387 388 <li>Group corresponding <code>NameVirtualHost</code> and 389 <code>VirtualHost</code> definitions in your configuration to 390 ensure better readability.</li> 391 392 <li>Avoid <code>ServerPaths</code> which are prefixes of 393 other <code>ServerPaths</code>. If you cannot avoid this then 394 you have to ensure that the longer (more specific) prefix 395 vhost appears earlier in the configuration file than the 396 shorter (less specific) prefix (<em>i.e.</em>, "ServerPath 397 /abc" should appear after "ServerPath /abc/def").</li> 398 </ul> 399 <hr /> 400 401 <h3 align="CENTER">Apache HTTP Server Version 1.3</h3> 402 <a href="./"><img src="../images/index.gif" alt="Index" /></a> 403 <a href="../"><img src="../images/home.gif" alt="Home" /></a> 404 405 </body> 406</html> 407 408