Current Path : /usr/local/apache22/share/doc/apache2/vhosts/ |
FreeBSD hs32.drive.ne.jp 9.1-RELEASE FreeBSD 9.1-RELEASE #1: Wed Jan 14 12:18:08 JST 2015 root@hs32.drive.ne.jp:/sys/amd64/compile/hs32 amd64 |
Current File : //usr/local/apache22/share/doc/apache2/vhosts/details.html.en |
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" /> <!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX This file is generated from xml source: DO NOT EDIT XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --> <title>An In-Depth Discussion of Virtual Host Matching - Apache HTTP Server Version 2.2</title> <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" /> <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" /> <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" /> <script src="../style/scripts/prettify.min.js" type="text/javascript"> </script> <link href="../images/favicon.ico" rel="shortcut icon" /><link href="http://httpd.apache.org/docs/current/vhosts/details.html" rel="canonical" /></head> <body id="manual-page"><div id="page-header"> <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p> <p class="apache">Apache HTTP Server Version 2.2</p> <img alt="" src="../images/feather.gif" /></div> <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div> <div id="path"> <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 class="retired"><h4>Please note</h4> <p> This document refers to a legacy release (<strong>2.2</strong>) of Apache httpd. The active release (<strong>2.4</strong>) is documented <a href="http://httpd.apache.org/docs/current">here</a>. If you have not already upgraded, please follow <a href="http://httpd.apache.org/docs/current/upgrading.html">this link</a> for more information.</p> <p>You may follow <a href="http://httpd.apache.org/docs/current/vhosts/details.html">this link</a> to go to the current version of this document.</p></div><div id="preamble"><h1>An In-Depth Discussion of Virtual Host Matching</h1> <div class="toplang"> <p><span>Available Languages: </span><a href="../en/vhosts/details.html" title="English"> en </a> | <a href="../fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Français"> fr </a> | <a href="../ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> | <a href="../tr/vhosts/details.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p> </div> <p>The virtual host code was completely rewritten in <strong>Apache 1.3</strong>. This document attempts to explain exactly what Apache does when deciding what virtual host to serve a hit from. With the help of the new <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code> directive virtual host configuration should be a lot easier and safer than with versions prior to 1.3.</p> <p>If you just want to <cite>make it work</cite> without understanding how, here are <a href="examples.html">some examples</a>.</p> </div> <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#configparsing">Config File Parsing</a></li> <li><img alt="" src="../images/down.gif" /> <a href="#hostmatching">Virtual Host Matching</a></li> <li><img alt="" src="../images/down.gif" /> <a href="#tips">Tips</a></li> </ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div> <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="configparsing" id="configparsing">Config File Parsing</a></h2> <p>There is a <em>main_server</em> which consists of all the definitions appearing outside of <code><VirtualHost></code> sections. There are virtual servers, called <em>vhosts</em>, which are defined by <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> sections.</p> <p>The directives <code class="directive"><a href="../mod/core.html#servername">ServerName</a></code> and <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code> can appear anywhere within the definition of a server. However, each appearance overrides the previous appearance (within that server).</p> <p>The main_server has no default <code>ServerPath</code>, or <code>ServerAlias</code>. The default <code>ServerName</code> is deduced from the server's IP address.</p> <p>Port numbers specified in the <code>VirtualHost</code> directive do not influence what port numbers Apache will listen on, they only discriminate between which <code>VirtualHost</code> will be selected to handle a request.</p> <p>Each address appearing in the <code>VirtualHost</code> directive can have an optional port. If the port is unspecified it is treated as a wildcard port. The special port <code>*</code> indicates a wildcard that matches any port. Collectively the entire set of addresses (including multiple <code>A</code> record results from DNS lookups) are called the vhost's <em>address set</em>.</p> <p>Unless a <code class="directive"><a href="../mod/core.html#namevirtualhost">NameVirtualHost</a></code> directive is used for the exact IP address and port pair in the <code>VirtualHost</code> directive, Apache selects the best match only on the basis of the IP address (or wildcard) and port number. If there are multiple identical best matches, the first <code>VirtualHost</code> appearing in the configuration file will be selected.</p> <p>If you want Apache to <em>further</em> discriminate on the basis of the HTTP <code>Host</code> header supplied by the client, the <code>NameVirtualHost</code> directive <em>must</em> appear with the exact IP address (or wildcard) and port pair used in a corresponding set of <code>VirtualHost</code> directives.</p> <p>The name-based virtual host selection occurs only after a single IP-based virtual host has been selected, and only considers the set of virtual hosts that carry an identical IP address and port pair.</p> <p>Hostnames can be used in place of IP addresses in a virtual host definition, but it is resolved at startup and is not recommended.</p> <p>Multiple <code>NameVirtualHost</code> directives can be used each with a set of <code>VirtualHost</code> directives but only one <code>NameVirtualHost</code> directive should be used for each specific IP:port pair.</p> <p>The ordering of <code>NameVirtualHost</code> and <code>VirtualHost</code> directives is not important which makes the following two examples identical (only the order of the <code>VirtualHost</code> directives for <em>one</em> address set is important, see below):</p> <table><tr> <td><div class="example"><p><code> NameVirtualHost 111.22.33.44<br /> <VirtualHost 111.22.33.44><br /> # server A<br /> ...<br /> </VirtualHost><br /> <VirtualHost 111.22.33.44><br /> # server B<br /> ...<br /> </VirtualHost><br /> <br /> NameVirtualHost 111.22.33.55<br /> <VirtualHost 111.22.33.55><br /> # server C<br /> ...<br /> </VirtualHost><br /> <VirtualHost 111.22.33.55><br /> # server D<br /> ...<br /> </VirtualHost> </code></p></div></td> <td><div class="example"><p><code> <VirtualHost 111.22.33.44><br /> # server A<br /> </VirtualHost><br /> <VirtualHost 111.22.33.55><br /> # server C<br /> ...<br /> </VirtualHost><br /> <VirtualHost 111.22.33.44><br /> # server B<br /> ...<br /> </VirtualHost><br /> <VirtualHost 111.22.33.55><br /> # server D<br /> ...<br /> </VirtualHost><br /> <br /> NameVirtualHost 111.22.33.44<br /> NameVirtualHost 111.22.33.55<br /> <br /> </code></p></div></td> </tr></table> <p>(To aid the readability of your configuration you should prefer the left variant.)</p> <p>During initialization a list for each IP address is generated and inserted into an hash table. If the IP address is used in a <code>NameVirtualHost</code> directive the list contains all name-based vhosts for the given IP address. If there are no vhosts defined for that address the <code>NameVirtualHost</code> directive is ignored and an error is logged. For an IP-based vhost the list in the hash table is empty.</p> <p>Due to a fast hashing function the overhead of hashing an IP address during a request is minimal and almost not existent. Additionally the table is optimized for IP addresses which vary in the last octet.</p> <p>For every vhost various default values are set. In particular:</p> <ol> <li>If a vhost has no <code class="directive"><a href="../mod/core.html#serveradmin">ServerAdmin</a></code>, <code class="directive"><a href="../mod/core.html#timeout">Timeout</a></code>, <code class="directive"><a href="../mod/core.html#keepalivetimeout">KeepAliveTimeout</a></code>, <code class="directive"><a href="../mod/core.html#keepalive">KeepAlive</a></code>, <code class="directive"><a href="../mod/core.html#maxkeepaliverequests">MaxKeepAliveRequests</a></code>, <code class="directive"><a href="../mod/mpm_common.html#receivebuffersize">ReceiveBufferSize</a></code>, or <code class="directive"><a href="../mod/mpm_common.html#sendbuffersize">SendBufferSize</a></code> directive then the respective value is inherited from the main_server. (That is, inherited from whatever the final setting of that value is in the main_server.)</li> <li>The "lookup defaults" that define the default directory permissions for a vhost are merged with those of the main_server. This includes any per-directory configuration information for any module.</li> <li>The per-server configs for each module from the main_server are merged into the vhost server.</li> </ol> <p>Essentially, the main_server is treated as "defaults" or a "base" on which to build each vhost. But the positioning of these main_server definitions in the config file is largely irrelevant -- the entire config of the main_server has been parsed when this final merging occurs. So even if a main_server definition appears after a vhost definition it might affect the vhost definition.</p> <p>If the main_server has no <code>ServerName</code> at this point, then the hostname of the machine that <code class="program"><a href="../programs/httpd.html">httpd</a></code> is running on is used instead. We will call the <em>main_server address set</em> those IP addresses returned by a DNS lookup on the <code>ServerName</code> of the main_server.</p> <p>For any undefined <code>ServerName</code> fields, a name-based vhost defaults to the address given first in the <code>VirtualHost</code> statement defining the vhost.</p> <p>Any vhost that includes the magic <code>_default_</code> wildcard is given the same <code>ServerName</code> as the main_server.</p> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="hostmatching" id="hostmatching">Virtual Host Matching</a></h2> <p>The server determines which vhost to use for a request as follows:</p> <h3><a name="hashtable" id="hashtable">Hash table lookup</a></h3> <p>When the connection is first made by a client, the IP address to which the client connected is looked up in the internal IP hash table.</p> <p>If the lookup fails (the IP address wasn't found) the request is served from the <code>_default_</code> vhost if there is such a vhost for the port to which the client sent the request. If there is no matching <code>_default_</code> vhost the request is served from the main_server.</p> <p>If the IP address is not found in the hash table then the match against the port number may also result in an entry corresponding to a <code>NameVirtualHost *</code>, which is subsequently handled like other name-based vhosts.</p> <p>If the lookup succeeded (a corresponding list for the IP address was found) the next step is to decide if we have to deal with an IP-based or a name-base vhost.</p> <h3><a name="ipbased" id="ipbased">IP-based vhost</a></h3> <p>If the entry we found has an empty name list then we have found an IP-based vhost, no further actions are performed and the request is served from that vhost.</p> <h3><a name="namebased" id="namebased">Name-based vhost</a></h3> <p>If the entry corresponds to a name-based vhost the name list contains one or more vhost structures. This list contains the vhosts in the same order as the <code>VirtualHost</code> directives appear in the config file.</p> <p>The first vhost on this list (the first vhost in the config file with the specified IP address) has the highest priority and catches any request to an unknown server name or a request without a <code>Host:</code> header field.</p> <p>If the client provided a <code>Host:</code> header field the list is searched for a matching vhost and the first hit on a <code>ServerName</code> or <code>ServerAlias</code> is taken and the request is served from that vhost. A <code>Host:</code> header field can contain a port number, but Apache always matches against the real port to which the client sent the request.</p> <p>The complete list of names in the <code>VirtualHost</code> directive are treated just like a (non wildcard) <code>ServerAlias</code> (but are not overridden by any <code>ServerAlias</code> statement).</p> <p>If the client submitted a HTTP/1.0 request without <code>Host:</code> header field we don't know to what server the client tried to connect and any existing <code>ServerPath</code> is matched against the URI from the request. The first matching path on the list is used and the request is served from that vhost.</p> <p>If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected (as already mentioned before).</p> <h3><a name="persistent" id="persistent">Persistent connections</a></h3> <p>The IP lookup described above is only done <em>once</em> for a particular TCP/IP session while the name lookup is done on <em>every</em> request during a KeepAlive/persistent connection. In other words a client may request pages from different name-based vhosts during a single persistent connection.</p> <h3><a name="absoluteURI" id="absoluteURI">Absolute URI</a></h3> <p>If the URI from the request is an absolute URI, and its hostname and port match the main server or one of the configured virtual hosts <em>and</em> match the address and port to which the client sent the request, then the scheme/hostname/port prefix is stripped off and the remaining relative URI is served by the corresponding main server or virtual host. If it does not match, then the URI remains untouched and the request is taken to be a proxy request.</p> <h3><a name="observations" id="observations">Observations</a></h3> <ul> <li>A name-based vhost can never interfere with an IP-base vhost and vice versa. IP-based vhosts can only be reached through an IP address of its own address set and never through any other address. The same applies to name-based vhosts, they can only be reached through an IP address of the corresponding address set which must be defined with a <code>NameVirtualHost</code> directive.</li> <li><code>ServerAlias</code> and <code>ServerPath</code> checks are never performed for an IP-based vhost.</li> <li>The order of name-/IP-based, the <code>_default_</code> vhost and the <code>NameVirtualHost</code> directive within the config file is not important. Only the ordering of name-based vhosts for a specific address set is significant. The one name-based vhosts that comes first in the configuration file has the highest priority for its corresponding address set.</li> <li>The <code>Host:</code> header field is never used during the matching process. Apache always uses the real port to which the client sent the request.</li> <li>If a <code>ServerPath</code> directive exists which is a prefix of another <code>ServerPath</code> directive that appears later in the configuration file, then the former will always be matched and the latter will never be matched. (That is assuming that no <code>Host:</code> header field was available to disambiguate the two.)</li> <li>If two IP-based vhosts have an address in common, the vhost appearing first in the config file is always matched. Such a thing might happen inadvertently. The server will give a warning in the error logfile when it detects this.</li> <li>A <code>_default_</code> vhost catches a request only if there is no other vhost with a matching IP address <em>and</em> a matching port number for the request. The request is only caught if the port number to which the client sent the request matches the port number of your <code>_default_</code> vhost which is your standard <code>Listen</code> by default. A wildcard port can be specified (<em>i.e.</em>, <code>_default_:*</code>) to catch requests to any available port. This also applies to <code>NameVirtualHost *</code> vhosts. Note that this is simply an extension of the "best match" principle, as a specific and exact match is favored over a wildcard.</li> <li>The main_server is only used to serve a request if the IP address and port number to which the client connected is unspecified and does not match any other vhost (including a <code>_default_</code> vhost). In other words the main_server only catches a request for an unspecified address/port combination (unless there is a <code>_default_</code> vhost which matches that port).</li> <li>A <code>_default_</code> vhost or the main_server is <em>never</em> matched for a request with an unknown or missing <code>Host:</code> header field if the client connected to an address (and port) which is used for name-based vhosts, <em>e.g.</em>, in a <code>NameVirtualHost</code> directive.</li> <li>You should never specify DNS names in <code>VirtualHost</code> directives because it will force your server to rely on DNS to boot. Furthermore it poses a security threat if you do not control the DNS for all the domains listed. There's <a href="../dns-caveats.html">more information</a> available on this and the next two topics.</li> <li><code>ServerName</code> should always be set for each vhost. Otherwise a DNS lookup is required for each vhost.</li> </ul> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="tips" id="tips">Tips</a></h2> <p>In addition to the tips on the <a href="../dns-caveats.html#tips">DNS Issues</a> page, here are some further tips:</p> <ul> <li>Place all main_server definitions before any <code>VirtualHost</code> definitions. (This is to aid the readability of the configuration -- the post-config merging process makes it non-obvious that definitions mixed in around virtual hosts might affect all virtual hosts.)</li> <li>Group corresponding <code>NameVirtualHost</code> and <code>VirtualHost</code> definitions in your configuration to ensure better readability.</li> <li>Avoid <code>ServerPaths</code> which are prefixes of other <code>ServerPaths</code>. If you cannot avoid this then you have to ensure that the longer (more specific) prefix vhost appears earlier in the configuration file than the shorter (less specific) prefix (<em>i.e.</em>, "ServerPath /abc" should appear after "ServerPath /abc/def").</li> </ul> </div></div> <div class="bottomlang"> <p><span>Available Languages: </span><a href="../en/vhosts/details.html" title="English"> en </a> | <a href="../fr/vhosts/details.html" hreflang="fr" rel="alternate" title="Français"> fr </a> | <a href="../ko/vhosts/details.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> | <a href="../tr/vhosts/details.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p> </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div> <script type="text/javascript"><!--//--><![CDATA[//><!-- var comments_shortname = 'httpd'; var comments_identifier = 'http://httpd.apache.org/docs/2.2/vhosts/details.html'; (function(w, d) { if (w.location.hostname.toLowerCase() == "httpd.apache.org") { d.write('<div id="comments_thread"><\/div>'); var s = d.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier; (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); } else { d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>'); } })(window, document); //--><!]]></script></div><div id="footer"> <p class="apache">Copyright 2017 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> <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!-- if (typeof(prettyPrint) !== 'undefined') { prettyPrint(); } //--><!]]></script> </body></html>