8N/A<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
371N/A<
TITLE>VirtualHost Examples</
TITLE>
8N/A<!-- Background white, links blue (unvisited), navy (visited), red (active) --> 8N/A<
H1 ALIGN="CENTER">Virtual Host examples for common setups</
H1>
8N/A<
H2>Base configuration</
H2>
8N/A<
LI><
A HREF="#purename">Simple name-based vhosting</
A>
8N/A<
LI><
A HREF="#name">More complicated name-based vhosts</
A>
8N/A<
LI><
A HREF="#ip">IP-based vhosts</
A>
8N/A<
LI><
A HREF="#port">Port-based vhosts</
A>
8N/A<
H2>Additional features</
H2>
493N/A<
LI><
A HREF="#default">Using <
CODE>_default_</
CODE> vhosts</
A>
8N/A<
LI><
A HREF="#migrate">Migrating a named-based vhost to an IP-based vhost</
A>
8N/A<
LI><
A HREF="#serverpath">Using the <
CODE>ServerPath</
CODE> directive</
A>
493N/A<
H3><
A NAME="purename">Simple name-based vhosting</
A></
H3>
8N/A<
LI><
STRONG>Setup:</
STRONG>
32N/A <
STRONG>Server configuration:</
STRONG>
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.
<
H3><
A NAME="name">More complicated name-based vhosts</
A></
H3>
<
LI><
STRONG>Setup 1:</
STRONG>
The server machine has one IP address (<
SAMP>111.22.33.44</
SAMP>)
<
STRONG>Server configuration:</
STRONG>
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
<VirtualHost 111.22.33.44>
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.
<
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.
<
STRONG>Server configuration:</
STRONG>
NameVirtualHost 111.22.33.55
<VirtualHost 111.22.33.55>
<VirtualHost 111.22.33.55>
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>
<
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>
<
STRONG>Server configuration:</
STRONG>
NameVirtualHost 192.168.1.1
NameVirtualHost 111.22.33.55
<VirtualHost 192.168.1.1 111.22.33.55>
Now requests from both networks will be served from the same
<
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.
<
STRONG>Server configuration:</
STRONG>
NameVirtualHost 111.22.33.44:80
NameVirtualHost 111.22.33.44:8080
<VirtualHost 111.22.33.44:80>
<VirtualHost 111.22.33.44:8080>
<VirtualHost 111.22.33.44:80>
<VirtualHost 111.22.33.44:8080>
<
H3><
A NAME="ip">IP-based vhosts</
A></
H3>
<
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>)
<
STRONG>Server configuration:</
STRONG>
<VirtualHost 111.22.33.55>
can only be reached through <
SAMP>111.22.33.44</
SAMP>
(which represents our main server).
<
LI><
STRONG>Setup 2:</
STRONG>
Same as setup 1, but we don't want to have a dedicated main server.
<
STRONG>Server configuration:</
STRONG>
<VirtualHost 111.22.33.44>
<VirtualHost 111.22.33.55>
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).
<
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>)
listening on port 8080, while the web server itself uses the default
<
STRONG>Server configuration:</
STRONG>
<VirtualHost 111.22.33.44:80>
<VirtualHost 111.22.33.55:8080>
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.
<
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>)
The address <
SAMP>111.22.33.44</
SAMP> should be used for a couple
of name-based vhosts and the other addresses for IP-based vhosts.
<
STRONG>Server configuration:</
STRONG>
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
<VirtualHost 111.22.33.44>
<VirtualHost 111.22.33.44>
<VirtualHost 111.22.33.55>
<VirtualHost 111.22.33.66>
<
H3><
A NAME="port">Port-based vhosts</
A></
H3>
<
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.
<
STRONG>Server configuration:</
STRONG>
<VirtualHost 111.22.33.44:8080>
from the main server and a request to port 8080 is served from
<
H3><
A NAME="default">Using <
CODE>_default_</
CODE> vhosts</
A></
H3>
<
LI><
STRONG>Setup 1:</
STRONG>
Catching <
EM>every</
EM> request to any unspecified IP address and port,
<
STRONG>Server configuration:</
STRONG>
<VirtualHost _default_:*>
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
address/
port that is used for name-based vhosts. If the request
contained an unknown or no <
CODE>Host:</
CODE> header it is
always served from the primary name-based vhost (the
vhost for that
address/
port appearing first in the configuration
to rewrite any request to a single information page (or script).
<
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.
<
STRONG>Server configuration:</
STRONG>
<VirtualHost _default_:80>
<VirtualHost _default_:*>
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.
<
LI><
STRONG>Setup 3:</
STRONG>
We want to have a default vhost for port 80, but no other default vhosts.
<
STRONG>Server configuration:</
STRONG>
<VirtualHost _default_:80>
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.
<
H3><
A NAME="migrate">Migrating a name-based vhost to an IP-based vhost</
A></
H3>
<
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.
<
STRONG>Server configuration:</
STRONG>
NameVirtualHost 111.22.33.55
<VirtualHost 111.22.33.55 111.22.33.66>
<VirtualHost 111.22.33.55>
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).
<
H3><
A NAME="serverpath">Using the <
CODE>ServerPath</
CODE> directive</
A></
H3>
<
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.
Old
HTTP/
1.0 clients do not send such a header and Apache has no clue
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.
<
STRONG>Server configuration:</
STRONG>
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
<VirtualHost 111.22.33.44>
<VirtualHost 111.22.33.44>
Due to the <
A HREF="/mod/core.html#serverpath"><
CODE>ServerPath</
CODE></
A>
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
both URL variants, <
EM>
i.e.</
EM>, with or without URL prefix.
<
LI><
STRONG>Setup:</
STRONG>