1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Copyright 2002-2005 The Apache Software Foundation or its licensors, as
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="mass.xml.meta">
24 <parentdocument href="./">Virtual Hosts</parentdocument>
25 <title>Dynamically configured mass virtual hosting</title>
29 <p>This document describes how to efficiently serve an
30 arbitrary number of virtual hosts with Apache. <!--
32 Written by Tony Finch (fanf@demon.net) (dot@dotat.at).
34 Some examples were derived from Ralf S. Engleschall's document
35 http://www.engelschall.com/pw/apache/rewriteguide/
37 Some suggestions were made by Brian Behlendorf.
44 <section id="motivation"><title>Motivation</title>
46 <p>The techniques described here are of interest if your
47 <code>httpd.conf</code> contains many
48 <code><VirtualHost></code> sections that are
49 substantially the same, for example:</p>
52 NameVirtualHost 111.22.33.44<br />
53 <VirtualHost 111.22.33.44><br />
55 ServerName www.customer-1.com<br />
56 DocumentRoot /www/hosts/www.customer-1.com/docs<br />
57 ScriptAlias /cgi-bin/ /www/hosts/www.customer-1.com/cgi-bin<br />
59 </VirtualHost><br />
60 <VirtualHost 111.22.33.44><br />
62 ServerName www.customer-2.com<br />
63 DocumentRoot /www/hosts/www.customer-2.com/docs<br />
64 ScriptAlias /cgi-bin/ /www/hosts/www.customer-2.com/cgi-bin<br />
66 </VirtualHost><br />
67 # blah blah blah<br />
68 <VirtualHost 111.22.33.44><br />
70 ServerName www.customer-N.com<br />
71 DocumentRoot /www/hosts/www.customer-N.com/docs<br />
72 ScriptAlias /cgi-bin/ /www/hosts/www.customer-N.com/cgi-bin<br />
77 <p>The basic idea is to replace all of the static
78 <code><VirtualHost></code> configuration with a mechanism
79 that works it out dynamically. This has a number of
83 <li>Your configuration file is smaller so Apache starts
84 faster and uses less memory.</li>
86 <li>Adding virtual hosts is simply a matter of creating the
87 appropriate directories in the filesystem and entries in the
88 DNS - you don't need to reconfigure or restart Apache.</li>
91 <p>The main disadvantage is that you cannot have a different
92 log file for each virtual host; however if you have very many
93 virtual hosts then doing this is dubious anyway because it eats
94 file descriptors. It is better to log to a pipe or a fifo and
95 arrange for the process at the other end to distribute the logs
96 to the customers (it can also accumulate statistics, etc.).</p>
100 <section id="overview"><title>Overview</title>
102 <p>A virtual host is defined by two pieces of information: its
103 IP address, and the contents of the <code>Host:</code> header
104 in the HTTP request. The dynamic mass virtual hosting technique
105 is based on automatically inserting this information into the
106 pathname of the file that is used to satisfy the request. This
107 is done most easily using <module>mod_vhost_alias</module>,
108 but if you are using a version of Apache up to 1.3.6 then you
109 must use <module>mod_rewrite</module>.
110 Both of these modules are disabled by default; you must enable
111 one of them when configuring and building Apache if you want to
112 use this technique.</p>
114 <p>A couple of things need to be `faked' to make the dynamic
115 virtual host look like a normal one. The most important is the
116 server name which is used by Apache to generate
117 self-referential URLs, etc. It is configured with the
118 <code>ServerName</code> directive, and it is available to CGIs
119 via the <code>SERVER_NAME</code> environment variable. The
120 actual value used at run time is controlled by the <directive
121 module="core">UseCanonicalName</directive>
122 setting. With <code>UseCanonicalName Off</code> the server name
123 comes from the contents of the <code>Host:</code> header in the
124 request. With <code>UseCanonicalName DNS</code> it comes from a
125 reverse DNS lookup of the virtual host's IP address. The former
126 setting is used for name-based dynamic virtual hosting, and the
127 latter is used for IP-based hosting. If Apache cannot work out
128 the server name because there is no <code>Host:</code> header
129 or the DNS lookup fails then the value configured with
130 <code>ServerName</code> is used instead.</p>
132 <p>The other thing to `fake' is the document root (configured
133 with <code>DocumentRoot</code> and available to CGIs via the
134 <code>DOCUMENT_ROOT</code> environment variable). In a normal
135 configuration this setting is used by the core module when
136 mapping URIs to filenames, but when the server is configured to
137 do dynamic virtual hosting that job is taken over by another
138 module (either <code>mod_vhost_alias</code> or
139 <code>mod_rewrite</code>) which has a different way of doing
140 the mapping. Neither of these modules is responsible for
141 setting the <code>DOCUMENT_ROOT</code> environment variable so
142 if any CGIs or SSI documents make use of it they will get a
143 misleading value.</p>
147 <section id="simple"><title>Simple dynamic virtual hosts</title>
149 <p>This extract from <code>httpd.conf</code> implements the
150 virtual host arrangement outlined in the <a
151 href="#motivation">Motivation</a> section above, but in a
152 generic fashion using <code>mod_vhost_alias</code>.</p>
155 # get the server name from the Host: header<br />
156 UseCanonicalName Off<br />
158 # this log format can be split per-virtual-host based on the first field<br />
159 LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
160 CustomLog logs/access_log vcommon<br />
162 # include the server name in the filenames used to satisfy requests<br />
163 VirtualDocumentRoot /www/hosts/%0/docs<br />
164 VirtualScriptAlias /www/hosts/%0/cgi-bin
167 <p>This configuration can be changed into an IP-based virtual
168 hosting solution by just turning <code>UseCanonicalName
169 Off</code> into <code>UseCanonicalName DNS</code>. The server
170 name that is inserted into the filename is then derived from
171 the IP address of the virtual host.</p>
175 <section id="homepages"><title>A virtually hosted homepages system</title>
177 <p>This is an adjustment of the above system tailored for an
178 ISP's homepages server. Using a slightly more complicated
179 configuration we can select substrings of the server name to
180 use in the filename so that e.g. the documents for
181 <code>www.user.isp.com</code> are found in
182 <code>/home/user/</code>. It uses a single <code>cgi-bin</code>
183 directory instead of one per virtual host.</p>
186 # all the preliminary stuff is the same as above, then<br />
188 # include part of the server name in the filenames<br />
189 VirtualDocumentRoot /www/hosts/%2/docs<br />
191 # single cgi-bin directory<br />
192 ScriptAlias /cgi-bin/ /www/std-cgi/<br />
195 <p>There are examples of more complicated
196 <code>VirtualDocumentRoot</code> settings in the
197 <module>mod_vhost_alias</module> documentation.</p>
201 <section id="combinations"><title>Using more than
202 one virtual hosting system on the same server</title>
204 <p>With more complicated setups you can use Apache's normal
205 <code><VirtualHost></code> directives to control the
206 scope of the various virtual hosting configurations. For
207 example, you could have one IP address for homepages customers
208 and another for commercial customers with the following setup.
209 This can of course be combined with conventional
210 <code><VirtualHost></code> configuration sections.</p>
213 UseCanonicalName Off<br />
215 LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
217 <Directory /www/commercial><br />
219 Options FollowSymLinks<br />
220 AllowOverride All<br />
222 </Directory><br />
224 <Directory /www/homepages><br />
226 Options FollowSymLinks<br />
227 AllowOverride None<br />
229 </Directory><br />
231 <VirtualHost 111.22.33.44><br />
233 ServerName www.commercial.isp.com<br />
235 CustomLog logs/access_log.commercial vcommon<br />
237 VirtualDocumentRoot /www/commercial/%0/docs<br />
238 VirtualScriptAlias /www/commercial/%0/cgi-bin<br />
240 </VirtualHost><br />
242 <VirtualHost 111.22.33.45><br />
244 ServerName www.homepages.isp.com<br />
246 CustomLog logs/access_log.homepages vcommon<br />
248 VirtualDocumentRoot /www/homepages/%0/docs<br />
249 ScriptAlias /cgi-bin/ /www/std-cgi/<br />
256 <section id="ipbased"><title>More efficient IP-based virtual hosting</title>
258 <p>After <a href="#simple">the first example</a> I noted that
259 it is easy to turn it into an IP-based virtual hosting setup.
260 Unfortunately that configuration is not very efficient because
261 it requires a DNS lookup for every request. This can be avoided
262 by laying out the filesystem according to the IP addresses
263 themselves rather than the corresponding names and changing the
264 logging similarly. Apache will then usually not need to work
265 out the server name and so incur a DNS lookup.</p>
268 # get the server name from the reverse DNS of the IP address<br />
269 UseCanonicalName DNS<br />
271 # include the IP address in the logs so they may be split<br />
272 LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon<br />
273 CustomLog logs/access_log vcommon<br />
275 # include the IP address in the filenames<br />
276 VirtualDocumentRootIP /www/hosts/%0/docs<br />
277 VirtualScriptAliasIP /www/hosts/%0/cgi-bin<br />
282 <section id="oldversion"><title>Using older versions of Apache</title>
284 <p>The examples above rely on <code>mod_vhost_alias</code>
285 which appeared after version 1.3.6. If you are using a version
286 of Apache without <code>mod_vhost_alias</code> then you can
287 implement this technique with <code>mod_rewrite</code> as
288 illustrated below, but only for Host:-header-based virtual
291 <p>In addition there are some things to beware of with logging.
292 Apache 1.3.6 is the first version to include the
293 <code>%V</code> log format directive; in versions 1.3.0 - 1.3.3
294 the <code>%v</code> option did what <code>%V</code> does;
295 version 1.3.4 has no equivalent. In all these versions of
296 Apache the <code>UseCanonicalName</code> directive can appear
297 in <code>.htaccess</code> files which means that customers can
298 cause the wrong thing to be logged. Therefore the best thing to
299 do is use the <code>%{Host}i</code> directive which logs the
300 <code>Host:</code> header directly; note that this may include
301 <code>:port</code> on the end which is not the case for
306 <section id="simple.rewrite"><title>Simple dynamic
307 virtual hosts using <code>mod_rewrite</code></title>
309 <p>This extract from <code>httpd.conf</code> does the same
310 thing as <a href="#simple">the first example</a>. The first
311 half is very similar to the corresponding part above but with
312 some changes for backward compatibility and to make the
313 <code>mod_rewrite</code> part work properly; the second half
314 configures <code>mod_rewrite</code> to do the actual work.</p>
316 <p>There are a couple of especially tricky bits: By default,
317 <code>mod_rewrite</code> runs before the other URI translation
318 modules (<code>mod_alias</code> etc.) so if they are used then
319 <code>mod_rewrite</code> must be configured to accommodate
320 them. Also, some magic must be performed to do a
321 per-dynamic-virtual-host equivalent of
322 <code>ScriptAlias</code>.</p>
325 # get the server name from the Host: header<br />
326 UseCanonicalName Off<br />
328 # splittable logs<br />
329 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon<br />
330 CustomLog logs/access_log vcommon<br />
332 <Directory /www/hosts><br />
334 # ExecCGI is needed here because we can't force<br />
335 # CGI execution in the way that ScriptAlias does<br />
336 Options FollowSymLinks ExecCGI<br />
338 </Directory><br />
340 # now for the hard bit<br />
342 RewriteEngine On<br />
344 # a ServerName derived from a Host: header may be any case at all<br />
345 RewriteMap lowercase int:tolower<br />
347 ## deal with normal documents first:<br />
348 # allow Alias /icons/ to work - repeat for other aliases<br />
349 RewriteCond %{REQUEST_URI} !^/icons/<br />
350 # allow CGIs to work<br />
351 RewriteCond %{REQUEST_URI} !^/cgi-bin/<br />
353 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1<br />
355 ## and now deal with CGIs - we have to force a handler<br />
356 RewriteCond %{REQUEST_URI} ^/cgi-bin/<br />
357 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]<br />
364 <section id="homepages.rewrite"><title>A
365 homepages system using <code>mod_rewrite</code></title>
367 <p>This does the same thing as <a href="#homepages">the second
371 RewriteEngine on<br />
373 RewriteMap lowercase int:tolower<br />
375 # allow CGIs to work<br />
376 RewriteCond %{REQUEST_URI} !^/cgi-bin/<br />
378 # check the hostname is right so that the RewriteRule works<br />
379 RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.isp\.com$<br />
381 # concatenate the virtual host name onto the start of the URI<br />
382 # the [C] means do the next rewrite on the result of this one<br />
383 RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]<br />
385 # now create the real file name<br />
386 RewriteRule ^www\.([a-z-]+)\.isp\.com/(.*) /home/$1/$2<br />
388 # define the global CGI directory<br />
389 ScriptAlias /cgi-bin/ /www/std-cgi/
394 <section id="xtra-conf"><title>Using a separate virtual
395 host configuration file</title>
397 <p>This arrangement uses more advanced <code>mod_rewrite</code>
398 features to get the translation from virtual host to document
399 root from a separate configuration file. This provides more
400 flexibility but requires more complicated configuration.</p>
402 <p>The <code>vhost.map</code> file contains something like
406 www.customer-1.com /www/customers/1<br />
407 www.customer-2.com /www/customers/2<br />
409 www.customer-N.com /www/customers/N<br />
412 <p>The <code>http.conf</code> contains this:</p>
415 RewriteEngine on<br />
417 RewriteMap lowercase int:tolower<br />
419 # define the map file<br />
420 RewriteMap vhost txt:/www/conf/vhost.map<br />
422 # deal with aliases as above<br />
423 RewriteCond %{REQUEST_URI} !^/icons/<br />
424 RewriteCond %{REQUEST_URI} !^/cgi-bin/<br />
425 RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$<br />
426 # this does the file-based remap<br />
427 RewriteCond ${vhost:%1} ^(/.*)$<br />
428 RewriteRule ^/(.*)$ %1/docs/$1<br />
430 RewriteCond %{REQUEST_URI} ^/cgi-bin/<br />
431 RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$<br />
432 RewriteCond ${vhost:%1} ^(/.*)$<br />
433 RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script]