name-based.html.en revision 49038652341bbe660a629c860507622583f8fdf0
22a41ae508535e87c3c2270202ae78de6ac4672crbowen<?xml version="1.0" encoding="ISO-8859-1"?>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
22a41ae508535e87c3c2270202ae78de6ac4672crbowen<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
22a41ae508535e87c3c2270202ae78de6ac4672crbowen XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
22a41ae508535e87c3c2270202ae78de6ac4672crbowen This file is generated from xml source: DO NOT EDIT
22a41ae508535e87c3c2270202ae78de6ac4672crbowen XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
22a41ae508535e87c3c2270202ae78de6ac4672crbowen --><title>Name-based Virtual Host Support - Apache HTTP Server</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 href="/images/favicon.ico" rel="shortcut icon" /></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="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p><p class="apache">Apache HTTP Server Version 2.0</p><img alt="" src="/images/feather.gif" /></div><div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div><div id="path"><a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs-project/">Documentation</a> &gt; <a href="../">Version 2.0</a></div><div id="page-content"><div id="preamble"><h1>Name-based Virtual Host Support</h1>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <p>This document describes when and how to use name-based virtual hosts.</p>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen</div><div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#namevip">Name-based vs. IP-based Virtual Hosts</a></li><li><img alt="" src="/images/down.gif" /> <a href="#using">Using Name-based Virtual Hosts</a></li><li><img alt="" src="/images/down.gif" /> <a href="#">Compatibility with Older Browsers</a></li></ul><h3>See also</h3><ul class="seealso"><li><a href="ip-based.html">IP-based Virtual Host Support</a></li><li><a href="details.html">An In-Depth Discussion of Virtual Host Matching</a></li><li><a href="mass.html">Dynamically configured mass virtual hosting</a></li><li><a href="examples.html">Virtual Host examples for common setups</a></li><li><a href="examples.html#serverpath">ServerPath configuration example</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="namevip" id="namevip">Name-based vs. IP-based Virtual Hosts</a></h2>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
4eb5e97c7a147352e1017b0114d2719d5f67cea9rbowen <p>IP-based virtual hosts use the IP address of the connection to
22a41ae508535e87c3c2270202ae78de6ac4672crbowen determine the correct virtual host to serve. Therefore you need to
52fff662005b1866a3ff09bb6c902800c5cc6dedjerenkrantz have a separate IP address for each host. With name-based virtual
22a41ae508535e87c3c2270202ae78de6ac4672crbowen hosting, the server relies on the client to report the hostname as
22a41ae508535e87c3c2270202ae78de6ac4672crbowen part of the HTTP headers. Using this technique, many different hosts
22a41ae508535e87c3c2270202ae78de6ac4672crbowen can share the same IP address.</p>
4b5981e276e93df97c34e4da05ca5cf8bbd937dand
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <p>Name-based virtual hosting is usually simpler, since you need
22a41ae508535e87c3c2270202ae78de6ac4672crbowen only configure your DNS server to map each hostname to the correct
22a41ae508535e87c3c2270202ae78de6ac4672crbowen IP address and then configure the Apache HTTP Server to recognize
22a41ae508535e87c3c2270202ae78de6ac4672crbowen the different hostnames. Name-based virtual hosting also eases
22a41ae508535e87c3c2270202ae78de6ac4672crbowen the demand for scarce IP addresses. Therefore you should use
22a41ae508535e87c3c2270202ae78de6ac4672crbowen name-based virtual hosting unless there is a specific reason to
22a41ae508535e87c3c2270202ae78de6ac4672crbowen choose IP-based virtual hosting. Some reasons why you might consider
22a41ae508535e87c3c2270202ae78de6ac4672crbowen using IP-based virtual hosting:</p>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <ul>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <li>Some ancient clients are not compatible with name-based virtual
22a41ae508535e87c3c2270202ae78de6ac4672crbowen hosting. For name-based virtual hosting to work, the client must send
22a41ae508535e87c3c2270202ae78de6ac4672crbowen the HTTP Host header. This is required by HTTP/1.1, and is
22a41ae508535e87c3c2270202ae78de6ac4672crbowen implemented by all modern HTTP/1.0 browsers as an extension. If you
22a41ae508535e87c3c2270202ae78de6ac4672crbowen need to support obsolete clients and still use name-based virtual
22a41ae508535e87c3c2270202ae78de6ac4672crbowen hosting, a possible technique is discussed at the end of this
ec7f95732d562ee4f5586faea04d12df7fb20180rbowen document.</li>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
cb31536ca972d939ea2833f27b99f8b0bdde049crbowen <li>Name-based virtual hosting cannot be used with SSL secure servers
22a41ae508535e87c3c2270202ae78de6ac4672crbowen because of the nature of the SSL protocol.</li>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <li>Some operating systems and network equipment implement bandwidth
22a41ae508535e87c3c2270202ae78de6ac4672crbowen management techniques that cannot differentiate between hosts unless
22a41ae508535e87c3c2270202ae78de6ac4672crbowen they are on separate IP addresses.</li>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen </ul>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div><div class="section"><h2><a name="using" id="using">Using Name-based Virtual Hosts</a></h2>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen<table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="/mod/core.html">core</a></code></li></ul></td><td><ul><li><code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code></li><li><code class="directive"><a href="/mod/core.html#namevirtualhost">NameVirtualHost</a></code></li><li><code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code></li><li><code class="directive"><a href="/mod/core.html#servername">ServerName</a></code></li><li><code class="directive"><a href="/mod/core.html#serverpath">ServerPath</a></code></li><li><code class="directive"><a href="/mod/core.html#virtualhost">VirtualHost</a></code></li></ul></td></tr></table>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <p>To use name-based virtual hosting, you must designate the IP
22a41ae508535e87c3c2270202ae78de6ac4672crbowen address (and possibly port) on the server that will be accepting
22a41ae508535e87c3c2270202ae78de6ac4672crbowen requests for the hosts. This is configured using the <code class="directive"><a href="/mod/core.html#namevirtualhost">NameVirtualHost</a></code> directive.
22a41ae508535e87c3c2270202ae78de6ac4672crbowen In the normal case where any and all IP addresses on the server should
22a41ae508535e87c3c2270202ae78de6ac4672crbowen be used, you can use <code>*</code> as the argument to
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <code>NameVirtualHost</code>. Note that mentioning an IP address in a
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <code>NameVirtualHost</code> directive does not automatically make the
22a41ae508535e87c3c2270202ae78de6ac4672crbowen server listen to that IP address. See <a href="/bind.html">Setting
22a41ae508535e87c3c2270202ae78de6ac4672crbowen which addresses and ports Apache uses</a> for more details. In addition,
22a41ae508535e87c3c2270202ae78de6ac4672crbowen any IP address specified here must be associated with a network interface
22a41ae508535e87c3c2270202ae78de6ac4672crbowen on the server.</p>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
1db37411af96e1c17b78ed2b35461187791250cbrbowen <p>The next step is to create a <code class="directive"><a href="/mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> block for
1db37411af96e1c17b78ed2b35461187791250cbrbowen each different host that you would like to serve. The argument to the
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <code>&lt;VirtualHost&gt;</code> directive should be the same as the
22a41ae508535e87c3c2270202ae78de6ac4672crbowen argument to the <code>NameVirtualHost</code> directive (ie, an IP
22a41ae508535e87c3c2270202ae78de6ac4672crbowen address, or <code>*</code> for all addresses). Inside each
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <code>&lt;VirtualHost&gt;</code> block, you will need at minimum a /
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <code class="directive"><a href="/mod/core.html#servername">ServerName</a></code> directive to
22a41ae508535e87c3c2270202ae78de6ac4672crbowen designate which host is served and a <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code> directive to
22a41ae508535e87c3c2270202ae78de6ac4672crbowen show where in the filesystem the content for that host lives.</p>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <p>If you are adding virtual hosts to an existing web server, you
4eb5e97c7a147352e1017b0114d2719d5f67cea9rbowen must also create a &lt;VirtualHost&gt; block for the existing host.
ec7f95732d562ee4f5586faea04d12df7fb20180rbowen The <code>ServerName</code> and <code>DocumentRoot</code> included in
e953a3ed43a5521c980dbd1c1390d14b7f903b85rbowen this virtual host should be the same as the global
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <code>ServerName</code> and <code>DocumentRoot</code>. List this
22a41ae508535e87c3c2270202ae78de6ac4672crbowen virtual host first in the configuration file so that it will act as
22a41ae508535e87c3c2270202ae78de6ac4672crbowen the default host.</p>
22a41ae508535e87c3c2270202ae78de6ac4672crbowen
f3f7fbd152943fb15146b438ec8a4c39c404a2fbrbowen <p>For example, suppose that you are serving the domain
f3f7fbd152943fb15146b438ec8a4c39c404a2fbrbowen <code>www.domain.tld</code> and you wish to add the virtual host
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <code>www.otherdomain.tld</code>, which points at the same IP address.
22a41ae508535e87c3c2270202ae78de6ac4672crbowen Then you simply add the following to <code>httpd.conf</code>:</p>
ec7f95732d562ee4f5586faea04d12df7fb20180rbowen
ec7f95732d562ee4f5586faea04d12df7fb20180rbowen <div class="example"><p><code>
ec7f95732d562ee4f5586faea04d12df7fb20180rbowen NameVirtualHost *<br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen &lt;VirtualHost *&gt;<br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen ServerName www.domain.tld<br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen DocumentRoot /www/domain<br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen &lt;/VirtualHost&gt;<br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen <br />
eac602f079a7b6a1039df751710a2c734c5f67cenoirin &lt;VirtualHost *&gt;<br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen ServerName www.otherdomain.tld<br />
22a41ae508535e87c3c2270202ae78de6ac4672crbowen DocumentRoot /www/otherdomain<br />
&lt;/VirtualHost&gt;<br />
</code></p></div>
<p>You can alternatively specify an explicit IP address in place of
the * in both the <code>NameVirtualHost</code> and
<code>&lt;VirtualHost&gt;</code> directives.</p>
<p>Many servers want to be accessible by more than one name. This is
possible with the <code class="directive"><a href="/mod/core.html#serveralias">ServerAlias</a></code>
directive, placed inside the &lt;VirtualHost&gt; section. For
example if you add this to the first &lt;VirtualHost&gt; block
above</p>
<div class="example"><p><code>
ServerAlias domain.tld *.domain.tld
</code></p></div>
<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 characters * and ? can be used to match names. Of course,
you can't just make up names and place them in <code>ServerName</code>
or <code>ServerAlias</code>. You must first have your DNS server
properly configured to map those names to an IP address associated
with your server.</p>
<p>Finally, you can fine-tune the configuration of the virtual hosts
by placing other directives inside the
<code>&lt;VirtualHost&gt;</code> containers. Most directives can be
placed in these containers and will then change the configuration only
of the relevant virtual host. To find out if a particular directive
is allowed, check the <a href="/mod/directive-dict.html#Context">Context</a> of the
directive. Configuration directives set in the <em>main server
context</em> (outside any <code>&lt;VirtualHost&gt;</code> container)
will be used only if they are not overriden 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 <code>NameVirtualHost</code>. If
it is, then it will look at each <code>&lt;VirtualHost&gt;</code>
section with a matching IP address and try to find one where the
<code>ServerName</code> 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 <code>DocumentRoot</code> from the
<em>main server</em> will <strong>never</strong> be used when an IP
address matches the <code>NameVirtualHost</code> 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
<code>&lt;VirtualHost&gt;</code> container and list it first in the
configuration file.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div><div class="section"><h2><a name="" id="">Compatibility with Older Browsers</a></h2>
<p>As mentioned earlier, there are some clients
who do not send the required data for the name-based virtual
hosts to work properly. These clients will always be sent the
pages from the first virtual host listed for that IP address
(the <cite>primary</cite> name-based virtual host).</p>
<p>There is a possible workaround with the <code class="directive"><a href="/mod/core.html#serverpath">ServerPath</a></code>
directive, albeit a slightly cumbersome one:</p>
<p>Example configuration:</p>
<div class="example"><p><code>
NameVirtualHost 111.22.33.44<br />
<br />
&lt;VirtualHost 111.22.33.44&gt;<br />
ServerName www.domain.tld<br />
ServerPath /domain<br />
DocumentRoot /web/domain<br />
&lt;/VirtualHost&gt;<br />
</code></p></div>
<p>What does this mean? It means that a request for any URI
beginning with "<code>/domain</code>" will be served from the
virtual host <code>www.domain.tld</code> This means that the
pages can be accessed as
<code>http://www.domain.tld/domain/</code> for all clients,
although clients sending a <code>Host:</code> header can also
access it as <code>http://www.domain.tld/</code>.</p>
<p>In order to make this work, put a link on your primary
virtual host's page to
<code>http://www.domain.tld/domain/</code> Then, in the virtual
host's pages, be sure to use either purely relative links
(<em>e.g.</em>, "<code>file.html</code>" or
"<code>/icons/image.gif</code>" or links containing the
prefacing <code>/domain/</code> (<em>e.g.</em>,
"<code>http://www.domain.tld/domain/misc/file.html</code>" or
"<code>/domain/misc/file.html</code>").</p>
<p>This requires a bit of discipline, but adherence to these
guidelines will, for the most part, ensure that your pages will
work with all browsers, new and old.</p>
</div></div><div id="footer"><p class="apache">Maintained by the <a href="http://httpd.apache.org/docs-project/">Apache HTTP Server Documentation Project</a></p><p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div></body></html>