examples.html revision d02774d77527dca6a8b3dd3481d38b9444959800
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML><HEAD>
<TITLE>VirtualHost Examples</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">Virtual Host examples for common setups</H1>
<H2>Base configuration</H2>
<UL>
<LI><A HREF="#purename">Simple name-based vhosting</A>
<LI><A HREF="#name">More complicated name-based vhosts</A>
<LI><A HREF="#ip">IP-based vhosts</A>
<LI><A HREF="#port">Port-based vhosts</A>
</UL>
<H2>Additional features</H2>
<UL>
<LI><A HREF="#default">Using <CODE>_default_</CODE> vhosts</A>
<LI><A HREF="#migrate">Migrating a named-based vhost to an IP-based vhost</A>
<LI><A HREF="#serverpath">Using the <CODE>ServerPath</CODE> directive</A>
</UL>
<HR>
<H3><A NAME="purename">Simple name-based vhosting</A></H3>
<UL>
<LI><STRONG>Setup:</STRONG>
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
ServerName server.domain.tld
NameVirtualHost *
<VirtualHost *>
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost *>
ServerName www.sub.domain.tld
...
</VirtualHost>
</PRE>
The asterisks match all addresses, so the main server serves no
in the configuration file, it has the highest priority and can be
seen as the <CITE>default</CITE> or <CITE>primary</CITE> server.
</BLOCKQUOTE>
<P>
</UL>
<HR>
<H3><A NAME="name">More complicated name-based vhosts</A></H3>
<UL>
<LI><STRONG>Setup 1:</STRONG>
The server machine has one IP address (<SAMP>111.22.33.44</SAMP>)
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
ServerName www.sub.domain.tld
...
</VirtualHost>
</PRE>
Apart from <SAMP>localhost</SAMP> there are no unspecified
<SAMP>localhost</SAMP> requests. Due to the fact
it can be seen as the <CITE>default</CITE> or
<CITE>primary</CITE> server.
</BLOCKQUOTE>
<P>
<LI><STRONG>Setup 2:</STRONG>
The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
and <SAMP>111.22.33.55</SAMP>)
main server which should also catch any unspecified addresses.
We want to use a virtual host for the alias
catch any request to hostnames of the form
The address <SAMP>111.22.33.55</SAMP> should be
used for the virtual hosts.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
ServerName www.domain.tld
NameVirtualHost 111.22.33.55
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
ServerName www.sub.domain.tld
ServerAlias *.sub.domain.tld
...
</VirtualHost>
</PRE>
Any request to an address other than <SAMP>111.22.33.55</SAMP>
will be served from the main server. A request to
<SAMP>111.22.33.55</SAMP> with an unknown or no <CODE>Host:</CODE>
</BLOCKQUOTE>
<LI><STRONG>Setup 3:</STRONG>
The server machine has two IP addresses (<SAMP>192.168.1.1</SAMP>
and <SAMP>111.22.33.55</SAMP>). The machine is sitting between
an internal (intranet) network and an external (internet) network.
resolves to the external address (<SAMP>111.22.33.55</SAMP>), but
inside the network, that same name resolves to the internal
address (<SAMP>192.168.1.1</SAMP>).<P>
The server can be made to respond to internal and external
requests with the same content, with just one <CODE>VirtualHost</CODE>
section.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
NameVirtualHost 192.168.1.1
NameVirtualHost 111.22.33.55
<VirtualHost 192.168.1.1 111.22.33.55>
ServerName server1.domain.tld
ServerAlias server1
...
</VirtualHost>
</PRE></BLOCKQUOTE>
Now requests from both networks will be served from the same
<CODE>VirtualHost</CODE>
<LI><STRONG>Setup 4:</STRONG>
You have multiple domains going to the same IP and also want
to serve multiple ports. By defining the
ports in the "NameVirtualHost" tag, you can allow this to
work. If you try using <VirtualHost name:port> without the
NameVirtualHost name:port or you try to use the Listen
directive, your configuration will not work.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
NameVirtualHost 111.22.33.44:80
NameVirtualHost 111.22.33.44:8080
<VirtualHost 111.22.33.44:80>
ServerName www.domain.tld
</VirtualHost>
<VirtualHost 111.22.33.44:8080>
ServerName www.domain.tld
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost 111.22.33.44:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain-80
</VirtualHost>
<VirtualHost 111.22.33.44:8080>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain-8080
</VirtualHost>
</PRE></BLOCKQUOTE>
</UL>
<HR>
<H3><A NAME="ip">IP-based vhosts</A></H3>
<UL>
<LI><STRONG>Setup 1:</STRONG>
The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
and <SAMP>111.22.33.55</SAMP>)
main server.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
ServerName www.domain.tld
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
</PRE>
can only be reached through <SAMP>111.22.33.44</SAMP>
(which represents our main server).
</BLOCKQUOTE>
<P>
<LI><STRONG>Setup 2:</STRONG>
Same as setup 1, but we don't want to have a dedicated main server.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
ServerName server.domain.tld
<VirtualHost 111.22.33.44>
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
</PRE>
The main server can never catch a request, because all IP addresses
of our machine are in use for IP-based virtual hosts
(only <SAMP>localhost</SAMP> requests can hit the main server).
</BLOCKQUOTE>
<P>
<LI><STRONG>Setup 3:</STRONG>
The server machine has two IP addresses (<SAMP>111.22.33.44</SAMP>
and <SAMP>111.22.33.55</SAMP>)
main server.
listening on port 8080, while the web server itself uses the default
port 80.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 111.22.33.44:80
Listen 111.22.33.55:8080
ServerName server.domain.tld
<VirtualHost 111.22.33.44:80>
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55:8080>
ServerName www-cache.domain.tld
...
<Directory proxy:>
Order Deny,Allow
Deny from all
Allow from 111.22.33
</Directory>
</VirtualHost>
</PRE>
The main server can never catch a request, because all IP addresses
(apart from <SAMP>localhost</SAMP>) of our machine are in use for IP-based
virtual hosts. The web server can only be reached on the first address
through port 80 and the proxy only on the second address through port 8080.
</BLOCKQUOTE>
</UL>
<HR>
<UL>
<LI><STRONG>Setup:</STRONG>
The server machine has three IP addresses (<SAMP>111.22.33.44</SAMP>,
<SAMP>111.22.33.55</SAMP> and <SAMP>111.22.33.66</SAMP>)
respectively.
The address <SAMP>111.22.33.44</SAMP> should we used for a couple
of name-based vhosts and the other addresses for IP-based vhosts.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain1
ServerName www.sub1.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain2
ServerName www.sub2.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain1
ServerName www.otherdomain1.tld
...
</VirtualHost>
<VirtualHost 111.22.33.66>
DocumentRoot /www/otherdomain2
ServerName www.otherdomain2.tld
...
</VirtualHost>
</PRE></BLOCKQUOTE>
</UL>
<HR>
<H3><A NAME="port">Port-based vhosts</A></H3>
<UL>
<LI><STRONG>Setup:</STRONG>
The server machine has one IP address (<SAMP>111.22.33.44</SAMP>)
If we don't have the option to get another address or alias
for our server we can use port-based vhosts if we need
a virtual host with a different configuration.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
Listen 8080
ServerName www.domain.tld
<VirtualHost 111.22.33.44:8080>
...
</VirtualHost>
</PRE>
from the main server and a request to port 8080 is served from
the virtual host.
</BLOCKQUOTE>
</UL>
<HR>
<H3><A NAME="default">Using <CODE>_default_</CODE> vhosts</A></H3>
<UL>
<LI><STRONG>Setup 1:</STRONG>
Catching <EM>every</EM> request to any unspecified IP address and port,
virtual host.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
<VirtualHost _default_:*>
...
</VirtualHost>
</PRE>
Using such a default vhost with a wildcard port effectively
prevents any request going to the main server.<BR>
A default vhost never serves a request that was sent to an
contained an unknown or no <CODE>Host:</CODE> header it is
always served from the primary name-based vhost (the
file).<BR>
You can use
or
to rewrite any request to a single information page (or script).
</BLOCKQUOTE>
<P>
<LI><STRONG>Setup 2:</STRONG>
Same as setup 1, but the server listens on several ports and
we want to use a second <CODE>_default_</CODE> vhost for port 80.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
<VirtualHost _default_:80>
...
</VirtualHost>
<VirtualHost _default_:*>
...
</VirtualHost>
</PRE>
The default vhost for port 80 (which <EM>must</EM> appear before
any default vhost with a wildcard port) catches all requests that
were sent to an unspecified IP address. The main server is
never used to serve a request.
</BLOCKQUOTE>
<P>
<LI><STRONG>Setup 3:</STRONG>
We want to have a default vhost for port 80, but no other default vhosts.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
<VirtualHost _default_:80>
...
</VirtualHost>
</PRE>
A request to an unspecified address on port 80 is served from the
default vhost any other request to an unspecified address and port
is served from the main server.
</BLOCKQUOTE>
</UL>
<HR>
<H3><A NAME="migrate">Migrating a name-based vhost to an IP-based vhost</A></H3>
<UL>
<LI><STRONG>Setup:</STRONG>
The name-based vhost with the hostname
example, setup 2) should get its own IP address.
To avoid problems with name servers or proxies who cached the old
IP address for the name-based vhost we want to provide both variants
during a migration phase.<BR>
The solution is easy, because we can simply add the new IP address
(<SAMP>111.22.33.66</SAMP>) to the <CODE>VirtualHost</CODE> directive.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
Listen 80
ServerName www.domain.tld
NameVirtualHost 111.22.33.55
<VirtualHost 111.22.33.55 111.22.33.66>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
ServerName www.sub.domain.tld
ServerAlias *.sub.domain.tld
...
</VirtualHost>
</PRE>
The vhost can now be accessed through the new address (as an IP-based
vhost) and through the old address (as a name-based vhost).
</BLOCKQUOTE>
</UL>
<HR>
<H3><A NAME="serverpath">Using the <CODE>ServerPath</CODE> directive</A></H3>
<UL>
<LI><STRONG>Setup:</STRONG>
We have a server with two name-based vhosts. In order to match the correct
virtual host a client must send the correct <CODE>Host:</CODE> header.
what vhost the client tried to reach (and serves the request from
the primary vhost). To provide as much backward compatibility
as possible we create a primary vhost which returns a single page
containing links with an URL prefix to the name-based virtual hosts.
<P>
<STRONG>Server configuration:</STRONG>
<BLOCKQUOTE><PRE>
...
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
# primary vhost
RewriteEngine On
...
</VirtualHost>
<VirtualHost 111.22.33.44>
ServerName www.sub1.domain.tld
ServerPath /sub1/
RewriteEngine On
...
</VirtualHost>
<VirtualHost 111.22.33.44>
ServerName www.sub2.domain.tld
ServerPath /sub2/
RewriteEngine On
...
</VirtualHost>
</PRE>
directive a request to the
served from the sub1-vhost. <BR>
is only served from the sub1-vhost if the client sent a correct
<CODE>Host:</CODE> header.
If no <CODE>Host:</CODE> header is sent the client gets the
information page from the primary host.<BR>
Please note that there is one oddity: A request to
the sub1-vhost if the client sent no <CODE>Host:</CODE> header. <BR>
The <CODE>RewriteRule</CODE> directives are used to make sure that
a client which sent a correct <CODE>Host:</CODE> header can use
</BLOCKQUOTE>
</UL>
<LI><STRONG>Setup:</STRONG>
<!--#include virtual="footer.html" -->
</BODY>
</HTML>