name-based.html revision 85939515009c2c78e391236bf825ded8609f6a1a
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen<HTML><HEAD>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<TITLE>Apache name-based Virtual Hosts</TITLE>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen</HEAD>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<BODY
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen BGCOLOR="#FFFFFF"
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen TEXT="#000000"
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen LINK="#0000FF"
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen VLINK="#000080"
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen ALINK="#FF0000"
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<!--#include virtual="header.html" -->
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<H1 ALIGN="CENTER">Apache name-based Virtual Host Support</H1>
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<STRONG>See Also:</STRONG>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<A HREF="ip-based.html">IP-based Virtual Host Support</A>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<HR>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen<H2>Name-based vs. IP-based virtual hosts</H2>
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen<P>While the approach with IP-based virtual hosts works very well,
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainenit is not the most elegant solution, because a dedicated IP address
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenis needed for every virtual host and it is hard to implement on some
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenmachines. The <CODE>HTTP/1.1</CODE> protocol contains a method for the
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenserver to identify what name it is being addressed as. Apache 1.1 and
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenlater support this approach as well as the traditional
1704aa6b56b6a97bab6e995bcf7170b0c6527291Timo SirainenIP-address-per-hostname method.</P>
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainen
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainen<P>The benefits of using the new name-based virtual host support is a
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainenpractically unlimited number of servers, ease of configuration and use, and
7e0bb2b365cc38645313a3513fa79bd32d34b63cTimo Sirainenrequires no additional hardware or software.
c7480644202e5451fbed448508ea29a25cffc99cTimo SirainenThe main disadvantage is that the client must support this part of the
c7480644202e5451fbed448508ea29a25cffc99cTimo Sirainenprotocol. The latest versions of most browsers do, but there are still
old browsers in use who do not. This can cause problems, although a possible
solution is addressed below.</P>
<H2>Using non-IP Virtual Hosts</H2>
<P>Using the new virtual hosts is quite easy, and superficially looks
like the old method. 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 111.22.33.44
&lt;VirtualHost 111.22.33.44&gt;
ServerName www.domain.tld
DocumentRoot /web/domain
&lt;/VirtualHost&gt;
</PRE>
<P>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.
<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 name
<SAMP>www.domain.tld</SAMP> points to the IP address
<SAMP>111.22.33.44</SAMP></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 <b>never</b>
be served from the specified IP address.
<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.</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
<CITE>primary</CITE> name-based virtual host (the first virtual host
appearing in the configuration file for a specific IP address).</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:
<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 (e.g. "<SAMP>file.html</SAMP>" or
"<SAMP>/icons/image.gif</SAMP>" or links containing the prefacing
<SAMP>/domain/</SAMP>
(e.g. "<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>