]> granicus.if.org Git - apache/blob - docs/manual/sections.xml
PR50376 clarify that <Directory> applies to the files under the Directory, not just...
[apache] / docs / manual / sections.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  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
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="sections.xml.meta">
24
25 <title>Configuration Sections</title>
26
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>
34
35 <section id="types"><title>Types of Configuration Section Containers</title>
36
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>
59
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>
70
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>
77
78 <example>
79 &lt;IfDefine ClosedForNow&gt;<br />
80 Redirect / http://otherserver.example.com/<br />
81 &lt;/IfDefine&gt;
82 </example>
83
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>
95
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>
99
100 <example>
101 &lt;IfModule mod_mime_magic.c&gt;<br />
102 MimeMagicFile conf/magic<br />
103 &lt;/IfModule&gt;
104 </example>
105
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>
113
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>
122
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>
130
131 <section id="file-and-web"><title>Filesystem and Webspace</title>
132
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 httpd 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 httpd configuration files, 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 httpd 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>
149
150 <section id="filesystem"><title>Filesystem Containers</title>
151
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 (as well as the files in those directories).  
160 The same effect can be obtained using <a
161 href="howto/htaccess.html">.htaccess files</a>.  For example, in the
162 following configuration, directory indexes will be enabled for the
163 <code>/var/web/dir1</code> directory and all subdirectories.</p>
164
165 <example>
166 &lt;Directory /var/web/dir1&gt;<br />
167 Options +Indexes<br />
168 &lt;/Directory&gt;
169 </example>
170
171 <p>Directives enclosed in a <directive type="section"
172 module="core">Files</directive> section apply to any file with
173 the specified name, regardless of what directory it lies in.
174 So for example, the following configuration directives will,
175 when placed in the main section of the configuration file,
176 deny access to any file named <code>private.html</code> regardless
177 of where it is found.</p>
178
179 <example>
180 &lt;Files private.html&gt;<br />
181 Order allow,deny<br />
182 Deny from all<br />
183 &lt;/Files&gt;
184 </example>
185
186 <p>To address files found in a particular part of the filesystem, the
187 <directive type="section" module="core">Files</directive> and
188 <directive type="section" module="core">Directory</directive> sections
189 can be combined.  For example, the following configuration will deny
190 access to <code>/var/web/dir1/private.html</code>,
191 <code>/var/web/dir1/subdir2/private.html</code>,
192 <code>/var/web/dir1/subdir3/private.html</code>, and any other instance
193 of <code>private.html</code> found under the <code>/var/web/dir1/</code>
194 directory.</p>
195
196 <example>
197 &lt;Directory /var/web/dir1&gt;<br />
198 &lt;Files private.html&gt;<br />
199 Order allow,deny<br />
200 Deny from all<br />
201 &lt;/Files&gt;<br />
202 &lt;/Directory&gt;
203 </example>
204 </section>
205
206 <section id="webspace"><title>Webspace Containers</title>
207
208 <p>The <directive type="section" module="core">Location</directive>
209 directive and its <glossary ref="regex">regex</glossary> counterpart, on
210 the other hand, change the
211 configuration for content in the webspace.  For example, the following
212 configuration prevents access to any URL-path that begins in /private.
213 In particular, it will apply to requests for
214 <code>http://yoursite.example.com/private</code>,
215 <code>http://yoursite.example.com/private123</code>, and
216 <code>http://yoursite.example.com/private/dir/file.html</code> as well
217 as any other requests starting with the <code>/private</code> string.</p>
218
219 <example>
220 &lt;Location /private&gt;<br />
221 Order Allow,Deny<br />
222 Deny from all<br />
223 &lt;/Location&gt;
224 </example>
225
226 <p>The <directive type="section" module="core">Location</directive>
227 directive need not have anything to do with the filesystem.
228 For example, the following example shows how to map a particular
229 URL to an internal Apache HTTP Server handler provided by <module>mod_status</module>.
230 No file called <code>server-status</code> needs to exist in the
231 filesystem.</p>
232
233 <example>
234 &lt;Location /server-status&gt;<br />
235 SetHandler server-status<br />
236 &lt;/Location&gt;
237 </example>
238 </section>
239
240 <section id="overlapping-webspace"><title>Overlapping Webspace</title>
241 <p>In order to have two overlapping URLs one has to consider the order in which
242 certain sections or directives are evaluated. For
243 <directive type="section" module="core">Location</directive> this would be:</p>
244 <example>
245 &lt;Location /foo&gt;<br />
246 &lt;/Location&gt;<br />
247 &lt;Location /foo/bar&gt;<br />
248 &lt;/Location>
249 </example>
250 <p><directive type="section" module="core">Alias</directive>es on the other hand,
251 are mapped vice-versa:</p>
252 <example>
253 Alias /foo/bar /srv/www/uncommon/bar<br />
254 Alias /foo /srv/www/common/foo<br />
255 </example>
256 <p>The same is true for the <directive module="mod_proxy">ProxyPass</directive>
257 directives:</p>
258 <example>
259 ProxyPass /special-area http://special.example.com smax=5 max=10<br />
260 ProxyPass / balancer://mycluster/ stickysession=JSESSIONID|jsessionid nofailover=On
261 </example>
262 </section> 
263
264 <section id="wildcards"><title>Wildcards and Regular Expressions</title>
265
266 <p>The <directive type="section" module="core">Directory</directive>,
267 <directive type="section" module="core">Files</directive>, and
268 <directive type="section" module="core">Location</directive>
269 directives can each use shell-style wildcard characters as in
270 <code>fnmatch</code> from the C standard library.  The character "*"
271 matches any sequence of characters, "?" matches any single character,
272 and "[<em>seq</em>]" matches any character in <em>seq</em>.  The "/"
273 character will not be matched by any wildcard; it must be specified
274 explicitly.</p>
275
276 <p>If even more flexible matching is required, each
277 container has a regular expression (regex) counterpart <directive
278 type="section" module="core">DirectoryMatch</directive>, <directive
279 type="section" module="core">FilesMatch</directive>, and <directive
280 type="section" module="core">LocationMatch</directive> that allow
281 perl-compatible
282 <glossary ref="regex">regular expressions</glossary>
283 to be used in choosing the matches.  But see the section below on
284 configuration merging to find out how using regex sections will change
285 how directives are applied.</p>
286
287 <p>A non-regex wildcard section that changes the configuration of
288 all user directories could look as follows:</p>
289
290 <example>
291 &lt;Directory /home/*/public_html&gt;<br />
292 Options Indexes<br />
293 &lt;/Directory&gt;
294 </example>
295
296 <p>Using regex sections, we can deny access to many types of image files
297 at once:</p>
298 <example>
299 &lt;FilesMatch \.(?i:gif|jpe?g|png)$&gt;<br />
300 Order allow,deny<br />
301 Deny from all<br />
302 &lt;/FilesMatch&gt;
303 </example>
304
305 </section>
306
307 <section id="whichwhen"><title>What to use When</title>
308
309 <p>Choosing between filesystem containers and webspace containers is
310 actually quite easy.  When applying directives to objects that reside
311 in the filesystem always use <directive type="section"
312 module="core">Directory</directive> or <directive type="section"
313 module="core">Files</directive>.  When applying directives to objects
314 that do not reside in the filesystem (such as a webpage generated from
315 a database), use <directive type="section"
316 module="core">Location</directive>.</p>
317
318 <p>It is important to never use <directive type="section"
319 module="core">Location</directive> when trying to restrict
320 access to objects in the filesystem.  This is because many
321 different webspace locations (URLs) could map to the same filesystem
322 location, allowing your restrictions to be circumvented.
323 For example, consider the following configuration:</p>
324
325 <example>
326 &lt;Location /dir/&gt;<br />
327 Order allow,deny<br />
328 Deny from all<br />
329 &lt;/Location&gt;
330 </example>
331
332 <p>This works fine if the request is for
333 <code>http://yoursite.example.com/dir/</code>.  But what if you are on
334 a case-insensitive filesystem?  Then your restriction could be easily
335 circumvented by requesting
336 <code>http://yoursite.example.com/DIR/</code>.  The <directive
337 type="section" module="core">Directory</directive> directive, in
338 contrast, will apply to any content served from that location,
339 regardless of how it is called.  (An exception is filesystem links.
340 The same directory can be placed in more than one part of the
341 filesystem using symbolic links.  The <directive type="section"
342 module="core">Directory</directive> directive will follow the symbolic
343 link without resetting the pathname.  Therefore, for the highest level
344 of security, symbolic links should be disabled with the appropriate
345 <directive module="core">Options</directive> directive.)</p>
346
347 <p>If you are, perhaps, thinking that none of this applies to you
348 because you use a case-sensitive filesystem, remember that there are
349 many other ways to map multiple webspace locations to the same
350 filesystem location.  Therefore you should always use the filesystem
351 containers when you can.  There is, however, one exception to this
352 rule.  Putting configuration restrictions in a <code>&lt;Location
353 /&gt;</code> section is perfectly safe because this section will apply
354 to all requests regardless of the specific URL.</p>
355 </section>
356
357 </section>
358
359 <section id="virtualhost"><title>Virtual Hosts</title>
360
361 <p>The <directive type="section" module="core">VirtualHost</directive>
362 container encloses directives that apply to specific hosts.
363 This is useful when serving multiple hosts from the same machine
364 with a different configuration for each.  For more information,
365 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
366 </section>
367
368 <section id="proxy"><title>Proxy</title>
369
370 <p>The <directive type="section" module="mod_proxy">Proxy</directive>
371 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
372 containers apply enclosed configuration directives only
373 to sites accessed through <module>mod_proxy</module>'s proxy server
374 that match the specified URL.  For example, the following configuration
375 will prevent the proxy server from being used to access the
376 <code>cnn.com</code> website.</p>
377
378 <example>
379 &lt;Proxy http://cnn.com/*&gt;<br />
380 Order allow,deny<br />
381 Deny from all<br />
382 &lt;/Proxy&gt;
383 </example>
384 </section>
385
386 <section id="whatwhere"><title>What Directives are Allowed?</title>
387
388 <p>To find out what directives are allowed in what types of
389 configuration sections, check the <a
390 href="mod/directive-dict.html#Context">Context</a> of the directive.
391 Everything that is allowed in 
392 <directive type="section" module="core">Directory</directive>
393 sections is also syntactically allowed in
394 <directive type="section" module="core">DirectoryMatch</directive>,
395 <directive type="section" module="core">Files</directive>,
396 <directive type="section" module="core">FilesMatch</directive>,
397 <directive type="section" module="core">Location</directive>,
398 <directive type="section" module="core">LocationMatch</directive>,
399 <directive type="section" module="mod_proxy">Proxy</directive>,
400 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
401 sections.  There are some exceptions, however:</p>
402
403 <ul>
404 <li>The <directive module="core">AllowOverride</directive> directive
405 works only in <directive type="section" module="core">Directory</directive>
406 sections.</li>
407
408 <li>The <code>FollowSymLinks</code> and
409 <code>SymLinksIfOwnerMatch</code> <directive
410 module="core">Options</directive> work only in <directive
411 type="section" module="core">Directory</directive> sections or
412 <code>.htaccess</code> files.</li>
413
414 <li>The <directive module="core">Options</directive> directive cannot
415 be used in <directive type="section" module="core">Files</directive>
416 and <directive type="section" module="core">FilesMatch</directive>
417 sections.</li>
418 </ul>
419 </section>
420
421 <section id="mergin"><title>How the sections are merged</title>
422
423 <p>The configuration sections are applied in a very particular order.
424 Since this can have important effects on how configuration directives
425 are interpreted, it is important to understand how this works.</p>
426
427     <p>The order of merging is:</p>
428
429     <ol>
430       <li> <directive type="section"
431       module="core">Directory</directive> (except regular expressions)
432       and <code>.htaccess</code> done simultaneously (with
433       <code>.htaccess</code>, if allowed, overriding
434       <directive type="section" module="core">Directory</directive>)</li>
435
436       <li><directive type="section" module="core">DirectoryMatch</directive>
437       (and <code>&lt;Directory ~&gt;</code>)</li>
438
439       <li><directive type="section"
440       module="core">Files</directive> and <directive
441       type="section" module="core">FilesMatch</directive> done
442       simultaneously</li>
443
444       <li><directive type="section" module="core">Location</directive>
445       and <directive type="section"
446       module="core">LocationMatch</directive> done simultaneously</li>
447     </ol>
448
449     <p>Apart from <directive type="section"
450     module="core">Directory</directive>, each group is processed in
451     the order that they appear in the configuration files.  <directive
452     type="section" module="core">Directory</directive> (group 1 above)
453     is processed in the order shortest directory component to longest.
454     So for example, <code>&lt;Directory /var/web/dir&gt;</code> will
455     be processed before <code>&lt;Directory
456     /var/web/dir/subdir&gt;</code>.  If multiple <directive
457     type="section" module="core">Directory</directive> sections apply
458     to the same directory they are processed in the configuration file
459     order. Configurations included via the <directive
460     module="core">Include</directive> directive will be treated as if
461     they were inside the including file at the location of the
462     <directive module="core">Include</directive> directive.</p>
463
464     <p>Sections inside <directive type="section"
465     module="core">VirtualHost</directive> sections
466     are applied <em>after</em> the corresponding sections outside
467     the virtual host definition. This allows virtual hosts to
468     override the main server configuration.</p>
469
470     <p>When the request is served by <module>mod_proxy</module>, the
471     <directive module="mod_proxy" type="section">Proxy</directive>
472     container takes the place of the <directive module="core"
473     type="section">Directory</directive> container in the processing
474     order.</p>
475
476     <p>Later sections override earlier ones.</p>
477
478 <note><title>Technical Note</title>
479       There is actually a
480       <code>&lt;Location&gt;</code>/<code>&lt;LocationMatch&gt;</code>
481       sequence performed just before the name translation phase
482       (where <code>Aliases</code> and <code>DocumentRoots</code>
483       are used to map URLs to filenames). The results of this
484       sequence are completely thrown away after the translation has
485       completed.
486 </note>
487
488 <section id="merge-examples"><title>Some Examples</title>
489
490 <p>Below is an artificial example to show the order of
491 merging. Assuming they all apply to the request, the directives in
492 this example will be applied in the order A &gt; B &gt; C &gt; D &gt;
493 E.</p>
494
495 <example>
496 &lt;Location /&gt;<br />
497 E<br />
498 &lt;/Location&gt;<br />
499 <br />
500 &lt;Files f.html&gt;<br />
501 D<br />
502 &lt;/Files&gt;<br />
503 <br />
504 &lt;VirtualHost *&gt;<br />
505 &lt;Directory /a/b&gt;<br />
506 B<br />
507 &lt;/Directory&gt;<br />
508 &lt;/VirtualHost&gt;<br />
509 <br />
510 &lt;DirectoryMatch "^.*b$"&gt;<br />
511 C<br />
512 &lt;/DirectoryMatch&gt;<br />
513 <br />
514 &lt;Directory /a/b&gt;<br />
515 A<br />
516 &lt;/Directory&gt;<br />
517 <br />
518 </example>
519
520 <p>For a more concrete example, consider the following.  Regardless of
521 any access restrictions placed in <directive module="core"
522 type="section">Directory</directive> sections, the <directive
523 module="core" type="section">Location</directive> section will be
524 evaluated last and will allow unrestricted access to the server.  In
525 other words, order of merging is important, so be careful!</p>
526
527 <example>
528 &lt;Location /&gt;<br />
529 Order deny,allow<br />
530 Allow from all<br />
531 &lt;/Location&gt;<br />
532 <br />
533 # Woops!  This &lt;Directory&gt; section will have no effect<br />
534 &lt;Directory /&gt;<br />
535 Order allow,deny<br />
536 Allow from all<br />
537 Deny from badguy.example.com<br />
538 &lt;/Directory&gt;
539 </example>
540
541 </section>
542
543 </section>
544 </manualpage>