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