examples.html.en revision 261c62b53a9c707c64c7824eed68b5463bd23e62
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
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<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<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs-project/">Documentation</a> > <a href="../">Version 2.1</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>VirtualHost Examples</h1>
a7835c7a8cf86045fdaee65dc2839bfe6314fb1end<p><span>Available Languages: </span><a href="/en/vhosts/examples.html" title="English"> en </a> |
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<a href="/ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
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<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#purename">Running several name-based web
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#twoips">Name-based hosts on more than one
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<li><img alt="" src="/images/down.gif" /> <a href="#port">Running different sites on different
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<li><img alt="" src="/images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#default">Using <code>_default_</code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<li><img alt="" src="/images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<h2><a name="purename" id="purename">Running several name-based web
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 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 class="example"><h3>Server configuration</h3><p><code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Ensure that Apache listens on port 80<br />
46d1ef8cb385aa2f519ce7d355afc51f144bd938slive Listen 80<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Listen for virtual host requests on all IP addresses<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive NameVirtualHost *:80<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <VirtualHost *:80><br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </VirtualHost><br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <VirtualHost *:80><br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here<br />
5e9423b4ca454c6416a2dc465dea1b1d34cec7a9slive </VirtualHost>
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
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 NameVirtualHost 172.20.30.40<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <VirtualHost 172.20.30.40><br />
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 <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 </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
01979b991e320f79fe2c1ae2afc298e8651bd69aslive<h2><a name="twoips" id="twoips">Name-based hosts on more than one
01979b991e320f79fe2c1ae2afc298e8651bd69aslive techniques discussed here can be extended to any number of IP
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive addresses.</p>
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 <div class="example"><h3>Server configuration</h3><p><code>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive Listen 80<br />
01979b991e320f79fe2c1ae2afc298e8651bd69aslive # This is the "main" server running on 172.20.30.40<br />
01979b991e320f79fe2c1ae2afc298e8651bd69aslive # This is the other address<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive NameVirtualHost 172.20.30.50<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive <VirtualHost 172.20.30.50><br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here ...<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </VirtualHost><br />
368bcafaedaee463f769c1b5f3547b9970df90d0slive <VirtualHost 172.20.30.50><br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive # Other directives here ...<br />
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive </VirtualHost>
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 </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive<h2><a name="intraextra" id="intraextra">Serving the same content on
3fa58e00171aebf3b2cfa90035ed530f5b1f651dslive different IP addresses (such as an internal and external
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
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 <div class="example"><h3>Server configuration</h3><p><code>
1cb160a981947e75a8353da3fe40a653aa87100eslive NameVirtualHost 192.168.1.1<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive NameVirtualHost 172.20.30.40<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive <VirtualHost 192.168.1.1 172.20.30.40><br />
1cb160a981947e75a8353da3fe40a653aa87100eslive ServerAlias server<br />
1cb160a981947e75a8353da3fe40a653aa87100eslive </VirtualHost>
1cb160a981947e75a8353da3fe40a653aa87100eslive <p>Now requests from both networks will be served from the same
1cb160a981947e75a8353da3fe40a653aa87100eslive network, one can just use the name <code>server</code> rather
1cb160a981947e75a8353da3fe40a653aa87100eslive than the fully qualified host name
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><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
<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
<code>www.example.org</code> (from our <a href="#name">name-based</a> example, setup 2) should get its own IP
The <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives
<p><span>Available Languages: </span><a href="/en/vhosts/examples.html" title="English"> en </a> |
<a href="/ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
<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>