]> granicus.if.org Git - apache/blob - docs/manual/vhosts/mass.xml
a6bd6df1defaf31c6fa629fc8e7378e700c896ec
[apache] / docs / manual / vhosts / mass.xml
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$ -->
5
6 <!--
7  Copyright 2002-2005 The Apache Software Foundation or its licensors, as
8  applicable.
9
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
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
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.
21 -->
22
23 <manualpage metafile="mass.xml.meta">
24 <parentdocument href="./">Virtual Hosts</parentdocument>
25    <title>Dynamically configured mass virtual hosting</title>
26
27 <summary>
28
29     <p>This document describes how to efficiently serve an
30     arbitrary number of virtual hosts with Apache. <!--
31
32                 Written by Tony Finch (fanf@demon.net) (dot@dotat.at).
33
34                 Some examples were derived from Ralf S. Engleschall's document
35                     http://www.engelschall.com/pw/apache/rewriteguide/
36
37                 Some suggestions were made by Brian Behlendorf.
38
39                 -->
40     </p>
41
42 </summary>
43
44 <section id="motivation"><title>Motivation</title>
45
46     <p>The techniques described here are of interest if your
47     <code>httpd.conf</code> contains many
48     <code>&lt;VirtualHost&gt;</code> sections that are
49     substantially the same, for example:</p>
50
51 <example>
52 NameVirtualHost 111.22.33.44<br />
53 &lt;VirtualHost 111.22.33.44&gt;<br />
54 <indent>
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 />
58 </indent>
59 &lt;/VirtualHost&gt;<br />
60 &lt;VirtualHost 111.22.33.44&gt;<br />
61 <indent>
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 />
65 </indent>
66 &lt;/VirtualHost&gt;<br />
67 # blah blah blah<br />
68 &lt;VirtualHost 111.22.33.44&gt;<br />
69 <indent>
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 />
73 </indent>
74 &lt;/VirtualHost&gt;
75 </example>
76
77     <p>The basic idea is to replace all of the static
78     <code>&lt;VirtualHost&gt;</code> configuration with a mechanism
79     that works it out dynamically. This has a number of
80     advantages:</p>
81
82     <ol>
83       <li>Your configuration file is smaller so Apache starts
84       faster and uses less memory.</li>
85
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>
89     </ol>
90
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>
97
98 </section>
99
100 <section id="overview"><title>Overview</title>
101
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>
113
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>
131
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>
144
145 </section>
146
147 <section id="simple"><title>Simple dynamic virtual hosts</title>
148
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>
153
154 <example>
155 # get the server name from the Host: header<br />
156 UseCanonicalName Off<br />
157 <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 />
161 <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
165 </example>
166
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>
172
173 </section>
174
175 <section id="homepages"><title>A virtually hosted homepages system</title>
176
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>
184
185 <example>
186 # all the preliminary stuff is the same as above, then<br />
187 <br />
188 # include part of the server name in the filenames<br />
189 VirtualDocumentRoot /www/hosts/%2/docs<br />
190 <br />
191 # single cgi-bin directory<br />
192 ScriptAlias  /cgi-bin/  /www/std-cgi/<br />
193 </example>
194
195     <p>There are examples of more complicated
196     <code>VirtualDocumentRoot</code> settings in the
197     <module>mod_vhost_alias</module> documentation.</p>
198
199 </section>
200
201 <section id="combinations"><title>Using more than
202     one virtual hosting system on the same server</title>
203
204     <p>With more complicated setups you can use Apache's normal
205     <code>&lt;VirtualHost&gt;</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>&lt;VirtualHost&gt;</code> configuration sections.</p>
211
212 <example>
213 UseCanonicalName Off<br />
214 <br />
215 LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
216 <br />
217 &lt;Directory /www/commercial&gt;<br />
218 <indent>
219     Options FollowSymLinks<br />
220     AllowOverride All<br />
221 </indent>
222 &lt;/Directory&gt;<br />
223 <br />
224 &lt;Directory /www/homepages&gt;<br />
225 <indent>
226     Options FollowSymLinks<br />
227     AllowOverride None<br />
228 </indent>
229 &lt;/Directory&gt;<br />
230 <br />
231 &lt;VirtualHost 111.22.33.44&gt;<br />
232 <indent>
233     ServerName www.commercial.isp.com<br />
234     <br />
235     CustomLog logs/access_log.commercial vcommon<br />
236     <br />
237     VirtualDocumentRoot /www/commercial/%0/docs<br />
238     VirtualScriptAlias  /www/commercial/%0/cgi-bin<br />
239 </indent>
240 &lt;/VirtualHost&gt;<br />
241 <br />
242 &lt;VirtualHost 111.22.33.45&gt;<br />
243 <indent>
244     ServerName www.homepages.isp.com<br />
245     <br />
246     CustomLog logs/access_log.homepages vcommon<br />
247     <br />
248     VirtualDocumentRoot /www/homepages/%0/docs<br />
249     ScriptAlias         /cgi-bin/ /www/std-cgi/<br />
250 </indent>
251 &lt;/VirtualHost&gt;
252 </example>
253
254 </section>
255
256 <section id="ipbased"><title>More efficient IP-based virtual hosting</title>
257
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>
266
267 <example>
268 # get the server name from the reverse DNS of the IP address<br />
269 UseCanonicalName DNS<br />
270 <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 />
274 <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 />
278 </example>
279
280 </section>
281
282 <section id="oldversion"><title>Using older versions of Apache</title>
283
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
289     hosts.</p>
290
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
302     <code>%V</code>.</p>
303
304 </section>
305
306 <section id="simple.rewrite"><title>Simple dynamic
307     virtual hosts using <code>mod_rewrite</code></title>
308
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>
315
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>
323
324 <example>
325 # get the server name from the Host: header<br />
326 UseCanonicalName Off<br />
327 <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 />
331 <br />
332 &lt;Directory /www/hosts&gt;<br />
333 <indent>
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 />
337 </indent>
338 &lt;/Directory&gt;<br />
339 <br />
340 # now for the hard bit<br />
341 <br />
342 RewriteEngine On<br />
343 <br />
344 # a ServerName derived from a Host: header may be any case at all<br />
345 RewriteMap  lowercase  int:tolower<br />
346 <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 />
352 # do the magic<br />
353 RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1<br />
354 <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 />
358 <br />
359 # that's it!
360 </example>
361
362 </section>
363
364 <section id="homepages.rewrite"><title>A
365     homepages system using <code>mod_rewrite</code></title>
366
367     <p>This does the same thing as <a href="#homepages">the second
368     example</a>.</p>
369
370 <example>
371 RewriteEngine on<br />
372 <br />
373 RewriteMap   lowercase  int:tolower<br />
374 <br />
375 # allow CGIs to work<br />
376 RewriteCond  %{REQUEST_URI}  !^/cgi-bin/<br />
377 <br />
378 # check the hostname is right so that the RewriteRule works<br />
379 RewriteCond  ${lowercase:%{SERVER_NAME}}  ^www\.[a-z-]+\.isp\.com$<br />
380 <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 />
384 <br />
385 # now create the real file name<br />
386 RewriteRule  ^www\.([a-z-]+)\.isp\.com/(.*) /home/$1/$2<br />
387 <br />
388 # define the global CGI directory<br />
389 ScriptAlias  /cgi-bin/  /www/std-cgi/
390 </example>
391
392 </section>
393
394 <section id="xtra-conf"><title>Using a separate virtual
395     host configuration file</title>
396
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>
401
402     <p>The <code>vhost.map</code> file contains something like
403     this:</p>
404
405 <example>
406 www.customer-1.com  /www/customers/1<br />
407 www.customer-2.com  /www/customers/2<br />
408 # ...<br />
409 www.customer-N.com  /www/customers/N<br />
410 </example>
411
412     <p>The <code>http.conf</code> contains this:</p>
413
414 <example>
415 RewriteEngine on<br />
416 <br />
417 RewriteMap   lowercase  int:tolower<br />
418 <br />
419 # define the map file<br />
420 RewriteMap   vhost      txt:/www/conf/vhost.map<br />
421 <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 />
429 <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]
434 </example>
435
436 </section>
437 </manualpage>