examples.html revision 210ac7f213498949b68ee13b33fd9324d08ff81f
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<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>
<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="name-based.html">name-based</a> or <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
proxy server.</p>
<h2>Examples</h2>
<ul>
<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
address.</a></li>
<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
ports.</a></li>
<li><a href="#ip">IP-based virtual hosting</a></li>
<li><a href="#ipport">Mixed port-based and ip-based virtual
hosts</a></li>
<li><a href="#mixed">Mixed name-based and IP-based vhosts</a></li>
</ul>
<!--
<h2>Common problems</h2>
<ul>
<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
first vhost)</li>
<li>I'm trying to use SSL on my virtual host, and I'm getting the
wrong site</li>
</ul>
-->
<h2>Additional features</h2>
<ul>
<li><a href="#default">Using <code>_default_</code> vhosts</a></li>
<li><a href="#migrate">Migrating a named-based vhost to an IP-based
vhost</a></li>
<li><a href="#serverpath">Using the <code>ServerPath</code>
directive</a></li>
</ul>
<hr />
<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
<samp>www.example1.com</samp> and <samp>www.example2.org</samp> on this
machine.</p>
<blockquote>
<table>
<tr>
<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
hosts entries.</td>
</tr>
</table>
</blockquote>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *
&lt;VirtualHost *&gt;
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here
&lt;/VirtualHost&gt;
&lt;VirtualHost *&gt;
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<p>The asterisks match all addresses, so the main server serves no
requests. Due to the fact that <samp>www.example1.com</samp> is first
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>
<blockquote>
<table>
<tr>
<td bgcolor="#e0e5f5">
<strong>Note:</strong>
<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>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
NameVirtualHost 172.20.30.40
&lt;VirtualHost 172.20.30.40&gt;
# etc ...
</pre>
</td>
</tr>
</table>
</blockquote>
<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
changes.</p>
</td>
</tr>
</table>
</blockquote>
<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>
<hr />
<h3><a id="twoips" name="twoips">Name-based hosts on more than one
IP address.</a></h3>
<blockquote>
<table>
<tr>
<td bgcolor="#e0e5f5"><strong>Note:</strong> Any of the
techniques discussed here can be extended to any number of IP
addresses.</td>
</tr>
</table>
</blockquote>
<strong>Setup 1:</strong>
<p>The server has two IP addresses. On one (<samp>172.20.30.40</samp>), we
will serve the "main" server, <samp>server.domain.com</samp> and on the
other (<samp>172.20.30.50</samp>), we will serve two or more virtual hosts.</p>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
Listen 80
# This is the "main" server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver
# This is the other address
NameVirtualHost 172.20.30.50
&lt;VirtualHost 172.20.30.50&gt;
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here ...
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.50&gt;
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here ...
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<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
<samp>www.example1.com</samp>.</p>
<h3><a id="intraextra" name="intraextra">Serving the same content on
different IP addresses (such as an internal and external
address).</a></h3>
<strong>Setup:</strong>
<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
of the network, the name <samp>server.example.com</samp> resolves to
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>
section.</p>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40
&lt;VirtualHost 192.168.1.1 172.20.30.40&gt;
DocumentRoot /www/server1
ServerName server.example.com
ServerAlias server
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<p>Now requests from both networks will be served from the same
<code>VirtualHost</code>.</p>
<blockquote>
<table>
<tr>
<td bgcolor="#e0e5f5">
<p><strong>Note:</strong> On the internal
network, one can just use the name <code>server</code> rather
than the fully qualified host name
<code>server.example.com</code>.</p>
<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>
</td>
</tr>
</table>
</blockquote>
<hr />
<h3><a id="port" name="port">Running different sites on different
ports.</a></h3>
<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 &lt;VirtualHost
name:port&gt; 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>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
&lt;VirtualHost 172.20.30.40:80&gt;
ServerName www.example1.com
DocumentRoot /www/domain-80
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40:8080&gt;
ServerName www.example1.com
DocumentRoot /www/domain-8080
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40:80&gt;
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40:8080&gt;
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<hr />
<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
<samp>www.example1.com</samp> and <samp>www.example2.org</samp>
respectively.</p>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
Listen 80
&lt;VirtualHost 172.20.30.40&gt;
DocumentRoot /www/example1
ServerName www.example1.com
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.50&gt;
DocumentRoot /www/example2
ServerName www.example2.org
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<p>Requests for any address not specified in one of the
<samp>&lt;VirtualHost&gt;</samp> directives (such as
<samp>localhost</samp>, for example) will go to the main server, if
there is one.</p>
<hr />
<h3><a id="ipport" name="ipport">Mixed port-based and ip-based
virtual hosts</a></h3>
<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
<samp>www.example1.com</samp> and <samp>www.example2.org</samp>
respectively. In each case, we want to run hosts on ports 80 and
8080.</p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
&lt;VirtualHost 172.20.30.40:80&gt;
DocumentRoot /www/example1-80
ServerName www.example1.com
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40:8080&gt;
DocumentRoot /www/example1-8080
ServerName www.example1.com
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.50:80&gt;
DocumentRoot /www/example2-80
ServerName www.example1.org
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.50:8080&gt;
DocumentRoot /www/example2-8080
ServerName www.example2.org
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<hr />
<h3><a id="mixed" name="mixed">Mixed name-based and IP-based
vhosts</a></h3>
<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>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
Listen 80
NameVirtualHost 172.20.30.40
&lt;VirtualHost 172.20.30.40&gt;
DocumentRoot /www/example1
ServerName www.example1.com
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40&gt;
DocumentRoot /www/example2
ServerName www.example2.org
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40&gt;
DocumentRoot /www/example3
ServerName www.example3.net
&lt;/VirtualHost&gt;
# IP-based
&lt;VirtualHost 172.20.30.50&gt;
DocumentRoot /www/example4
ServerName www.example4.edu
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.60&gt;
DocumentRoot /www/example5
ServerName www.example5.gov
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<hr />
<h3><a id="default" name="default">Using <code>_default_</code>
vhosts</a></h3>
<p><strong>Setup 1:</strong></p>
<p>Catching <em>every</em> request to any unspecified IP address and
port, <em>i.e.</em>, an address/port combination that is not used for
any other virtual host.</p>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
&lt;VirtualHost _default_:*&gt;
DocumentRoot /www/default
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<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>
<p>You can use <a
href="/mod/mod_alias.html#aliasmatch"><code>AliasMatch</code></a> or
<a
href="/mod/mod_rewrite.html#RewriteRule"><code>RewriteRule</code></a>
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>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
&lt;VirtualHost _default_:80&gt;
DocumentRoot /www/default80
# ...
&lt;/VirtualHost&gt;
&lt;VirtualHost _default_:*&gt;
DocumentRoot /www/default
# ...
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<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
request.</p>
<p><strong>Setup 3:</strong></p>
<p>We want to have a default vhost for port 80, but no other default
vhosts.</p>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
&lt;VirtualHost _default_:80&gt;
DocumentRoot /www/default
...
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<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>
<hr />
<h3><a id="migrate" name="migrate">Migrating a name-based vhost to an
IP-based vhost</a></h3>
<p><strong>Setup:</strong></p>
<p>The name-based vhost with the hostname
<samp>www.example2.org</samp> (from our <a
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>
directive.</p>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
Listen 80
ServerName www.example1.com
DocumentRoot /www/example1
NameVirtualHost 172.20.30.40
&lt;VirtualHost 172.20.30.40 172.20.30.50&gt;
DocumentRoot /www/example2
ServerName www.example2.org
# ...
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40&gt;
DocumentRoot /www/example3
ServerName www.example3.net
ServerAlias *.example3.net
# ...
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<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
vhost).</p>
<hr />
<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
hosts.</p>
<p><strong>Server configuration:</strong></p>
<blockquote>
<table cellpadding="10">
<tr>
<td bgcolor="#eeeeee">
<pre>
NameVirtualHost 172.20.30.40
&lt;VirtualHost 172.20.30.40&gt;
# primary vhost
DocumentRoot /www/subdomain
RewriteEngine On
RewriteRule ^/.* /www/subdomain/index.html
# ...
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40&gt;
DocumentRoot /www/subdomain/sub1
ServerName www.sub1.domain.tld
ServerPath /sub1/
RewriteEngine On
RewriteRule ^(/sub1/.*) /www/subdomain$1
# ...
&lt;/VirtualHost&gt;
&lt;VirtualHost 172.20.30.40&gt;
DocumentRoot /www/subdomain/sub2
ServerName www.sub2.domain.tld
ServerPath /sub2/
RewriteEngine On
RewriteRule ^(/sub2/.*) /www/subdomain$1
# ...
&lt;/VirtualHost&gt;
</pre>
</td>
</tr>
</table>
</blockquote>
<p>Due to the <a
href="/mod/core.html#serverpath"><code>ServerPath</code></a>
directive a request to the URL
<samp>http://www.sub1.domain.tld/sub1/</samp> is <em>always</em> served
from the sub1-vhost.<br />
A request to the URL <samp>http://www.sub1.domain.tld/</samp> 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
<samp>http://www.sub2.domain.tld/sub1/</samp> is also served from 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.</p>
<!--#include virtual="footer.html" -->
</body>
</html>