examples.html.en revision 261c62b53a9c707c64c7824eed68b5463bd23e62
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<?xml version="1.0" encoding="ISO-8859-1"?>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive This file is generated from xml source: DO NOT EDIT
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6fbd2e53c97ea6976d93e0ac521adabc55e0fb73nd -->
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<title>VirtualHost Examples - Apache HTTP Server</title>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<link href="/images/favicon.ico" rel="shortcut icon" /></head>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<body id="manual-page"><div id="page-header">
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<p class="apache">Apache HTTP Server Version 2.1</p>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<img alt="" src="/images/feather.gif" /></div>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<div id="path">
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs-project/">Documentation</a> &gt; <a href="../">Version 2.1</a> &gt; <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>VirtualHost Examples</h1>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<div class="toplang">
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<p><span>Available Languages: </span><a href="/en/vhosts/examples.html" title="English">&nbsp;en&nbsp;</a> |
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<a href="/ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive</div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <p>This document attempts to answer the commonly-asked questions about
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive setting up virtual hosts. These scenarios are those involving multiple
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive 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
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive coming soon about running sites on several servers behind a single
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive proxy server.</p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive</div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#purename">Running several name-based web
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive sites on a single IP address.</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive IP address.</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#intraextra">Serving the same content on
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive different IP addresses (such as an internal and external
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive address).</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#port">Running different sites on different
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive ports.</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive hosts</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive vhosts</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#default">Using <code>_default_</code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive vhosts</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive IP-based vhost</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive directive</a></li>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive</ul></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<div class="section">
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<h2><a name="purename" id="purename">Running several name-based web
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive sites on a single IP address.</a></h2>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <p>Your server has a single IP address, and multiple aliases (CNAMES)
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive point to this machine in DNS. You want to run a web server for
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <code>www.example.com</code> and <code>www.example.org</code> on this
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive machine.</p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <div class="note"><h3>Note</h3><p>Creating virtual
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive host configurations on your Apache server does not magically
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive cause DNS entries to be created for those host names. You
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <em>must</em> have the names in DNS, resolving to your IP
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive address, or nobody else will be able to see your web site. You
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive can put entries in your <code>hosts</code> file for local
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive testing, but that will work only from the machine with those
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive hosts entries.</p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <div class="example"><h3>Server configuration</h3><p><code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
1cb160a981947e75a8353da3fe40a653aa87100eslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Ensure that Apache listens on port 80<br />
46d1ef8cb385aa2f519ce7d355afc51f144bd938slive Listen 80<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Listen for virtual host requests on all IP addresses<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive NameVirtualHost *:80<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive &lt;VirtualHost *:80&gt;<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive <span class="indent">
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive DocumentRoot /www/example1<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive ServerName www.example.com<br />
46d1ef8cb385aa2f519ce7d355afc51f144bd938slive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </span>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive &lt;/VirtualHost&gt;<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive &lt;VirtualHost *:80&gt;<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <span class="indent">
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive DocumentRoot /www/example2<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive ServerName www.example.org<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </span>
5e9423b4ca454c6416a2dc465dea1b1d34cec7a9slive &lt;/VirtualHost&gt;
5e9423b4ca454c6416a2dc465dea1b1d34cec7a9slive </code></p></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
58842e03db54033d15cc224d997f467abc88a97aslive <p>The asterisks match all addresses, so the main server serves no
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive requests. Due to the fact that <code>www.example.com</code> is first
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive in the configuration file, it has the highest priority and can be seen
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive as the <cite>default</cite> or <cite>primary</cite> server. That means
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive that if a request is received that does not match one of the specified
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <code>ServerName</code> directives, it will be served by this first
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <code>VirtualHost</code>.</p>
46d1ef8cb385aa2f519ce7d355afc51f144bd938slive
1c6a10274e908898347be82bc82bc7ae18c66410slive <div class="note">
1c6a10274e908898347be82bc82bc7ae18c66410slive <h3>Note</h3>
1c6a10274e908898347be82bc82bc7ae18c66410slive
1c6a10274e908898347be82bc82bc7ae18c66410slive <p>You can, if you wish, replace <code>*</code> with the actual
1c6a10274e908898347be82bc82bc7ae18c66410slive IP address of the system. In that case, the argument to
1c6a10274e908898347be82bc82bc7ae18c66410slive <code>VirtualHost</code> <em>must</em> match the argument to
1c6a10274e908898347be82bc82bc7ae18c66410slive <code>NameVirtualHost</code>:</p>
1c6a10274e908898347be82bc82bc7ae18c66410slive
1c6a10274e908898347be82bc82bc7ae18c66410slive <div class="example"><p><code>
1c6a10274e908898347be82bc82bc7ae18c66410slive NameVirtualHost 172.20.30.40<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive &lt;VirtualHost 172.20.30.40&gt;<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # etc ...
46d1ef8cb385aa2f519ce7d355afc51f144bd938slive </code></p></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <p>However, it is additionally useful to use <code>*</code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive on systems where the IP address is not predictable - for
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive example if you have a dynamic IP address with your ISP, and
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive you are using some variety of dynamic DNS solution. Since
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <code>*</code> matches any IP address, this configuration
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive would work without changes whenever your IP address
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive changes.</p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <p>The above configuration is what you will want to use in almost
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive all name-based virtual hosting situations. The only thing that this
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive configuration will not work for, in fact, is when you are serving
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive different content based on differing IP addresses or ports.</p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<div class="section">
01979b991e320f79fe2c1ae2afc298e8651bd69aslive<h2><a name="twoips" id="twoips">Name-based hosts on more than one
01979b991e320f79fe2c1ae2afc298e8651bd69aslive IP address.</a></h2>
01979b991e320f79fe2c1ae2afc298e8651bd69aslive
01979b991e320f79fe2c1ae2afc298e8651bd69aslive <div class="note">
01979b991e320f79fe2c1ae2afc298e8651bd69aslive <h3>Note</h3><p>Any of the
01979b991e320f79fe2c1ae2afc298e8651bd69aslive techniques discussed here can be extended to any number of IP
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive addresses.</p>
01979b991e320f79fe2c1ae2afc298e8651bd69aslive </div>
01979b991e320f79fe2c1ae2afc298e8651bd69aslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive will serve the "main" server, <code>server.domain.com</code> and on the
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <div class="example"><h3>Server configuration</h3><p><code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive Listen 80<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
01979b991e320f79fe2c1ae2afc298e8651bd69aslive # This is the "main" server running on 172.20.30.40<br />
01979b991e320f79fe2c1ae2afc298e8651bd69aslive ServerName server.domain.com<br />
01979b991e320f79fe2c1ae2afc298e8651bd69aslive DocumentRoot /www/mainserver<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
01979b991e320f79fe2c1ae2afc298e8651bd69aslive # This is the other address<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive NameVirtualHost 172.20.30.50<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive &lt;VirtualHost 172.20.30.50&gt;<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <span class="indent">
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive DocumentRoot /www/example1<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive ServerName www.example.com<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here ...<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </span>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive &lt;/VirtualHost&gt;<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
368bcafaedaee463f769c1b5f3547b9970df90d0slive &lt;VirtualHost 172.20.30.50&gt;<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <span class="indent">
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive DocumentRoot /www/example2<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive ServerName www.example.org<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here ...<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </span>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive &lt;/VirtualHost&gt;
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </code></p></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <p>Any request to an address other than <code>172.20.30.50</code> will be
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive served from the main server. A request to <code>172.20.30.50</code> with an
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive unknown hostname, or no <code>Host:</code> header, will be served from
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <code>www.example.com</code>.</p>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<div class="section">
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<h2><a name="intraextra" id="intraextra">Serving the same content on
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive different IP addresses (such as an internal and external
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive address).</a></h2>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <p>The server machine has two IP addresses (<code>192.168.1.1</code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive and <code>172.20.30.40</code>). The machine is sitting between an
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive internal (intranet) network and an external (internet) network. Outside
46d1ef8cb385aa2f519ce7d355afc51f144bd938slive of the network, the name <code>server.example.com</code> resolves to
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive the external address (<code>172.20.30.40</code>), but inside the
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive network, that same name resolves to the internal address
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive (<code>192.168.1.1</code>).</p>
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive <p>The server can be made to respond to internal and external requests
1cb160a981947e75a8353da3fe40a653aa87100eslive with the same content, with just one <code>VirtualHost</code>
1cb160a981947e75a8353da3fe40a653aa87100eslive section.</p>
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive <div class="example"><h3>Server configuration</h3><p><code>
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive NameVirtualHost 192.168.1.1<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive NameVirtualHost 172.20.30.40<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive <br />
1cb160a981947e75a8353da3fe40a653aa87100eslive &lt;VirtualHost 192.168.1.1 172.20.30.40&gt;<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive <span class="indent">
1cb160a981947e75a8353da3fe40a653aa87100eslive DocumentRoot /www/server1<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive ServerName server.example.com<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive ServerAlias server<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive </span>
1cb160a981947e75a8353da3fe40a653aa87100eslive &lt;/VirtualHost&gt;
1cb160a981947e75a8353da3fe40a653aa87100eslive </code></p></div>
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive <p>Now requests from both networks will be served from the same
1cb160a981947e75a8353da3fe40a653aa87100eslive <code>VirtualHost</code>.</p>
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive <div class="note">
1cb160a981947e75a8353da3fe40a653aa87100eslive <h3>Note:</h3><p>On the internal
1cb160a981947e75a8353da3fe40a653aa87100eslive network, one can just use the name <code>server</code> rather
1cb160a981947e75a8353da3fe40a653aa87100eslive than the fully qualified host name
1cb160a981947e75a8353da3fe40a653aa87100eslive <code>server.example.com</code>.</p>
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive <p>Note also that, in the above example, you can replace the list
1cb160a981947e75a8353da3fe40a653aa87100eslive of IP addresses with <code>*</code>, which will cause the server to
1cb160a981947e75a8353da3fe40a653aa87100eslive respond the same on all addresses.</p>
1cb160a981947e75a8353da3fe40a653aa87100eslive </div>
1cb160a981947e75a8353da3fe40a653aa87100eslive
1cb160a981947e75a8353da3fe40a653aa87100eslive </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
1cb160a981947e75a8353da3fe40a653aa87100eslive<div class="section">
<h2><a name="port" id="port">Running different sites on different
ports.</a></h2>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
Listen 80<br />
Listen 8080<br />
<br />
NameVirtualHost 172.20.30.40:80<br />
NameVirtualHost 172.20.30.40:8080<br />
<br />
&lt;VirtualHost 172.20.30.40:80&gt;<br />
<span class="indent">
ServerName www.example.com<br />
DocumentRoot /www/domain-80<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40:8080&gt;<br />
<span class="indent">
ServerName www.example.com<br />
DocumentRoot /www/domain-8080<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40:80&gt;<br />
<span class="indent">
ServerName www.example.org<br />
DocumentRoot /www/otherdomain-80<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40:8080&gt;<br />
<span class="indent">
ServerName www.example.org<br />
DocumentRoot /www/otherdomain-8080<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="ip" id="ip">IP-based virtual hosting</a></h2>
<p>The server has two IP addresses (<code>172.20.30.40</code> and
<code>172.20.30.50</code>) which resolve to the names
<code>www.example.com</code> and <code>www.example.org</code>
respectively.</p>
<div class="example"><h3>Server configuration</h3><p><code>
Listen 80<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
<span class="indent">
DocumentRoot /www/example1<br />
ServerName www.example.com<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.50&gt;<br />
<span class="indent">
DocumentRoot /www/example2<br />
ServerName www.example.org<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
<p>Requests for any address not specified in one of the
<code>&lt;VirtualHost&gt;</code> directives (such as
<code>localhost</code>, for example) will go to the main server, if
there is one.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
hosts</a></h2>
<p>The server machine has two IP addresses (<code>172.20.30.40</code> and
<code>172.20.30.50</code>) which resolve to the names
<code>www.example.com</code> and <code>www.example.org</code>
respectively. In each case, we want to run hosts on ports 80 and
8080.</p>
<div class="example"><h3>Server configuration</h3><p><code>
Listen 172.20.30.40:80<br />
Listen 172.20.30.40:8080<br />
Listen 172.20.30.50:80<br />
Listen 172.20.30.50:8080<br />
<br />
&lt;VirtualHost 172.20.30.40:80&gt;<br />
<span class="indent">
DocumentRoot /www/example1-80<br />
ServerName www.example.com<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40:8080&gt;<br />
<span class="indent">
DocumentRoot /www/example1-8080<br />
ServerName www.example.com<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.50:80&gt;<br />
<span class="indent">
DocumentRoot /www/example2-80<br />
ServerName www.example.org<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.50:8080&gt;<br />
<span class="indent">
DocumentRoot /www/example2-8080<br />
ServerName www.example.org<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="mixed" id="mixed">Mixed name-based and IP-based
vhosts</a></h2>
<p>On some of my addresses, I want to do name-based virtual hosts, and
on others, IP-based hosts.</p>
<div class="example"><h3>Server configuration</h3><p><code>
Listen 80<br />
<br />
NameVirtualHost 172.20.30.40<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
<span class="indent">
DocumentRoot /www/example1<br />
ServerName www.example.com<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
<span class="indent">
DocumentRoot /www/example2<br />
ServerName www.example.org<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
<span class="indent">
DocumentRoot /www/example3<br />
ServerName www.example3.net<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
# IP-based<br />
&lt;VirtualHost 172.20.30.50&gt;<br />
<span class="indent">
DocumentRoot /www/example4<br />
ServerName www.example4.edu<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.60&gt;<br />
<span class="indent">
DocumentRoot /www/example5<br />
ServerName www.example5.gov<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="default" id="default">Using <code>_default_</code>
vhosts</a></h2>
<h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
for all ports</a></h3>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
&lt;VirtualHost _default_:*&gt;<br />
<span class="indent">
DocumentRoot /www/default<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
<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 <code class="directive"><a href="/mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
<code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
request to a single information page (or script).</p>
<h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
for different ports</a></h3>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
&lt;VirtualHost _default_:80&gt;<br />
<span class="indent">
DocumentRoot /www/default80<br />
# ...<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost _default_:*&gt;<br />
<span class="indent">
DocumentRoot /www/default<br />
# ...<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
<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>
<h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
for one port</a></h3>
<p>We want to have a default vhost for port 80, but no other default
vhosts.</p>
<div class="example"><h3>Server configuration</h3><p><code>
&lt;VirtualHost _default_:80&gt;<br />
DocumentRoot /www/default<br />
...<br />
&lt;/VirtualHost&gt;
</code></p></div>
<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>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
IP-based vhost</a></h2>
<p>The name-based vhost with the hostname
<code>www.example.org</code> (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
(<code>172.20.30.50</code>) to the <code>VirtualHost</code>
directive.</p>
<div class="example"><h3>Server configuration</h3><p><code>
Listen 80<br />
ServerName www.example.com<br />
DocumentRoot /www/example1<br />
<br />
NameVirtualHost 172.20.30.40<br />
<br />
&lt;VirtualHost 172.20.30.40 172.20.30.50&gt;<br />
<span class="indent">
DocumentRoot /www/example2<br />
ServerName www.example.org<br />
# ...<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
<span class="indent">
DocumentRoot /www/example3<br />
ServerName www.example.net<br />
ServerAlias *.example.net<br />
# ...<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
<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>
</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<div class="section">
<h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
directive</a></h2>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
NameVirtualHost 172.20.30.40<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
<span class="indent">
# primary vhost<br />
DocumentRoot /www/subdomain<br />
RewriteEngine On<br />
RewriteRule ^/.* /www/subdomain/index.html<br />
# ...<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
DocumentRoot /www/subdomain/sub1<br />
<span class="indent">
ServerName www.sub1.domain.tld<br />
ServerPath /sub1/<br />
RewriteEngine On<br />
RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
# ...<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 172.20.30.40&gt;<br />
<span class="indent">
DocumentRoot /www/subdomain/sub2<br />
ServerName www.sub2.domain.tld<br />
ServerPath /sub2/<br />
RewriteEngine On<br />
RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
# ...<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>
<p>Due to the <code class="directive"><a href="/mod/core.html#serverpath">ServerPath</a></code>
directive a request to the URL
<code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served
from the sub1-vhost.<br /> A request to the URL
<code>http://www.sub1.domain.tld/</code> 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
<code>http://www.sub2.domain.tld/sub1/</code> is also served from the
sub1-vhost if the client sent no <code>Host:</code> header.<br />
The <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></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>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/vhosts/examples.html" title="English">&nbsp;en&nbsp;</a> |
<a href="/ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
</div><div id="footer">
<p class="apache">Copyright 1999-2004 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div>
</body></html>