mirror of
https://github.com/apache/httpd.git
synced 2025-06-01 23:21:45 +03:00
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@472949 13f79535-47bb-0310-9956-ffa450edef68
428 lines
19 KiB
XML
428 lines
19 KiB
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
|
|
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
This file is generated from xml source: DO NOT EDIT
|
|
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
-->
|
|
<title>Dynamically Configured Mass Virtual Hosting - Apache HTTP Server</title>
|
|
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
|
|
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
|
|
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
|
|
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
|
|
<body id="manual-page"><div id="page-header">
|
|
<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>
|
|
<p class="apache">Apache HTTP Server Version 2.3</p>
|
|
<img alt="" src="../images/feather.gif" /></div>
|
|
<div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
|
|
<div id="path">
|
|
<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.3</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>Dynamically Configured Mass Virtual Hosting</h1>
|
|
<div class="toplang">
|
|
<p><span>Available Languages: </span><a href="../en/vhosts/mass.html" title="English"> en </a> |
|
|
<a href="../ko/vhosts/mass.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
|
|
</div>
|
|
|
|
|
|
<p>This document describes how to efficiently serve an
|
|
arbitrary number of virtual hosts with the Apache httpd webserver.
|
|
</p>
|
|
|
|
</div>
|
|
<div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#motivation">Motivation</a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#overview">Overview</a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#simple">Simple Dynamic Virtual Hosts</a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#homepages">A Virtually Hosted Homepages System</a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#combinations">Using Multiple Virtual
|
|
Hosting Systems on the Same Server</a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#ipbased">More Efficient IP-Based Virtual Hosting</a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#simple.rewrite">Simple Dynamic
|
|
Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#homepages.rewrite">A
|
|
Homepages System Using <code>mod_rewrite</code></a></li>
|
|
<li><img alt="" src="../images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual
|
|
Host Configuration File</a></li>
|
|
</ul></div>
|
|
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
|
|
<div class="section">
|
|
<h2><a name="motivation" id="motivation">Motivation</a></h2>
|
|
|
|
<p>The techniques described here are of interest if your
|
|
<code>httpd.conf</code> contains many
|
|
<code><VirtualHost></code> sections that are
|
|
substantially the same, for example:</p>
|
|
|
|
<div class="example"><p><code>
|
|
NameVirtualHost 111.22.33.44<br />
|
|
<VirtualHost 111.22.33.44><br />
|
|
<span class="indent">
|
|
ServerName www.customer-1.com<br />
|
|
DocumentRoot /www/hosts/www.customer-1.com/docs<br />
|
|
ScriptAlias /cgi-bin/ /www/hosts/www.customer-1.com/cgi-bin<br />
|
|
</span>
|
|
</VirtualHost><br />
|
|
<VirtualHost 111.22.33.44><br />
|
|
<span class="indent">
|
|
ServerName www.customer-2.com<br />
|
|
DocumentRoot /www/hosts/www.customer-2.com/docs<br />
|
|
ScriptAlias /cgi-bin/ /www/hosts/www.customer-2.com/cgi-bin<br />
|
|
</span>
|
|
</VirtualHost><br />
|
|
# blah blah blah<br />
|
|
<VirtualHost 111.22.33.44><br />
|
|
<span class="indent">
|
|
ServerName www.customer-N.com<br />
|
|
DocumentRoot /www/hosts/www.customer-N.com/docs<br />
|
|
ScriptAlias /cgi-bin/ /www/hosts/www.customer-N.com/cgi-bin<br />
|
|
</span>
|
|
</VirtualHost>
|
|
</code></p></div>
|
|
|
|
<p>The basic idea is to replace all of the static
|
|
<code><VirtualHost></code> configurations with a mechanism
|
|
that works them out dynamically. This has a number of
|
|
advantages:</p>
|
|
|
|
<ol>
|
|
<li>Your configuration file is smaller, so Apache starts
|
|
more quickly and uses less memory.</li>
|
|
|
|
<li>Adding virtual hosts is simply a matter of creating the
|
|
appropriate directories in the filesystem and entries in the
|
|
DNS - you don't need to reconfigure or restart Apache.</li>
|
|
</ol>
|
|
|
|
<p>The main disadvantage is that you cannot have a different log file for
|
|
each virtual host; however, if you have many virtual hosts, doing
|
|
this can be a bad idea anyway, because of the number of file
|
|
descriptors needed. It is better to log to a pipe or a fifo, and arrange for
|
|
the process at the other end to distribute the logs to the customers.
|
|
(This can also be used to accumulate statistics, etc.).</p>
|
|
|
|
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
|
|
<div class="section">
|
|
<h2><a name="overview" id="overview">Overview</a></h2>
|
|
|
|
<p>A virtual host is defined by two pieces of information: its
|
|
IP address, and the contents of the <code>Host:</code> header
|
|
in the HTTP request. The dynamic mass virtual hosting technique
|
|
used here is based on automatically inserting this information into the
|
|
pathname of the file that is used to satisfy the request. This
|
|
can be most easily done by using <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code>
|
|
with Apache 2.0. Alternatively, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> can be used.
|
|
Both of these modules are disabled by default; you must enable
|
|
one of them when configuring and building Apache if you want to
|
|
use this technique.</p>
|
|
|
|
<p>A couple of things need to be `faked' to make the dynamic
|
|
virtual host look like a normal one. The most important is the
|
|
server name, which is used by Apache to generate
|
|
self-referential URLs etc. It is configured with the
|
|
<code>ServerName</code> directive, and it is available to CGIs
|
|
via the <code>SERVER_NAME</code> environment variable. The
|
|
actual value used at run time is controlled by the <code class="directive"><a href="../mod/core.html#usecanonicalname">UseCanonicalName</a></code>
|
|
setting. With <code>UseCanonicalName Off</code>, the server name
|
|
is taken from the contents of the <code>Host:</code> header in the
|
|
request. With <code>UseCanonicalName DNS</code>, it is taken from a
|
|
reverse DNS lookup of the virtual host's IP address. The former
|
|
setting is used for name-based dynamic virtual hosting, and the
|
|
latter is used for IP-based hosting. If Apache cannot work out
|
|
the server name because there is no <code>Host:</code> header,
|
|
or the DNS lookup fails, then the value configured with
|
|
<code>ServerName</code> is used instead.</p>
|
|
|
|
<p>The other thing to `fake' is the document root (configured
|
|
with <code>DocumentRoot</code> and available to CGIs via the
|
|
<code>DOCUMENT_ROOT</code> environment variable). In a normal
|
|
configuration, this is used by the core module when
|
|
mapping URIs to filenames, but when the server is configured to
|
|
do dynamic virtual hosting, that job must be taken over by another
|
|
module (either <code>mod_vhost_alias</code> or
|
|
<code>mod_rewrite</code>), which has a different way of doing
|
|
the mapping. Neither of these modules is responsible for
|
|
setting the <code>DOCUMENT_ROOT</code> environment variable so
|
|
if any CGIs or SSI documents make use of it, they will get a
|
|
misleading value.</p>
|
|
|
|
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
|
|
<div class="section">
|
|
<h2><a name="simple" id="simple">Simple Dynamic Virtual Hosts</a></h2>
|
|
|
|
<p>This extract from <code>httpd.conf</code> implements the
|
|
virtual host arrangement outlined in the <a href="#motivation">Motivation</a> section above, but in a
|
|
generic fashion using <code>mod_vhost_alias</code>.</p>
|
|
|
|
<div class="example"><p><code>
|
|
# get the server name from the Host: header<br />
|
|
UseCanonicalName Off<br />
|
|
<br />
|
|
# this log format can be split per-virtual-host based on the first field<br />
|
|
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
|
|
CustomLog logs/access_log vcommon<br />
|
|
<br />
|
|
# include the server name in the filenames used to satisfy requests<br />
|
|
VirtualDocumentRoot /www/hosts/%0/docs<br />
|
|
VirtualScriptAlias /www/hosts/%0/cgi-bin
|
|
</code></p></div>
|
|
|
|
<p>This configuration can be changed into an IP-based virtual
|
|
hosting solution by just turning <code>UseCanonicalName
|
|
Off</code> into <code>UseCanonicalName DNS</code>. The server
|
|
name that is inserted into the filename is then derived from
|
|
the IP address of the virtual host.</p>
|
|
|
|
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
|
|
<div class="section">
|
|
<h2><a name="homepages" id="homepages">A Virtually Hosted Homepages System</a></h2>
|
|
|
|
<p>This is an adjustment of the above system, tailored for an
|
|
ISP's homepages server. Using a slightly more complicated
|
|
configuration, we can select substrings of the server name to
|
|
use in the filename so that, for example, the documents for
|
|
<code>www.user.isp.com</code> are found in
|
|
<code>/home/user/</code>. It uses a single <code>cgi-bin</code>
|
|
directory instead of one per virtual host.</p>
|
|
|
|
<div class="example"><p><code>
|
|
# all the preliminary stuff is the same as above, then<br />
|
|
<br />
|
|
# include part of the server name in the filenames<br />
|
|
VirtualDocumentRoot /www/hosts/%2/docs<br />
|
|
<br />
|
|
# single cgi-bin directory<br />
|
|
ScriptAlias /cgi-bin/ /www/std-cgi/<br />
|
|
</code></p></div>
|
|
|
|
<p>There are examples of more complicated
|
|
<code>VirtualDocumentRoot</code> settings in the
|
|
<code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> documentation.</p>
|
|
|
|
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
|
|
<div class="section">
|
|
<h2><a name="combinations" id="combinations">Using Multiple Virtual
|
|
Hosting Systems on the Same Server</a></h2>
|
|
|
|
<p>With more complicated setups, you can use Apache's normal
|
|
<code><VirtualHost></code> directives to control the
|
|
scope of the various virtual hosting configurations. For
|
|
example, you could have one IP address for general customers' homepages,
|
|
and another for commercial customers, with the following setup.
|
|
This can, of course, be combined with conventional
|
|
<code><VirtualHost></code> configuration sections.</p>
|
|
|
|
<div class="example"><p><code>
|
|
UseCanonicalName Off<br />
|
|
<br />
|
|
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
|
|
<br />
|
|
<Directory /www/commercial><br />
|
|
<span class="indent">
|
|
Options FollowSymLinks<br />
|
|
AllowOverride All<br />
|
|
</span>
|
|
</Directory><br />
|
|
<br />
|
|
<Directory /www/homepages><br />
|
|
<span class="indent">
|
|
Options FollowSymLinks<br />
|
|
AllowOverride None<br />
|
|
</span>
|
|
</Directory><br />
|
|
<br />
|
|
<VirtualHost 111.22.33.44><br />
|
|
<span class="indent">
|
|
ServerName www.commercial.isp.com<br />
|
|
<br />
|
|
CustomLog logs/access_log.commercial vcommon<br />
|
|
<br />
|
|
VirtualDocumentRoot /www/commercial/%0/docs<br />
|
|
VirtualScriptAlias /www/commercial/%0/cgi-bin<br />
|
|
</span>
|
|
</VirtualHost><br />
|
|
<br />
|
|
<VirtualHost 111.22.33.45><br />
|
|
<span class="indent">
|
|
ServerName www.homepages.isp.com<br />
|
|
<br />
|
|
CustomLog logs/access_log.homepages vcommon<br />
|
|
<br />
|
|
VirtualDocumentRoot /www/homepages/%0/docs<br />
|
|
ScriptAlias /cgi-bin/ /www/std-cgi/<br />
|
|
</span>
|
|
</VirtualHost>
|
|
</code></p></div>
|
|
|
|
<div class="note">
|
|
<h3>Note</h3>
|
|
<p>If the first VirtualHost block does <em>not</em> include a
|
|
<code class="directive"><a href="../mod/core.html#servername">ServerName</a></code> directive, the reverse
|
|
DNS of the relevant IP will be used instead.
|
|
If this is not the server name you
|
|
wish to use, a bogus entry (<code>ServerName
|
|
none.example.com</code>) can be added to get around this
|
|
behaviour.</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="ipbased" id="ipbased">More Efficient IP-Based Virtual Hosting</a></h2>
|
|
|
|
<p>The configuration changes suggested to turn <a href="#simple">the first
|
|
example</a> into an IP-based virtual hosting setup result in
|
|
a rather inefficient setup. A new DNS lookup is required for every
|
|
request. To avoid this overhead, the filesystem can be arranged to
|
|
correspond to the IP addresses, instead of to the host names, thereby
|
|
negating the need for a DNS lookup. Logging will also have to be adjusted
|
|
to fit this system.</p>
|
|
|
|
<div class="example"><p><code>
|
|
# get the server name from the reverse DNS of the IP address<br />
|
|
UseCanonicalName DNS<br />
|
|
<br />
|
|
# include the IP address in the logs so they may be split<br />
|
|
LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon<br />
|
|
CustomLog logs/access_log vcommon<br />
|
|
<br />
|
|
# include the IP address in the filenames<br />
|
|
VirtualDocumentRootIP /www/hosts/%0/docs<br />
|
|
VirtualScriptAliasIP /www/hosts/%0/cgi-bin<br />
|
|
</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="simple.rewrite" id="simple.rewrite">Simple Dynamic
|
|
Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></h2>
|
|
|
|
<p>This extract from <code>httpd.conf</code> does the same
|
|
thing as <a href="#simple">the first example</a>. The first
|
|
half is very similar to the corresponding part above, except for
|
|
some changes, required for backward compatibility and to make the
|
|
<code>mod_rewrite</code> part work properly; the second half
|
|
configures <code>mod_rewrite</code> to do the actual work.</p>
|
|
|
|
<p>There are a couple of especially tricky bits: by default,
|
|
<code>mod_rewrite</code> runs before other URI translation
|
|
modules (<code>mod_alias</code> etc.) - so if you wish to use these modules, <code>mod_rewrite</code> must be configured to accommodate
|
|
them. Also, some magic is required to do a
|
|
per-dynamic-virtual-host equivalent of
|
|
<code>ScriptAlias</code>.</p>
|
|
|
|
<div class="example"><p><code>
|
|
# get the server name from the Host: header<br />
|
|
UseCanonicalName Off<br />
|
|
<br />
|
|
# splittable logs<br />
|
|
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon<br />
|
|
CustomLog logs/access_log vcommon<br />
|
|
<br />
|
|
<Directory /www/hosts><br />
|
|
<span class="indent">
|
|
# ExecCGI is needed here because we can't force<br />
|
|
# CGI execution in the way that ScriptAlias does<br />
|
|
Options FollowSymLinks ExecCGI<br />
|
|
</span>
|
|
</Directory><br />
|
|
<br />
|
|
# now for the hard bit<br />
|
|
<br />
|
|
RewriteEngine On<br />
|
|
<br />
|
|
# a ServerName derived from a Host: header may be any case at all<br />
|
|
RewriteMap lowercase int:tolower<br />
|
|
<br />
|
|
## deal with normal documents first:<br />
|
|
# allow Alias /icons/ to work - repeat for other aliases<br />
|
|
RewriteCond %{REQUEST_URI} !^/icons/<br />
|
|
# allow CGIs to work<br />
|
|
RewriteCond %{REQUEST_URI} !^/cgi-bin/<br />
|
|
# do the magic<br />
|
|
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1<br />
|
|
<br />
|
|
## and now deal with CGIs - we have to force a handler<br />
|
|
RewriteCond %{REQUEST_URI} ^/cgi-bin/<br />
|
|
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]<br />
|
|
<br />
|
|
# that's it!
|
|
</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="homepages.rewrite" id="homepages.rewrite">A
|
|
Homepages System Using <code>mod_rewrite</code></a></h2>
|
|
|
|
<p>This does the same thing as <a href="#homepages">the second
|
|
example</a>.</p>
|
|
|
|
<div class="example"><p><code>
|
|
RewriteEngine on<br />
|
|
<br />
|
|
RewriteMap lowercase int:tolower<br />
|
|
<br />
|
|
# allow CGIs to work<br />
|
|
RewriteCond %{REQUEST_URI} !^/cgi-bin/<br />
|
|
<br />
|
|
# check the hostname is right so that the RewriteRule works<br />
|
|
RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.isp\.com$<br />
|
|
<br />
|
|
# concatenate the virtual host name onto the start of the URI<br />
|
|
# the [C] means do the next rewrite on the result of this one<br />
|
|
RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]<br />
|
|
<br />
|
|
# now create the real file name<br />
|
|
RewriteRule ^www\.([a-z-]+)\.isp\.com/(.*) /home/$1/$2<br />
|
|
<br />
|
|
# define the global CGI directory<br />
|
|
ScriptAlias /cgi-bin/ /www/std-cgi/
|
|
</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="xtra-conf" id="xtra-conf">Using a Separate Virtual
|
|
Host Configuration File</a></h2>
|
|
|
|
<p>This arrangement uses more advanced <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
|
|
features to work out the translation from virtual host to document
|
|
root, from a separate configuration file. This provides more
|
|
flexibility, but requires more complicated configuration.</p>
|
|
|
|
<p>The <code>vhost.map</code> file should look something like
|
|
this:</p>
|
|
|
|
<div class="example"><p><code>
|
|
www.customer-1.com /www/customers/1<br />
|
|
www.customer-2.com /www/customers/2<br />
|
|
# ...<br />
|
|
www.customer-N.com /www/customers/N<br />
|
|
</code></p></div>
|
|
|
|
<p>The <code>httpd.conf</code> should contain the following:</p>
|
|
|
|
<div class="example"><p><code>
|
|
RewriteEngine on<br />
|
|
<br />
|
|
RewriteMap lowercase int:tolower<br />
|
|
<br />
|
|
# define the map file<br />
|
|
RewriteMap vhost txt:/www/conf/vhost.map<br />
|
|
<br />
|
|
# deal with aliases as above<br />
|
|
RewriteCond %{REQUEST_URI} !^/icons/<br />
|
|
RewriteCond %{REQUEST_URI} !^/cgi-bin/<br />
|
|
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$<br />
|
|
# this does the file-based remap<br />
|
|
RewriteCond ${vhost:%1} ^(/.*)$<br />
|
|
RewriteRule ^/(.*)$ %1/docs/$1<br />
|
|
<br />
|
|
RewriteCond %{REQUEST_URI} ^/cgi-bin/<br />
|
|
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$<br />
|
|
RewriteCond ${vhost:%1} ^(/.*)$<br />
|
|
RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script]
|
|
</code></p></div>
|
|
|
|
</div></div>
|
|
<div class="bottomlang">
|
|
<p><span>Available Languages: </span><a href="../en/vhosts/mass.html" title="English"> en </a> |
|
|
<a href="../ko/vhosts/mass.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
|
|
</div><div id="footer">
|
|
<p class="apache">Copyright 2006 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> |