1N/A<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
1N/A<
TITLE>Apache name-based Virtual Hosts</
TITLE>
1N/A<!-- Background white, links blue (unvisited), navy (visited), red (active) --> 1N/A<
H1 ALIGN="CENTER">Apache name-based Virtual Host Support</
H1>
1N/A<
STRONG>See Also:</
STRONG>
1N/A<
H2>Name-based vs. IP-based virtual hosts</
H2>
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<
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<
H2>Using non-IP Virtual Hosts</
H2>
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/Adirective which specifies an IP address that should be used as a
1N/Atarget for name-based virtual hosts.</
P>
1N/A<
SAMP>111.22.33.44</
SAMP>. Then you simply add to one of the Apache
1N/A<
CODE>
srm.conf</
CODE>) code similar to the following:</
P>
1N/A NameVirtualHost 111.22.33.44
1N/A <VirtualHost 111.22.33.44>
1N/A </VirtualHost>
1N/A <VirtualHost 111.22.33.44>
1N/A </VirtualHost>
1N/A<
P>Of course, any additional directives can (and should) be placed
1N/Ainto the <
CODE><VirtualHost></
CODE> section. To make this work,
1N/Aall that is needed is to make sure that the names
1N/Aare pointing to the IP address <
SAMP>111.22.33.44</
SAMP></
P>
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 <VirtualHost>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 <VirtualHost> section for
1N/A<
EM>every</
EM> server (hostname) you want to maintain on your server.
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/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/Adirective, placed inside the <VirtualHost> section. For
1N/A<
P>Note that you can use <
CODE>*</
CODE> and <
CODE>?</
CODE> as wild-card
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<
H2>Compatibility with Older Browsers</
H2>
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<
P>There is a possible workaround with the
1N/Adirective, albeit a slightly cumbersome one:</
P>
1N/A<
P>Example configuration:
1N/A NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
<
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
clients sending a <
SAMP>Host:</
SAMP> header can also access it as
<
P>In order to make this work, put a link on your primary virtual host's page
Then, in the virtual host's pages, be sure to use either purely
<
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