name-based.html revision 8576ab8c3d4b1db11140b1cda83116a7d5051590
1N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
1N/A<HTML><HEAD>
1N/A<TITLE>Apache name-based Virtual Hosts</TITLE>
1N/A</HEAD>
1N/A
1N/A<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
1N/A<BODY
1N/A BGCOLOR="#FFFFFF"
1N/A TEXT="#000000"
1N/A LINK="#0000FF"
1N/A VLINK="#000080"
1N/A ALINK="#FF0000"
1N/A>
1N/A<!--#include virtual="header.html" -->
1N/A<H1 ALIGN="CENTER">Apache name-based Virtual Host Support</H1>
1N/A
1N/A<STRONG>See Also:</STRONG>
1N/A<A HREF="ip-based.html">IP-based Virtual Host Support</A>
1N/A
1N/A<HR>
1N/A
1N/A<H2>Name-based vs. IP-based virtual hosts</H2>
1N/A
1N/A<P>While the approach with IP-based virtual hosts works very well,
1N/Ait is not the most elegant solution, because a dedicated IP address
1N/Ais needed for every virtual host and it is hard to implement on some
1N/Amachines. The <CODE>HTTP/1.1</CODE> protocol contains a method for the
1N/Aserver to identify what name it is being addressed as. Apache 1.1 and
1N/Alater support this approach as well as the traditional
1N/AIP-address-per-hostname method.</P>
1N/A
1N/A<P>The benefits of using the new name-based virtual host support is a
1N/Apractically unlimited number of servers, ease of configuration and use, and
1N/Arequires no additional hardware or software.
1N/AThe main disadvantage is that the client must support this part of the
1N/Aprotocol. The latest versions of most browsers do, but there are still
1N/Aold browsers in use who do not. This can cause problems, although a possible
1N/Asolution is addressed below.</P>
1N/A
1N/A<H2>Using non-IP Virtual Hosts</H2>
1N/A
1N/A<P>Using the new virtual hosts is quite easy, and superficially looks
1N/Alike the old method. The notable difference between IP-based and
1N/Aname-based virtual host configuration is the
1N/A<A HREF="/mod/core.html#namevirtualhost"><CODE>NameVirtualHost</CODE></A>
1N/Adirective which specifies an IP address that should be used as a
1N/Atarget for name-based virtual hosts.</P>
1N/A
1N/A<P>For example, suppose that both <SAMP>www.domain.tld</SAMP> and
1N/A<SAMP>www.otherdomain.tld</SAMP> point at the IP address
1N/A<SAMP>111.22.33.44</SAMP>. Then you simply add to one of the Apache
1N/Aconfiguration files (most likely <CODE>httpd.conf</CODE> or
1N/A<CODE>srm.conf</CODE>) code similar to the following:</P>
1N/A
1N/A
1N/A
1N/A<PRE>
1N/A NameVirtualHost 111.22.33.44
1N/A
1N/A &lt;VirtualHost 111.22.33.44&gt;
1N/A ServerName www.domain.tld
1N/A DocumentRoot /www/domain
1N/A &lt;/VirtualHost&gt;
1N/A
1N/A &lt;VirtualHost 111.22.33.44&gt;
1N/A ServerName www.otherdomain.tld
1N/A DocumentRoot /www/otherdomain
1N/A &lt;/VirtualHost&gt;
1N/A</PRE>
1N/A
1N/A<P>Of course, any additional directives can (and should) be placed
1N/Ainto the <CODE>&lt;VirtualHost&gt;</CODE> section. To make this work,
1N/Aall that is needed is to make sure that the names
1N/A<SAMP>www.domain.tld</SAMP> and <SAMP>www.otherdomain.tld</SAMP>
1N/Aare pointing to the IP address <SAMP>111.22.33.44</SAMP></P>
1N/A
1N/A<P>Note: When you specify an IP address in a <CODE>NameVirtualHost</CODE>
1N/Adirective then requests to that IP address will only ever be served
1N/Aby matching &lt;VirtualHost&gt;s. The "main server" will
1N/A<STRONG>never</STRONG> be served from the specified IP address.
1N/AIf you start to use virtual hosts you should stop to use the "main server"
1N/Aas an independent server and rather use it as a place for
1N/Aconfiguration directives that are common for all your virtual hosts.
1N/AIn other words, you should add a &lt;VirtualHost&gt; section for
1N/A<EM>every</EM> server (hostname) you want to maintain on your server.
1N/A
1N/A<P>Additionally, many servers may wish to be accessible by more than
1N/Aone name. For example, the example server might want to be accessible
1N/Aas <CODE>domain.tld</CODE>, or <CODE>www2.domain.tld</CODE>, assuming
1N/Athe IP addresses pointed to the same server. In fact, one might want it
1N/Aso that all addresses at <CODE>domain.tld</CODE> were picked up by the
1N/Aserver. This is possible with the
1N/A<A HREF="/mod/core.html#serveralias"><CODE>ServerAlias</CODE></A>
1N/Adirective, placed inside the &lt;VirtualHost&gt; section. For
1N/Aexample:</P>
1N/A
1N/A<PRE>
1N/A ServerAlias domain.tld *.domain.tld
1N/A</PRE>
1N/A
1N/A<P>Note that you can use <CODE>*</CODE> and <CODE>?</CODE> as wild-card
1N/Acharacters.</P>
1N/A
1N/A<P>You also might need <CODE>ServerAlias</CODE> if you are
1N/Aserving local users who do not always include the domain name.
1N/AFor example, if local users are
1N/Afamiliar with typing "www" or "www.foobar" then you will need to add
1N/A<CODE>ServerAlias www www.foobar</CODE>. It isn't possible for the
1N/Aserver to know what domain the client uses for their name resolution
1N/Abecause the client doesn't provide that information in the request.</P>
1N/A
1N/A<H2>Compatibility with Older Browsers</H2>
1N/A
1N/A<P>As mentioned earlier, there are still some clients in use who
1N/Ado not send the required data for the name-based virtual hosts to work
1N/Aproperly. These clients will always be sent the pages from the
1N/A<CITE>primary</CITE> name-based virtual host (the first virtual host
1N/Aappearing in the configuration file for a specific IP address).</P>
1N/A
1N/A<P>There is a possible workaround with the
1N/A<A HREF="/mod/core.html#serverpath"><CODE>ServerPath</CODE></A>
1N/Adirective, albeit a slightly cumbersome one:</P>
1N/A
1N/A<P>Example configuration:
1N/A
1N/A<PRE>
1N/A 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>