mirror of
https://github.com/apache/httpd.git
synced 2025-07-30 20:03:10 +03:00
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1818059 13f79535-47bb-0310-9956-ffa450edef68
315 lines
12 KiB
XML
315 lines
12 KiB
XML
<?xml version='1.0' encoding='UTF-8' ?>
|
|
<!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
|
|
<?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
|
|
<!-- $LastChangedRevision$ -->
|
|
|
|
<!--
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
this work for additional information regarding copyright ownership.
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
(the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
-->
|
|
|
|
<manualpage metafile="mass.xml.meta">
|
|
<parentdocument href="./">Virtual Hosts</parentdocument>
|
|
<title>Dynamically Configured Mass Virtual Hosting</title>
|
|
|
|
<summary>
|
|
|
|
<p>This document describes how to efficiently serve an
|
|
arbitrary number of virtual hosts with the Apache HTTP Server. A
|
|
<a href="../rewrite/vhosts.html">separate document</a> discusses using
|
|
<module>mod_rewrite</module> to create dynamic mass virtual hosts.
|
|
</p>
|
|
|
|
</summary>
|
|
|
|
<section id="motivation"><title>Motivation</title>
|
|
|
|
<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>
|
|
|
|
<highlight language="config">
|
|
<VirtualHost 111.22.33.44>
|
|
ServerName customer-1.example.com
|
|
DocumentRoot "/www/hosts/customer-1.example.com/docs"
|
|
ScriptAlias "/cgi-bin/" "/www/hosts/customer-1.example.com/cgi-bin"
|
|
</VirtualHost>
|
|
|
|
<VirtualHost 111.22.33.44>
|
|
ServerName customer-2.example.com
|
|
DocumentRoot "/www/hosts/customer-2.example.com/docs"
|
|
ScriptAlias "/cgi-bin/" "/www/hosts/customer-2.example.com/cgi-bin"
|
|
</VirtualHost>
|
|
|
|
<VirtualHost 111.22.33.44>
|
|
ServerName customer-N.example.com
|
|
DocumentRoot "/www/hosts/customer-N.example.com/docs"
|
|
ScriptAlias "/cgi-bin/" "/www/hosts/customer-N.example.com/cgi-bin"
|
|
</VirtualHost>
|
|
</highlight>
|
|
|
|
<p>We wish to replace these multiple
|
|
<code><VirtualHost></code> blocks 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. Perhaps more importantly, the
|
|
smaller configuration is easier to maintain, and leaves less room
|
|
for errors.</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 <a
|
|
href="fd-limits.html">number of file descriptors needed</a>.
|
|
It is better to <a href="../logs.html#piped">log to a pipe or a fifo</a>,
|
|
and arrange for the process at the other end to split up the log
|
|
files into one per virtual host. One example of such a process can
|
|
be found in the <a
|
|
href="../programs/split-logfile.html">split-logfile</a>
|
|
utility.</p>
|
|
|
|
</section>
|
|
|
|
<section id="overview"><title>Overview</title>
|
|
|
|
<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 <module>mod_vhost_alias</module>
|
|
with Apache httpd. Alternatively,
|
|
<a href="../rewrite/vhosts.html">mod_rewrite can
|
|
be used</a>.</p>
|
|
<p>Both of these modules are disabled by default; you must enable
|
|
one of them when configuring and building Apache httpd if you want to
|
|
use this technique.</p>
|
|
|
|
<p>A couple of things need to be determined from the request in
|
|
order to make the dynamic
|
|
virtual host look like a normal one. The most important is the
|
|
server name, which is used by the server 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 <directive
|
|
module="core">UseCanonicalName</directive>
|
|
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 httpd 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 determine is the document root (configured
|
|
with <code>DocumentRoot</code> and available to CGI scripts 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 <module>mod_vhost_alias</module> or
|
|
<module>mod_rewrite</module>), 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>
|
|
|
|
</section>
|
|
|
|
<section id="simple"><title>Dynamic Virtual Hosts with
|
|
mod_vhost_alias</title>
|
|
|
|
<p>This extract from <code>httpd.conf</code> implements the
|
|
virtual host arrangement outlined in the <a
|
|
href="#motivation">Motivation</a> section above
|
|
using <module>mod_vhost_alias</module>.</p>
|
|
|
|
<highlight language="config">
|
|
# get the server name from the Host: header
|
|
UseCanonicalName Off
|
|
|
|
# this log format can be split per-virtual-host based on the first field
|
|
# using the split-logfile utility.
|
|
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
|
|
CustomLog "logs/access_log" vcommon
|
|
|
|
# include the server name in the filenames used to satisfy requests
|
|
VirtualDocumentRoot "/www/hosts/%0/docs"
|
|
VirtualScriptAlias "/www/hosts/%0/cgi-bin"
|
|
</highlight>
|
|
|
|
<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. The variable <code>%0</code>
|
|
references the requested servername, as indicated in the
|
|
<code>Host:</code> header.</p>
|
|
|
|
<p>See the <module>mod_vhost_alias</module> documentation for more usage
|
|
examples.</p>
|
|
|
|
</section>
|
|
|
|
<section id="homepages"><title>Simplified Dynamic Virtual Hosts</title>
|
|
|
|
<p>This is an adjustment of the above system, tailored for an
|
|
ISP's web hosting server. Using <code>%2</code>,
|
|
we can select substrings of the server name to
|
|
use in the filename so that, for example, the documents for
|
|
<code>www.user.example.com</code> are found in
|
|
<code>/home/user/www</code>. It uses a single <code>cgi-bin</code>
|
|
directory instead of one per virtual host.</p>
|
|
|
|
<highlight language="config">
|
|
UseCanonicalName Off
|
|
|
|
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
|
|
CustomLog "logs/access_log" vcommon
|
|
|
|
# include part of the server name in the filenames
|
|
VirtualDocumentRoot "/home/%2/www"
|
|
|
|
# single cgi-bin directory
|
|
ScriptAlias "/cgi-bin/" "/www/std-cgi/"
|
|
</highlight>
|
|
|
|
<p>There are examples of more complicated
|
|
<code>VirtualDocumentRoot</code> settings in the
|
|
<module>mod_vhost_alias</module> documentation.</p>
|
|
|
|
</section>
|
|
|
|
<section id="combinations"><title>Using Multiple Virtual
|
|
Hosting Systems on the Same Server</title>
|
|
|
|
<p>With more complicated setups, you can use httpd'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 be combined with conventional
|
|
<code><VirtualHost></code> configuration sections, as shown
|
|
below.</p>
|
|
|
|
<highlight language="config">
|
|
UseCanonicalName Off
|
|
|
|
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
|
|
|
|
<Directory "/www/commercial">
|
|
Options FollowSymLinks
|
|
AllowOverride All
|
|
</Directory>
|
|
|
|
<Directory "/www/homepages">
|
|
Options FollowSymLinks
|
|
AllowOverride None
|
|
</Directory>
|
|
|
|
<VirtualHost 111.22.33.44>
|
|
ServerName www.commercial.example.com
|
|
|
|
CustomLog "logs/access_log.commercial" vcommon
|
|
|
|
VirtualDocumentRoot "/www/commercial/%0/docs"
|
|
VirtualScriptAlias "/www/commercial/%0/cgi-bin"
|
|
</VirtualHost>
|
|
|
|
<VirtualHost 111.22.33.45>
|
|
ServerName www.homepages.example.com
|
|
|
|
CustomLog "logs/access_log.homepages" vcommon
|
|
|
|
VirtualDocumentRoot "/www/homepages/%0/docs"
|
|
ScriptAlias "/cgi-bin/" "/www/std-cgi/"
|
|
</VirtualHost>
|
|
</highlight>
|
|
|
|
<note>
|
|
<title>Note</title>
|
|
<p>If the first VirtualHost block does <em>not</em> include a
|
|
<directive module="core">ServerName</directive> 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 (eg. <code>ServerName
|
|
none.example.com</code>) can be added to get around this
|
|
behaviour.</p>
|
|
</note>
|
|
|
|
</section>
|
|
|
|
<section id="ipbased"><title>More Efficient IP-Based Virtual Hosting</title>
|
|
|
|
<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>
|
|
|
|
<highlight language="config">
|
|
# get the server name from the reverse DNS of the IP address
|
|
UseCanonicalName DNS
|
|
|
|
# include the IP address in the logs so they may be split
|
|
LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon
|
|
CustomLog "logs/access_log" vcommon
|
|
|
|
# include the IP address in the filenames
|
|
VirtualDocumentRootIP "/www/hosts/%0/docs"
|
|
VirtualScriptAliasIP "/www/hosts/%0/cgi-bin"
|
|
</highlight>
|
|
|
|
</section>
|
|
|
|
<section id="rewrite"><title>Mass virtual hosts with
|
|
mod_rewrite</title>
|
|
|
|
<p>
|
|
Mass virtual hosting may also be accomplished using
|
|
<module>mod_rewrite</module>, either using simple <directive
|
|
module="mod_rewrite">RewriteRule</directive> directives, or using more
|
|
complicated techniques such as storing the vhost definitions externally
|
|
and accessing them via <directive
|
|
module="mod_rewrite">RewriteMap</directive>. These techniques are
|
|
discussed in the <a href="../rewrite/vhosts.html">rewrite
|
|
documentation</a>.</p>
|
|
</section>
|
|
|
|
<section id="macro"><title>Mass virtual hosts with mod_macro</title>
|
|
|
|
<p>Another option for dynamically generated virtual hosts is
|
|
<module>mod_macro</module>, with which you can create a virtualhost
|
|
template, and invoke it for multiple hostnames. An example of this is
|
|
provided in the <strong>Usage</strong> section of the module
|
|
documentation.
|
|
</p>
|
|
</section>
|
|
|
|
</manualpage>
|