determine the correct virtual host to serve. Therefore you need to
have a separate IP address for each host.</p>
- <p>With name-based virtual
- hosting, the server relies on the client to report the hostname as
- part of the HTTP headers. Using this technique, many different hosts
- can share the same IP address.</p>
+ <p>With name-based virtual hosting, the server relies on the client to
+ report the hostname as part of the HTTP headers. Using this technique,
+ many different hosts can share the same IP address.</p>
<p>Name-based virtual hosting is usually simpler, since you need
only configure your DNS server to map each hostname to the correct
they are on separate IP addresses.</li>
</ul>
+
+</section>
+
+<section id="alg"><title>How the server selects the proper name-based virtual host</title>
+
+ <p>It is important to recognize that the first step in name-based virtual
+ host resolution is IP-based resolution. Name-based virtual host
+ resolution only chooses the most appropriate name-based virtual host
+ after narrowing down the candidates to the best IP-based match. Using a wildcard (*)
+ for the IP address in all of the NameVirtualHost and VirtualHost directives makes this
+ IP-based mapping irrelevant.</p>
+
+ <p>When a request arrives, the server will first check if it is using
+ an IP address that matches exactly any <directive module="core"
+ >NameVirtualHost</directive>. If it is, then it will look at each <directive
+ type="section" module="core">VirtualHost</directive> section with a (literal) matching
+ IP address and try to find one where the <directive module="core"
+ >ServerName</directive> or <code>ServerAlias</code> matches the requested
+ hostname. If it finds one, then it uses the configuration for that server.</p>
+
+ <p id="defaultvhost"> If no matching ServerName or ServerAlias is found in the
+ set of virtual hosts matching the NameVirtualHost directive, then
+ <strong>the first listed virtual host</strong> that matches the IP
+ address will be used.</p>
</section>
<section id="using"><title>Using Name-based Virtual Hosts</title>
lives.</p>
<note><title>Main host goes away</title>
- <p>If you are adding virtual hosts to an existing web server, you
- must also create a <directive type="section" module="core"
- >VirtualHost</directive> block for the existing host. The <directive
- module="core">ServerName</directive> and <directive module="core"
- >DocumentRoot</directive> included in this virtual host should be the
- same as the global <directive module="core">ServerName</directive> and
- <directive module="core">DocumentRoot</directive>. List this virtual
- host first in the configuration file so that it will act as the default
- host.</p>
+ <p> Any request that doesn't match an existing <directive type="section"
+ module="core">VirtualHost</directive> is handled by the global
+ server configuration, regardless of the hostname or ServerName.</p>
+
+ <p> When you add a name-based virtual host to an existing server, and
+ the virtual host arguments match preexisting IP and port combinations,
+ requests will now be handled by an explicit virtual host. In this case,
+ it's usually wise to create a <a href="#defaultvhost">default virtual host</a>
+ with a <directive module="core">ServerName</directive> matching that of
+ the base server. New domains on the same interface and port, but
+ requiring separate configurations, can then be added as subsequent (non-default)
+ virtual hosts.</p>
</note>
<p>For example, suppose that you are serving the domain
- <code>www.domain.tld</code> and you wish to add the virtual host
- <code>www.otherdomain.tld</code>, which points at the same IP address.
+ <code>www.example.com</code> and you wish to add the virtual host
+ <code>other.example.com</code>, which points at the same IP address.
Then you simply add the following to <code>httpd.conf</code>:</p>
<example>
<br />
<VirtualHost *:80><br />
<indent>
- ServerName www.domain.tld<br />
- ServerAlias domain.tld *.domain.tld<br />
+ # This first-listed virtual host is also the default for *:80
+ ServerName www.example.com<br />
+ ServerAlias example.com *.example.com<br />
DocumentRoot /www/domain<br />
</indent>
</VirtualHost><br />
<br />
<VirtualHost *:80><br />
- <indent>ServerName www.otherdomain.tld<br />
+ <indent>ServerName other.example.com<br />
DocumentRoot /www/otherdomain<br />
</indent>
</VirtualHost><br />
web site:</p>
<example>
- ServerAlias domain.tld *.domain.tld
+ ServerAlias example.com *.example.com
</example>
- <p>then requests for all hosts in the <code>domain.tld</code> domain will
- be served by the <code>www.domain.tld</code> virtual host. The wildcard
+ <p>then requests for all hosts in the <code>example.com</code> domain will
+ be served by the <code>www.example.com</code> virtual host. The wildcard
characters <code>*</code> and <code>?</code> can be used to match names.
Of course, you can't just make up names and place them in <directive
module="core">ServerName</directive> or <code>ServerAlias</code>. You must
container) will be used only if they are not overridden by the virtual host
settings.</p>
- <p>Now when a request arrives, the server will first check if it is using
- an IP address that matches the <directive module="core"
- >NameVirtualHost</directive>. If it is, then it will look at each <directive
- type="section" module="core">VirtualHost</directive> section with a matching
- IP address and try to find one where the <directive module="core"
- >ServerName</directive> or <code>ServerAlias</code> matches the requested
- hostname. If it finds one, then it uses the configuration for that server.
- If no matching virtual host is found, then <strong>the first listed virtual
- host</strong> that matches the IP address will be used.</p>
-
- <p>As a consequence, the first listed virtual host is the <em>default</em>
- virtual host. The <directive module="core">DocumentRoot</directive> from
- the <em>main server</em> will <strong>never</strong> be used when an IP
- address matches the <directive module="core">NameVirtualHost</directive>
- directive. If you would like to have a special configuration for requests
- that do not match any particular virtual host, simply put that configuration
- in a <directive type="section" module="core">VirtualHost</directive>
- container and list it first in the configuration file.</p>
-
</section>
</manualpage>