Moved apache code into a folder to help prepare for packaging where we dont want...
[httpd-crcsyncproxy.git] / apache / docs / manual / sections.xml
blob98d806cbb63ec250ad03a0d180fd34f2cda6ec7a
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$ -->
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  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.
21 -->
23 <manualpage metafile="sections.xml.meta">
25 <title>Configuration Sections</title>
27 <summary> <p>Directives in the <a
28 href="configuring.html">configuration files</a> may apply to the
29 entire server, or they may be restricted to apply only to particular
30 directories, files, hosts, or URLs.  This document describes how to
31 use configuration section containers or <code>.htaccess</code> files
32 to change the scope of other configuration directives.</p>
33 </summary>
35 <section id="types"><title>Types of Configuration Section Containers</title>
37 <related>
38 <modulelist>
39 <module>core</module>
40 <module>mod_version</module>
41 <module>mod_proxy</module>
42 </modulelist>
43 <directivelist>
44 <directive type="section" module="core">Directory</directive>
45 <directive type="section" module="core">DirectoryMatch</directive>
46 <directive type="section" module="core">Files</directive>
47 <directive type="section" module="core">FilesMatch</directive>
48 <directive type="section" module="core">If</directive>
49 <directive type="section" module="core">IfDefine</directive>
50 <directive type="section" module="core">IfModule</directive>
51 <directive type="section" module="mod_version">IfVersion</directive>
52 <directive type="section" module="core">Location</directive>
53 <directive type="section" module="core">LocationMatch</directive>
54 <directive type="section" module="mod_proxy">Proxy</directive>
55 <directive type="section" module="mod_proxy">ProxyMatch</directive>
56 <directive type="section" module="core">VirtualHost</directive>
57 </directivelist>
58 </related>
60 <p>There are two basic types of containers.  Most containers are
61 evaluated for each request.  The enclosed directives are applied only
62 for those requests that match the containers.  The <directive
63 type="section" module="core">IfDefine</directive>, <directive
64 type="section" module="core">IfModule</directive>, and
65 <directive type="section" module="mod_version">IfVersion</directive>
66 containers, on the other hand, are evaluated only at server startup
67 and restart.  If their conditions are true at startup, then the
68 enclosed directives will apply to all requests.  If the conditions are
69 not true, the enclosed directives will be ignored.</p>
71 <p>The <directive type="section" module="core">IfDefine</directive> directive
72 encloses directives that will only be applied if an appropriate
73 parameter is defined on the <program>httpd</program> command line.  For example,
74 with the following configuration, all requests will be redirected
75 to another site only if the server is started using
76 <code>httpd -DClosedForNow</code>:</p>
78 <example>
79 &lt;IfDefine ClosedForNow&gt;<br />
80 Redirect / http://otherserver.example.com/<br />
81 &lt;/IfDefine&gt;
82 </example>
84 <p>The <directive type="section" module="core">IfModule</directive>
85 directive is very similar, except it encloses directives that will
86 only be applied if a particular module is available in the server.
87 The module must either be statically compiled in the server, or it
88 must be dynamically compiled and its <directive
89 module="mod_so">LoadModule</directive> line must be earlier in the
90 configuration file.  This directive should only be used if you need
91 your configuration file to work whether or not certain modules are
92 installed.  It should not be used to enclose directives that you want
93 to work all the time, because it can suppress useful error messages
94 about missing modules.</p>
96 <p>In the following example, the <directive
97 module="mod_mime_magic">MimeMagicFiles</directive> directive will be
98 applied only if <module>mod_mime_magic</module> is available.</p>
100 <example>
101 &lt;IfModule mod_mime_magic.c&gt;<br />
102 MimeMagicFile conf/magic<br />
103 &lt;/IfModule&gt;
104 </example>
106 <p>The <directive type="section" module="mod_version">IfVersion</directive>
107 directive is very similar to <directive type="section"
108 module="core">IfDefine</directive> and <directive type="section"
109 module="core">IfModule</directive>, except it encloses directives that will
110 only be applied if a particular version of the server is executing.  This
111 module is designed for the use in test suites and large networks which have to
112 deal with different httpd versions and different configurations.</p>
114 <example>
115   &lt;IfVersion >= 2.1&gt;<br />
116   <indent>
117     # this happens only in versions greater or<br />
118     # equal 2.1.0.<br />
119   </indent>
120   &lt;/IfVersion&gt;
121 </example>
123 <p><directive type="section" module="core">IfDefine</directive>,
124 <directive type="section" module="core">IfModule</directive>, and the
125 <directive type="section" module="mod_version">IfVersion</directive>
126 can apply negative conditions by preceding their test with "!".
127 Also, these sections can be nested to achieve more complex
128 restrictions.</p>
129 </section>
131 <section id="file-and-web"><title>Filesystem and Webspace</title>
133 <p>The most commonly used configuration section containers are the
134 ones that change the configuration of particular places in the
135 filesystem or webspace.  First, it is important to understand the
136 difference between the two.  The filesystem is the view of your disks
137 as seen by your operating system.  For example, in a default install,
138 Apache resides at <code>/usr/local/apache2</code> in the Unix
139 filesystem or <code>"c:/Program Files/Apache Group/Apache2"</code> in
140 the Windows filesystem.  (Note that forward slashes should always be
141 used as the path separator in Apache, even for Windows.)  In contrast,
142 the webspace is the view of your site as delivered by the web server
143 and seen by the client.  So the path <code>/dir/</code> in the
144 webspace corresponds to the path
145 <code>/usr/local/apache2/htdocs/dir/</code> in the filesystem of a
146 default Apache install on Unix.  The webspace need not map directly to
147 the filesystem, since webpages may be generated dynamically
148 from databases or other locations.</p>
150 <section id="filesystem"><title>Filesystem Containers</title>
152 <p>The <directive type="section" module="core">Directory</directive>
153 and <directive type="section" module="core">Files</directive>
154 directives, along with their <glossary ref="regex">regex</glossary>
155 counterparts, apply directives to
156 parts of the filesystem.  Directives enclosed in a <directive
157 type="section" module="core">Directory</directive> section apply to
158 the named filesystem directory and all subdirectories of that
159 directory.  The same effect can be obtained using <a
160 href="howto/htaccess.html">.htaccess files</a>.  For example, in the
161 following configuration, directory indexes will be enabled for the
162 <code>/var/web/dir1</code> directory and all subdirectories.</p>
164 <example>
165 &lt;Directory /var/web/dir1&gt;<br />
166 Options +Indexes<br />
167 &lt;/Directory&gt;
168 </example>
170 <p>Directives enclosed in a <directive type="section"
171 module="core">Files</directive> section apply to any file with
172 the specified name, regardless of what directory it lies in.
173 So for example, the following configuration directives will,
174 when placed in the main section of the configuration file,
175 deny access to any file named <code>private.html</code> regardless
176 of where it is found.</p>
178 <example>
179 &lt;Files private.html&gt;<br />
180 Order allow,deny<br />
181 Deny from all<br />
182 &lt;/Files&gt;
183 </example>
185 <p>To address files found in a particular part of the filesystem, the
186 <directive type="section" module="core">Files</directive> and
187 <directive type="section" module="core">Directory</directive> sections
188 can be combined.  For example, the following configuration will deny
189 access to <code>/var/web/dir1/private.html</code>,
190 <code>/var/web/dir1/subdir2/private.html</code>,
191 <code>/var/web/dir1/subdir3/private.html</code>, and any other instance
192 of <code>private.html</code> found under the <code>/var/web/dir1/</code>
193 directory.</p>
195 <example>
196 &lt;Directory /var/web/dir1&gt;<br />
197 &lt;Files private.html&gt;<br />
198 Order allow,deny<br />
199 Deny from all<br />
200 &lt;/Files&gt;<br />
201 &lt;/Directory&gt;
202 </example>
203 </section>
205 <section id="webspace"><title>Webspace Containers</title>
207 <p>The <directive type="section" module="core">Location</directive>
208 directive and its <glossary ref="regex">regex</glossary> counterpart, on
209 the other hand, change the
210 configuration for content in the webspace.  For example, the following
211 configuration prevents access to any URL-path that begins in /private.
212 In particular, it will apply to requests for
213 <code>http://yoursite.example.com/private</code>,
214 <code>http://yoursite.example.com/private123</code>, and
215 <code>http://yoursite.example.com/private/dir/file.html</code> as well
216 as any other requests starting with the <code>/private</code> string.</p>
218 <example>
219 &lt;Location /private&gt;<br />
220 Order Allow,Deny<br />
221 Deny from all<br />
222 &lt;/Location&gt;
223 </example>
225 <p>The <directive type="section" module="core">Location</directive>
226 directive need not have anything to do with the filesystem.
227 For example, the following example shows how to map a particular
228 URL to an internal Apache handler provided by <module>mod_status</module>.
229 No file called <code>server-status</code> needs to exist in the
230 filesystem.</p>
232 <example>
233 &lt;Location /server-status&gt;<br />
234 SetHandler server-status<br />
235 &lt;/Location&gt;
236 </example>
237 </section>
239 <section id="wildcards"><title>Wildcards and Regular Expressions</title>
241 <p>The <directive type="section" module="core">Directory</directive>,
242 <directive type="section" module="core">Files</directive>, and
243 <directive type="section" module="core">Location</directive>
244 directives can each use shell-style wildcard characters as in
245 <code>fnmatch</code> from the C standard library.  The character "*"
246 matches any sequence of characters, "?" matches any single character,
247 and "[<em>seq</em>]" matches any character in <em>seq</em>.  The "/"
248 character will not be matched by any wildcard; it must be specified
249 explicitly.</p>
251 <p>If even more flexible matching is required, each
252 container has a regular expression (regex) counterpart <directive
253 type="section" module="core">DirectoryMatch</directive>, <directive
254 type="section" module="core">FilesMatch</directive>, and <directive
255 type="section" module="core">LocationMatch</directive> that allow
256 perl-compatible
257 <glossary ref="regex">regular expressions</glossary>
258 to be used in choosing the matches.  But see the section below on
259 configuration merging to find out how using regex sections will change
260 how directives are applied.</p>
262 <p>A non-regex wildcard section that changes the configuration of
263 all user directories could look as follows:</p>
265 <example>
266 &lt;Directory /home/*/public_html&gt;<br />
267 Options Indexes<br />
268 &lt;/Directory&gt;
269 </example>
271 <p>Using regex sections, we can deny access to many types of image files
272 at once:</p>
273 <example>
274 &lt;FilesMatch \.(?i:gif|jpe?g|png)$&gt;<br />
275 Order allow,deny<br />
276 Deny from all<br />
277 &lt;/FilesMatch&gt;
278 </example>
280 </section>
282 <section id="whichwhen"><title>What to use When</title>
284 <p>Choosing between filesystem containers and webspace containers is
285 actually quite easy.  When applying directives to objects that reside
286 in the filesystem always use <directive type="section"
287 module="core">Directory</directive> or <directive type="section"
288 module="core">Files</directive>.  When applying directives to objects
289 that do not reside in the filesystem (such as a webpage generated from
290 a database), use <directive type="section"
291 module="core">Location</directive>.</p>
293 <p>It is important to never use <directive type="section"
294 module="core">Location</directive> when trying to restrict
295 access to objects in the filesystem.  This is because many
296 different webspace locations (URLs) could map to the same filesystem
297 location, allowing your restrictions to be circumvented.
298 For example, consider the following configuration:</p>
300 <example>
301 &lt;Location /dir/&gt;<br />
302 Order allow,deny<br />
303 Deny from all<br />
304 &lt;/Location&gt;
305 </example>
307 <p>This works fine if the request is for
308 <code>http://yoursite.example.com/dir/</code>.  But what if you are on
309 a case-insensitive filesystem?  Then your restriction could be easily
310 circumvented by requesting
311 <code>http://yoursite.example.com/DIR/</code>.  The <directive
312 type="section" module="core">Directory</directive> directive, in
313 contrast, will apply to any content served from that location,
314 regardless of how it is called.  (An exception is filesystem links.
315 The same directory can be placed in more than one part of the
316 filesystem using symbolic links.  The <directive type="section"
317 module="core">Directory</directive> directive will follow the symbolic
318 link without resetting the pathname.  Therefore, for the highest level
319 of security, symbolic links should be disabled with the appropriate
320 <directive module="core">Options</directive> directive.)</p>
322 <p>If you are, perhaps, thinking that none of this applies to you
323 because you use a case-sensitive filesystem, remember that there are
324 many other ways to map multiple webspace locations to the same
325 filesystem location.  Therefore you should always use the filesystem
326 containers when you can.  There is, however, one exception to this
327 rule.  Putting configuration restrictions in a <code>&lt;Location
328 /&gt;</code> section is perfectly safe because this section will apply
329 to all requests regardless of the specific URL.</p>
330 </section>
332 </section>
334 <section id="virtualhost"><title>Virtual Hosts</title>
336 <p>The <directive type="section" module="core">VirtualHost</directive>
337 container encloses directives that apply to specific hosts.
338 This is useful when serving multiple hosts from the same machine
339 with a different configuration for each.  For more information,
340 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
341 </section>
343 <section id="proxy"><title>Proxy</title>
345 <p>The <directive type="section" module="mod_proxy">Proxy</directive>
346 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
347 containers apply enclosed configuration directives only
348 to sites accessed through <module>mod_proxy</module>'s proxy server
349 that match the specified URL.  For example, the following configuration
350 will prevent the proxy server from being used to access the
351 <code>cnn.com</code> website.</p>
353 <example>
354 &lt;Proxy http://cnn.com/*&gt;<br />
355 Order allow,deny<br />
356 Deny from all<br />
357 &lt;/Proxy&gt;
358 </example>
359 </section>
361 <section id="whatwhere"><title>What Directives are Allowed?</title>
363 <p>To find out what directives are allowed in what types of
364 configuration sections, check the <a
365 href="mod/directive-dict.html#Context">Context</a> of the directive.
366 Everything that is allowed in 
367 <directive type="section" module="core">Directory</directive>
368 sections is also syntactically allowed in
369 <directive type="section" module="core">DirectoryMatch</directive>,
370 <directive type="section" module="core">Files</directive>,
371 <directive type="section" module="core">FilesMatch</directive>,
372 <directive type="section" module="core">Location</directive>,
373 <directive type="section" module="core">LocationMatch</directive>,
374 <directive type="section" module="mod_proxy">Proxy</directive>,
375 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
376 sections.  There are some exceptions, however:</p>
378 <ul>
379 <li>The <directive module="core">AllowOverride</directive> directive
380 works only in <directive type="section" module="core">Directory</directive>
381 sections.</li>
383 <li>The <code>FollowSymLinks</code> and
384 <code>SymLinksIfOwnerMatch</code> <directive
385 module="core">Options</directive> work only in <directive
386 type="section" module="core">Directory</directive> sections or
387 <code>.htaccess</code> files.</li>
389 <li>The <directive module="core">Options</directive> directive cannot
390 be used in <directive type="section" module="core">Files</directive>
391 and <directive type="section" module="core">FilesMatch</directive>
392 sections.</li>
393 </ul>
394 </section>
396 <section id="mergin"><title>How the sections are merged</title>
398 <p>The configuration sections are applied in a very particular order.
399 Since this can have important effects on how configuration directives
400 are interpreted, it is important to understand how this works.</p>
402     <p>The order of merging is:</p>
404     <ol>
405       <li> <directive type="section"
406       module="core">Directory</directive> (except regular expressions)
407       and <code>.htaccess</code> done simultaneously (with
408       <code>.htaccess</code>, if allowed, overriding
409       <directive type="section" module="core">Directory</directive>)</li>
411       <li><directive type="section" module="core">DirectoryMatch</directive>
412       (and <code>&lt;Directory ~&gt;</code>)</li>
414       <li><directive type="section"
415       module="core">Files</directive> and <directive
416       type="section" module="core">FilesMatch</directive> done
417       simultaneously</li>
419       <li><directive type="section" module="core">Location</directive>
420       and <directive type="section"
421       module="core">LocationMatch</directive> done simultaneously</li>
422     </ol>
424     <p>Apart from <directive type="section"
425     module="core">Directory</directive>, each group is processed in
426     the order that they appear in the configuration files.  <directive
427     type="section" module="core">Directory</directive> (group 1 above)
428     is processed in the order shortest directory component to longest.
429     So for example, <code>&lt;Directory /var/web/dir&gt;</code> will
430     be processed before <code>&lt;Directory
431     /var/web/dir/subdir&gt;</code>.  If multiple <directive
432     type="section" module="core">Directory</directive> sections apply
433     to the same directory they are processed in the configuration file
434     order. Configurations included via the <directive
435     module="core">Include</directive> directive will be treated as if
436     they were inside the including file at the location of the
437     <directive module="core">Include</directive> directive.</p>
439     <p>Sections inside <directive type="section"
440     module="core">VirtualHost</directive> sections
441     are applied <em>after</em> the corresponding sections outside
442     the virtual host definition. This allows virtual hosts to
443     override the main server configuration.</p>
445     <p>When the request is served by <module>mod_proxy</module>, the
446     <directive module="mod_proxy" type="section">Proxy</directive>
447     container takes the place of the <directive module="core"
448     type="section">Directory</directive> container in the processing
449     order.</p>
451     <p>Later sections override earlier ones.</p>
453 <note><title>Technical Note</title>
454       There is actually a
455       <code>&lt;Location&gt;</code>/<code>&lt;LocationMatch&gt;</code>
456       sequence performed just before the name translation phase
457       (where <code>Aliases</code> and <code>DocumentRoots</code>
458       are used to map URLs to filenames). The results of this
459       sequence are completely thrown away after the translation has
460       completed.
461 </note>
463 <section id="merge-examples"><title>Some Examples</title>
465 <p>Below is an artificial example to show the order of
466 merging. Assuming they all apply to the request, the directives in
467 this example will be applied in the order A &gt; B &gt; C &gt; D &gt;
468 E.</p>
470 <example>
471 &lt;Location /&gt;<br />
472 E<br />
473 &lt;/Location&gt;<br />
474 <br />
475 &lt;Files f.html&gt;<br />
476 D<br />
477 &lt;/Files&gt;<br />
478 <br />
479 &lt;VirtualHost *&gt;<br />
480 &lt;Directory /a/b&gt;<br />
481 B<br />
482 &lt;/Directory&gt;<br />
483 &lt;/VirtualHost&gt;<br />
484 <br />
485 &lt;DirectoryMatch "^.*b$"&gt;<br />
486 C<br />
487 &lt;/DirectoryMatch&gt;<br />
488 <br />
489 &lt;Directory /a/b&gt;<br />
490 A<br />
491 &lt;/Directory&gt;<br />
492 <br />
493 </example>
495 <p>For a more concrete example, consider the following.  Regardless of
496 any access restrictions placed in <directive module="core"
497 type="section">Directory</directive> sections, the <directive
498 module="core" type="section">Location</directive> section will be
499 evaluated last and will allow unrestricted access to the server.  In
500 other words, order of merging is important, so be careful!</p>
502 <example>
503 &lt;Location /&gt;<br />
504 Order deny,allow<br />
505 Allow from all<br />
506 &lt;/Location&gt;<br />
507 <br />
508 # Woops!  This &lt;Directory&gt; section will have no effect<br />
509 &lt;Directory /&gt;<br />
510 Order allow,deny<br />
511 Allow from all<br />
512 Deny from badguy.example.com<br />
513 &lt;/Directory&gt;
514 </example>
516 </section>
518 </section>
519 </manualpage>