<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <
meta name="generator" content="HTML Tidy, see www.w3.org" />
<
title>VirtualHost Examples</
title>
<!-- Background white, links blue (unvisited), navy (visited), red (active) --> <
body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#000080" <
h1 align="CENTER">Virtual Host examples for common setups</
h1>
<
p>This document attempts to answer the commonly-asked questions about
setting up virtual hosts. These scenarios are those involving multiple
web sites running on a single server, via <
a href="ip-based.html">IP-based</
a> virtual hosts. A document should be
coming soon about running sites on several servers behind a single
<
li><
a href="#purename">Running several name-based web sites on
a single IP address.</
a></
li>
<
li><
a href="#twoips">Name-based hosts on more than one IP
<
li><
a href="#intraextra">Serving the same content on different IP
addresses (such as an internal and external address).</
a></
li>
<
li><
a href="#port">Running different sites on different
<
li><
a href="#ip">IP-based virtual hosting</
a></
li>
<
li><
a href="#ipport">Mixed port-based and ip-based virtual
<
li><
a href="#mixed">Mixed name-based and IP-based vhosts</
a></
li>
<li>mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results</li> <li>No matter what hostname I use, I'm getting the main server (or <li>I'm trying to use SSL on my virtual host, and I'm getting the <
h2>Additional features</
h2>
<
li><
a href="#default">Using <
code>_default_</
code> vhosts</
a></
li>
<
li><
a href="#migrate">Migrating a named-based vhost to an IP-based
<
li><
a href="#serverpath">Using the <
code>ServerPath</
code>
<
h3><
a id="purename" name="purename">Running several name-based web
sites on a single IP address.</
a></
h3>
<
a id="name" name="name">
<!-- Anchor for backward compatibility with the previous version of this document. However, using * rather than the IP address will always work in a single-IP-address scenario, and tends to confuse people a little less anyways. --></
a>
<
p><
strong>Setup:</
strong></
p>
<
p>Your server has a single IP address, and multiple aliases (CNAMES)
point to this machine in DNS. You want to run a web server for
<
td bgcolor="#e0e5f5"><
strong>Note:</
strong> Creating virtual
host configurations on your Apache server does not magically
cause DNS entries to be created for those host names. You
<
em>must</
em> have the names in DNS, resolving to your IP
address, or nobody else will be able to see your web site. You
can put entries in your <
samp>hosts</
samp> file for local
testing, but that will work only from the machine with those
<
p><
strong>Server configuration:</
strong></
p>
# Ensure that Apache listens on port 80
# Listen for virtual host requests on all IP addresses
<
p>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. That means
that if a request is received that does not match one of the specified
<
samp>ServerName</
samp> directives, it will be served by this first
<
samp>VirtualHost</
samp>.</
p>
<
p>You can, if you wish, replace <
code>*</
code> with the actual
IP address of the system. In that case, the argument to
<
code>VirtualHost</
code> <
em>must</
em> match the argument to
<
code>NameVirtualHost</
code>:</
p>
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
<
p>However, it is additionally useful to use <
code>*</
code>
on systems where the IP address is not predictable - for
example if you have a dynamic IP address with your ISP, and
you are using some variety of dynamic DNS solution. Since
<
code>*</
code> matches any IP address, this configuration
would work without changes whenever your IP address
<
p>The above configuration is what you will want to use in almost
all name-based virtual hosting situations. The only think that this
configuration will not work for, in fact, is when you are serving
different content based on differing IP addresses or ports.</
p>
<
h3><
a id="twoips" name="twoips">Name-based hosts on more than one
<
td bgcolor="#e0e5f5"><
strong>Note:</
strong> Any of the
techniques discussed here can be extended to any number of IP
<
strong>Setup 1:</
strong>
<
p>The server has two IP addresses. On one (<
samp>172.20.30.40</
samp>), we
other (<
samp>172.20.30.50</
samp>), we will serve two or more virtual hosts.</
p>
<
p><
strong>Server configuration:</
strong></
p>
# This is the "main" server running on 172.20.30.40
# This is the other address
NameVirtualHost 172.20.30.50
<VirtualHost 172.20.30.50>
# Other directives here ...
<VirtualHost 172.20.30.50>
# Other directives here ...
<
p>Any request to an address other than <
samp>172.20.30.50</
samp> will be
served from the main server. A request to <
samp>172.20.30.50</
samp> with an
unknown hostname, or no <
code>Host:</
code> header, will be served from
<
h3><
a id="intraextra" name="intraextra">Serving the same content on
different IP addresses (such as an internal and external
<
p>The server machine has two IP addresses (<
samp>192.168.1.1</
samp>
and <
samp>172.20.30.40</
samp>). The machine is sitting between an
internal (intranet) network and an external (internet) network. Outside
the external address (<
samp>172.20.30.40</
samp>), but inside the
network, that same name resolves to the internal address
(<
samp>192.168.1.1</
samp>).</
p>
<
p>The server can be made to respond to internal and external requests
with the same content, with just one <
code>VirtualHost</
code>
<
p><
strong>Server configuration:</
strong></
p>
NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40
<VirtualHost 192.168.1.1 172.20.30.40>
<
p>Now requests from both networks will be served from the same
<
code>VirtualHost</
code>.</
p>
<
p><
strong>Note:</
strong> On the internal
network, one can just use the name <
code>server</
code> rather
than the fully qualified host name
<
p>Note also that, in the above example, you can replace the list
of IP addresses with <
code>*</
code>, which will cause the server to
respond the same on all addresses.</
p>
<
h3><
a id="port" name="port">Running different sites on different
<
p><
strong>Setup:</
strong></
p>
<
p>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>
<
p><
strong>Server configuration:</
strong></
p>
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
<VirtualHost 172.20.30.40:80>
<VirtualHost 172.20.30.40:8080>
<VirtualHost 172.20.30.40:80>
<VirtualHost 172.20.30.40:8080>
<
h3><
a id="ip" name="ip">IP-based virtual hosting</
a></
h3>
<
p><
strong>Setup:</
strong></
p>
<
p>The server has two IP addresses (<
samp>172.20.30.40</
samp> and
<
samp>172.20.30.50</
samp>) which resolve to the names
<
p><
strong>Server configuration:</
strong></
p>
<VirtualHost 172.20.30.40>
<VirtualHost 172.20.30.50>
<
p>Requests for any address not specified in one of the
<
samp><VirtualHost></
samp> directives (such as
<
samp>localhost</
samp>, for example) will go to the main server, if
<
h3><
a id="#ipport" name="#ipport">Mixed port-based and ip-based
<
p><
strong>Setup:</
strong></
p>
<
p>The server machine has two IP addresses (<
samp>172.20.30.40</
samp> and
<
samp>172.20.30.50</
samp>) which resolve to the names
respectively. In each case, we want to run hosts on ports 80 and
<VirtualHost 172.20.30.40:80>
<VirtualHost 172.20.30.40:8080>
<VirtualHost 172.20.30.50:80>
<VirtualHost 172.20.30.50:8080>
<
h3><
a id="mixed" name="mixed">Mixed name-based and IP-based
<
p><
strong>Setup:</
strong></
p>
<
p>On some of my addresses, I want to do name-based virtual hosts, and
on others, IP-based hosts.</
p>
<
p><
strong>Server configuration:</
strong></
p>
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
<VirtualHost 172.20.30.40>
<VirtualHost 172.20.30.40>
<VirtualHost 172.20.30.50>
<VirtualHost 172.20.30.60>
<
h3><
a id="default" name="default">Using <
code>_default_</
code>
<
p><
strong>Setup 1:</
strong></
p>
<
p>Catching <
em>every</
em> request to any unspecified IP address and
any other virtual host.</
p>
<
p><
strong>Server configuration:</
strong></
p>
<VirtualHost _default_:*>
<
p>Using such a default vhost with a wildcard port effectively prevents
any request going to the main server.</
p>
<
p>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 file).</
p>
to rewrite any request to a single information page (or script).</
p>
<
p><
strong>Setup 2:</
strong></
p>
<
p>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>
<
p><
strong>Server configuration:</
strong></
p>
<VirtualHost _default_:80>
<VirtualHost _default_:*>
<
p>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
<
p><
strong>Setup 3:</
strong></
p>
<
p>We want to have a default vhost for port 80, but no other default
<
p><
strong>Server configuration:</
strong></
p>
<VirtualHost _default_:80>
<
p>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.</
p>
<
h3><
a id="migrate" name="migrate">Migrating a name-based vhost to an
<
p><
strong>Setup:</
strong></
p>
<
p>The name-based vhost with the hostname
href="#name">name-based</
a> 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>172.20.30.50</
samp>) to the <
code>VirtualHost</
code>
<
p><
strong>Server configuration:</
strong></
p>
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40 172.20.30.50>
<VirtualHost 172.20.30.40>
<
p>The vhost can now be accessed through the new address (as an
IP-based vhost) and through the old address (as a name-based
<
h3><
a id="serverpath" name="serverpath">Using the
<
code>ServerPath</
code> directive</
a></
h3>
<
p><
strong>Setup:</
strong></
p>
<
p>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
<
p><
strong>Server configuration:</
strong></
p>
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
<VirtualHost 172.20.30.40>
<VirtualHost 172.20.30.40>
href="/mod/core.html#serverpath"><
code>ServerPath</
code></
a>
directive a request to the URL
from the sub1-vhost.<
br />
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
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.</
p>