home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 May / Gamestar_62_2004-05_dvd.iso / Programy / apache_2.0.48-win32-x86-no_ssl.msi / Data.Cab / F253021_details.xml < prev    next >
Extensible Markup Language  |  2003-04-15  |  18KB  |  416 lines

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