name-based.html revision 2eaf662cbc81e823e8d9aeb8d54e69e63032493e
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<title>Apache name-based Virtual Hosts</title>
</head>
<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF"
vlink="#000080" alink="#FF0000">
<!--#include virtual="header.html" -->
<h1 align="CENTER">Apache name-based Virtual Host Support</h1>
<strong>See Also:</strong> <a href="ip-based.html">IP-based
Virtual Host Support</a>
<hr />
<h2>Name-based vs. IP-based virtual hosts</h2>
<p>Early versions of HTTP (like many other protocols, e.g. FTP)
required a different IP address for each virtual host on the
server. On some platforms this can limit the number of virtual
hosts you can run, and because there are concerns about the
availability of IP addresses it is strongly discouraged by the
registraries (ARIN, RIPE, and APNIC).</p>
<p>The <code>HTTP/1.1</code> protocol, and a common extension
to <code>HTTP/1.0</code>, includes a method for the server to
identify what name it is being addressed as. Apache 1.1 and
later support this approach as well as the old
IP-address-per-hostname method.</p>
<p>The benefits of using the name-based virtual hosts is a
practically unlimited number of servers, ease of configuration
and use, and it requires no additional hardware or software.
The main disadvantage is that the client must support this part
of the protocol. Almost all browsers do, but there are still
tiny numbers of very old browsers in use which do not. This can
cause problems, although a possible solution is addressed
below.</p>
<h2>Using name-based virtual hosts</h2>
<p>Using name-based virtual hosts is quite easy, and
superficially looks like the old method. The notable difference
between IP-based and name-based virtual host configuration is
the <a
href="/mod/core.html#namevirtualhost"><code>NameVirtualHost</code></a>
directive which specifies an IP address that should be used as
a target for name-based virtual hosts, or the wildcard
<code>*</code> to indicate that the server only does name-based
virtual hosting (no IP-based virtual hosting).</p>
<p>For example, suppose that both <samp>www.domain.tld</samp>
and <samp>www.otherdomain.tld</samp> point at the IP address of
your server. Then you simply add to one of the Apache
configuration files (most likely <code>httpd.conf</code> or
<code>srm.conf</code>) code similar to the following:</p>
<pre>
NameVirtualHost *
&lt;VirtualHost *&gt;
ServerName www.domain.tld
DocumentRoot /www/domain
&lt;/VirtualHost&gt;
&lt;VirtualHost *&gt;
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
&lt;/VirtualHost&gt;
</pre>
<p>Of course, any additional directives can (and should) be
placed into the <code>&lt;VirtualHost&gt;</code> section. To
make this work, all that is needed is to make sure that the
names <samp>www.domain.tld</samp> and
<samp>www.otherdomain.tld</samp> are pointing to the right IP
address.</p>
<p>Note: When you specify an IP address in a
<code>NameVirtualHost</code> directive then requests to that IP
address will only ever be served by matching
&lt;VirtualHost&gt;s. The "main server" will
<strong>never</strong> be served from the specified IP address.
If you specify a wildcard then the "main server" isn't used at
all. If you start to use virtual hosts you should stop using
the "main server" as an independent server and rather use it as
a place for configuration directives that are common for all
your virtual hosts. In other words, you should add a
&lt;VirtualHost&gt; section for <em>every</em> server
(hostname) you want to maintain on your server.</p>
<p>Additionally, many servers may wish to be accessible by more
than one name. For example, the example server might want to be
accessible as <code>domain.tld</code>, or
<code>www2.domain.tld</code>, assuming the IP addresses pointed
to the same server. In fact, one might want it so that all
addresses at <code>domain.tld</code> were picked up by the
server. This is possible with the <a
href="/mod/core.html#serveralias"><code>ServerAlias</code></a>
directive, placed inside the &lt;VirtualHost&gt; section. For
example:</p>
<pre>
ServerAlias domain.tld *.domain.tld
</pre>
<p>Note that you can use <code>*</code> and <code>?</code> as
wild-card characters.</p>
<p>You also might need <code>ServerAlias</code> if you are
serving local users who do not always include the domain name.
For example, if local users are familiar with typing "www" or
"www.foobar" then you will need to add <code>ServerAlias www
www.foobar</code>. It isn't possible for the server to know
what domain the client uses for their name resolution because
the client doesn't provide that information in the request. The
<code>ServerAlias</code> directive is generally a way to have
different hostnames pointing to the same virtual host.</p>
<h2>Compatibility with Older Browsers</h2>
<p>As mentioned earlier, there are still some clients in use
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 <a
href="/mod/core.html#serverpath"><code>ServerPath</code></a>
directive, albeit a slightly cumbersome one:</p>
<p>Example configuration:</p>
<pre>
NameVirtualHost 111.22.33.44
&lt;VirtualHost 111.22.33.44&gt;
ServerName www.domain.tld
ServerPath /domain
DocumentRoot /web/domain
&lt;/VirtualHost&gt;
</pre>
<p>What does this mean? It means that a request for any URI
beginning with "<samp>/domain</samp>" will be served from the
virtual host <samp>www.domain.tld</samp> This means that the
pages can be accessed as
<code>http://www.domain.tld/domain/</code> for all clients,
although clients sending a <samp>Host:</samp> 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
<samp>http://www.domain.tld/domain/</samp> Then, in the virtual
host's pages, be sure to use either purely relative links
(<em>e.g.</em>, "<samp>file.html</samp>" or
"<samp>/icons/image.gif</samp>" or links containing the
prefacing <samp>/domain/</samp> (<em>e.g.</em>,
"<samp>http://www.domain.tld/domain/misc/file.html</samp>" or
"<samp>/domain/misc/file.html</samp>").</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>
<p>See also: <a href="examples.html#serverpath">ServerPath
configuration example</a></p>
<!--#include virtual="footer.html" -->
</body>
</html>