]> granicus.if.org Git - apache/blob - docs/manual/sections.xml
xforms
[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 <highlight language="config">
79 &lt;IfDefine ClosedForNow&gt;
80     Redirect / http://otherserver.example.com/
81 &lt;/IfDefine&gt;
82 </highlight>
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">MimeMagicFile</directive> directive will be
98 applied only if <module>mod_mime_magic</module> is available.</p>
99
100 <highlight language="config">
101 &lt;IfModule mod_mime_magic.c&gt;
102     MimeMagicFile conf/magic
103 &lt;/IfModule&gt;
104 </highlight>
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 <highlight language="config">
115   &lt;IfVersion >= 2.1&gt;
116     # this happens only in versions greater or
117     # equal 2.1.0.
118   &lt;/IfVersion&gt;
119 </highlight>
120
121 <p><directive type="section" module="core">IfDefine</directive>,
122 <directive type="section" module="core">IfModule</directive>, and the
123 <directive type="section" module="mod_version">IfVersion</directive>
124 can apply negative conditions by preceding their test with "!".
125 Also, these sections can be nested to achieve more complex
126 restrictions.</p>
127 </section>
128
129 <section id="file-and-web"><title>Filesystem, Webspace, and Boolean Expressions</title>
130
131 <p>The most commonly used configuration section containers are the
132 ones that change the configuration of particular places in the
133 filesystem or webspace.  First, it is important to understand the
134 difference between the two.  The filesystem is the view of your disks
135 as seen by your operating system.  For example, in a default install,
136 Apache httpd resides at <code>/usr/local/apache2</code> in the Unix
137 filesystem or <code>"c:/Program Files/Apache Group/Apache2"</code> in
138 the Windows filesystem.  (Note that forward slashes should always be
139 used as the path separator in Apache httpd configuration files, even for Windows.)  In contrast,
140 the webspace is the view of your site as delivered by the web server
141 and seen by the client.  So the path <code>/dir/</code> in the
142 webspace corresponds to the path
143 <code>/usr/local/apache2/htdocs/dir/</code> in the filesystem of a
144 default Apache httpd install on Unix.  The webspace need not map directly to
145 the filesystem, since webpages may be generated dynamically
146 from databases or other locations.</p>
147
148 <section id="filesystem"><title>Filesystem Containers</title>
149
150 <p>The <directive type="section" module="core">Directory</directive>
151 and <directive type="section" module="core">Files</directive>
152 directives, along with their <glossary ref="regex">regex</glossary>
153 counterparts, apply directives to
154 parts of the filesystem.  Directives enclosed in a <directive
155 type="section" module="core">Directory</directive> section apply to
156 the named filesystem directory and all subdirectories of that
157 directory (as well as the files in those directories).
158 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 <highlight language="config">
164 &lt;Directory /var/web/dir1&gt;
165     Options +Indexes
166 &lt;/Directory&gt;
167 </highlight>
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 <highlight language="config">
178 &lt;Files private.html&gt;
179     Order allow,deny
180     Deny from all
181 &lt;/Files&gt;
182 </highlight>
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 <highlight language="config">
195 &lt;Directory /var/web/dir1&gt;
196     &lt;Files private.html&gt;
197         Order allow,deny
198         Deny from all
199     &lt;/Files&gt;
200 &lt;/Directory&gt;
201 </highlight>
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 <highlight language="config">
218 &lt;LocationMatch ^/private&gt;
219     Order Allow,Deny
220     Deny from all
221 &lt;/Location&gt;
222 </highlight>
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 HTTP Server 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 <highlight language="config">
232 &lt;Location /server-status&gt;
233     SetHandler server-status
234 &lt;/Location&gt;
235 </highlight>
236 </section>
237
238 <section id="overlapping-webspace"><title>Overlapping Webspace</title>
239 <p>In order to have two overlapping URLs one has to consider the order in which
240 certain sections or directives are evaluated. For
241 <directive type="section" module="core">Location</directive> this would be:</p>
242 <highlight language="config">
243 &lt;Location /foo&gt;
244 &lt;/Location&gt;
245 &lt;Location /foo/bar&gt;
246 &lt;/Location>
247 </highlight>
248 <p><directive type="section" module="mod_alias">Alias</directive>es on the other hand,
249 are mapped vice-versa:</p>
250 <highlight language="config">
251 Alias /foo/bar /srv/www/uncommon/bar
252 Alias /foo /srv/www/common/foo
253 </highlight>
254 <p>The same is true for the <directive module="mod_proxy">ProxyPass</directive>
255 directives:</p>
256 <highlight language="config">
257 ProxyPass /special-area http://special.example.com smax=5 max=10
258 ProxyPass / balancer://mycluster/ stickysession=JSESSIONID|jsessionid nofailover=On
259 </highlight>
260 </section>
261
262 <section id="wildcards"><title>Wildcards and Regular Expressions</title>
263
264 <p>The <directive type="section" module="core">Directory</directive>,
265 <directive type="section" module="core">Files</directive>, and
266 <directive type="section" module="core">Location</directive>
267 directives can each use shell-style wildcard characters as in
268 <code>fnmatch</code> from the C standard library.  The character "*"
269 matches any sequence of characters, "?" matches any single character,
270 and "[<em>seq</em>]" matches any character in <em>seq</em>.  The "/"
271 character will not be matched by any wildcard; it must be specified
272 explicitly.</p>
273
274 <p>If even more flexible matching is required, each
275 container has a regular expression (regex) counterpart <directive
276 type="section" module="core">DirectoryMatch</directive>, <directive
277 type="section" module="core">FilesMatch</directive>, and <directive
278 type="section" module="core">LocationMatch</directive> that allow
279 perl-compatible
280 <glossary ref="regex">regular expressions</glossary>
281 to be used in choosing the matches.  But see the section below on
282 configuration merging to find out how using regex sections will change
283 how directives are applied.</p>
284
285 <p>A non-regex wildcard section that changes the configuration of
286 all user directories could look as follows:</p>
287
288 <highlight language="config">
289 &lt;Directory /home/*/public_html&gt;
290     Options Indexes
291 &lt;/Directory&gt;
292 </highlight>
293
294 <p>Using regex sections, we can deny access to many types of image files
295 at once:</p>
296 <highlight language="config">
297 &lt;FilesMatch \.(?i:gif|jpe?g|png)$&gt;
298     Order allow,deny
299     Deny from all
300 &lt;/FilesMatch&gt;
301 </highlight>
302
303 </section>
304
305 <section id="expressions"><title>Boolean expressions</title>
306 <p>The <directive type="section" module="core">If</directive>
307 directive change the configuration depending on a condition which can be
308 expressed by a boolean expression. For example, the following configuration
309 denies access if the HTTP Referer header does not start with
310 "http://www.example.com/".</p>
311 <highlight language="config">
312 &lt;If "!(%{HTTP_REFERER} -strmatch 'http://www.example.com/*')"&gt;
313     Require all denied
314 &lt;/If&gt;
315 </highlight>
316
317 </section>
318
319 <section id="whichwhen"><title>What to use When</title>
320
321 <p>Choosing between filesystem containers and webspace containers is
322 actually quite easy.  When applying directives to objects that reside
323 in the filesystem always use <directive type="section"
324 module="core">Directory</directive> or <directive type="section"
325 module="core">Files</directive>.  When applying directives to objects
326 that do not reside in the filesystem (such as a webpage generated from
327 a database), use <directive type="section"
328 module="core">Location</directive>.</p>
329
330 <p>It is important to never use <directive type="section"
331 module="core">Location</directive> when trying to restrict
332 access to objects in the filesystem.  This is because many
333 different webspace locations (URLs) could map to the same filesystem
334 location, allowing your restrictions to be circumvented.
335 For example, consider the following configuration:</p>
336
337 <highlight language="config">
338 &lt;Location /dir/&gt;
339     Order allow,deny
340     Deny from all
341 &lt;/Location&gt;
342 </highlight>
343
344 <p>This works fine if the request is for
345 <code>http://yoursite.example.com/dir/</code>.  But what if you are on
346 a case-insensitive filesystem?  Then your restriction could be easily
347 circumvented by requesting
348 <code>http://yoursite.example.com/DIR/</code>.  The <directive
349 type="section" module="core">Directory</directive> directive, in
350 contrast, will apply to any content served from that location,
351 regardless of how it is called.  (An exception is filesystem links.
352 The same directory can be placed in more than one part of the
353 filesystem using symbolic links.  The <directive type="section"
354 module="core">Directory</directive> directive will follow the symbolic
355 link without resetting the pathname.  Therefore, for the highest level
356 of security, symbolic links should be disabled with the appropriate
357 <directive module="core">Options</directive> directive.)</p>
358
359 <p>If you are, perhaps, thinking that none of this applies to you
360 because you use a case-sensitive filesystem, remember that there are
361 many other ways to map multiple webspace locations to the same
362 filesystem location.  Therefore you should always use the filesystem
363 containers when you can.  There is, however, one exception to this
364 rule.  Putting configuration restrictions in a <code>&lt;Location
365 /&gt;</code> section is perfectly safe because this section will apply
366 to all requests regardless of the specific URL.</p>
367 </section>
368
369 <section id="nesting"><title>Nesting of sections</title>
370
371 <p>Some section types can be nested inside other section types. One the one
372 hand, <directive type="section" module="core">Files</directive> can be used
373 inside <directive type="section" module="core">Directory</directive>.  On
374 the other hand, <directive type="section" module="core">If</directive> can
375 be used inside <directive type="section" module="core">Directory</directive>,
376 <directive type="section" module="core">Location</directive>, and <directive
377 type="section" module="core">Files</directive> sections. The regex
378 counterparts of the named section behave identically.</p>
379
380 <p>Nested sections are merged after non-nested sections of the same type.</p>
381
382 </section>
383
384 </section>
385
386 <section id="virtualhost"><title>Virtual Hosts</title>
387
388 <p>The <directive type="section" module="core">VirtualHost</directive>
389 container encloses directives that apply to specific hosts.
390 This is useful when serving multiple hosts from the same machine
391 with a different configuration for each.  For more information,
392 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
393 </section>
394
395 <section id="proxy"><title>Proxy</title>
396
397 <p>The <directive type="section" module="mod_proxy">Proxy</directive>
398 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
399 containers apply enclosed configuration directives only
400 to sites accessed through <module>mod_proxy</module>'s proxy server
401 that match the specified URL.  For example, the following configuration
402 will prevent the proxy server from being used to access the
403 <code>www.example.com</code> website.</p>
404
405 <highlight language="config">
406 &lt;Proxy http://www.example.com/*&gt;
407     Order allow,deny
408     Deny from all
409 &lt;/Proxy&gt;
410 </highlight>
411 </section>
412
413 <section id="whatwhere"><title>What Directives are Allowed?</title>
414
415 <p>To find out what directives are allowed in what types of
416 configuration sections, check the <a
417 href="mod/directive-dict.html#Context">Context</a> of the directive.
418 Everything that is allowed in
419 <directive type="section" module="core">Directory</directive>
420 sections is also syntactically allowed in
421 <directive type="section" module="core">DirectoryMatch</directive>,
422 <directive type="section" module="core">Files</directive>,
423 <directive type="section" module="core">FilesMatch</directive>,
424 <directive type="section" module="core">Location</directive>,
425 <directive type="section" module="core">LocationMatch</directive>,
426 <directive type="section" module="mod_proxy">Proxy</directive>,
427 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
428 sections.  There are some exceptions, however:</p>
429
430 <ul>
431 <li>The <directive module="core">AllowOverride</directive> directive
432 works only in <directive type="section" module="core">Directory</directive>
433 sections.</li>
434
435 <li>The <code>FollowSymLinks</code> and
436 <code>SymLinksIfOwnerMatch</code> <directive
437 module="core">Options</directive> work only in <directive
438 type="section" module="core">Directory</directive> sections or
439 <code>.htaccess</code> files.</li>
440
441 <li>The <directive module="core">Options</directive> directive cannot
442 be used in <directive type="section" module="core">Files</directive>
443 and <directive type="section" module="core">FilesMatch</directive>
444 sections.</li>
445 </ul>
446 </section>
447
448 <section id="mergin"><title>How the sections are merged</title>
449
450 <p>The configuration sections are applied in a very particular order.
451 Since this can have important effects on how configuration directives
452 are interpreted, it is important to understand how this works.</p>
453
454     <p>The order of merging is:</p>
455
456     <ol>
457       <li> <directive type="section"
458       module="core">Directory</directive> (except regular expressions)
459       and <code>.htaccess</code> done simultaneously (with
460       <code>.htaccess</code>, if allowed, overriding
461       <directive type="section" module="core">Directory</directive>)</li>
462
463       <li><directive type="section" module="core">DirectoryMatch</directive>
464       (and <code>&lt;Directory ~&gt;</code>)</li>
465
466       <li><directive type="section"
467       module="core">Files</directive> and <directive
468       type="section" module="core">FilesMatch</directive> done
469       simultaneously</li>
470
471       <li><directive type="section" module="core">Location</directive>
472       and <directive type="section"
473       module="core">LocationMatch</directive> done simultaneously</li>
474
475       <li><directive type="section" module="core">If</directive>
476       </li>
477
478     </ol>
479
480     <p>Apart from <directive type="section"
481     module="core">Directory</directive>, each group is processed in
482     the order that they appear in the configuration files.  <directive
483     type="section" module="core">Directory</directive> (group 1 above)
484     is processed in the order shortest directory component to longest.
485     So for example, <code>&lt;Directory /var/web/dir&gt;</code> will
486     be processed before <code>&lt;Directory
487     /var/web/dir/subdir&gt;</code>.  If multiple <directive
488     type="section" module="core">Directory</directive> sections apply
489     to the same directory they are processed in the configuration file
490     order. Configurations included via the <directive
491     module="core">Include</directive> directive will be treated as if
492     they were inside the including file at the location of the
493     <directive module="core">Include</directive> directive.</p>
494
495     <p>Sections inside <directive type="section"
496     module="core">VirtualHost</directive> sections
497     are applied <em>after</em> the corresponding sections outside
498     the virtual host definition. This allows virtual hosts to
499     override the main server configuration.</p>
500
501     <p>When the request is served by <module>mod_proxy</module>, the
502     <directive module="mod_proxy" type="section">Proxy</directive>
503     container takes the place of the <directive module="core"
504     type="section">Directory</directive> container in the processing
505     order.</p>
506
507     <p>Later sections override earlier ones, however each module is responsible
508     for interpeting what form this override takes.  A later configuration section 
509     with directives from a given module might cause a conceptual "merge" of some
510     directives, all directives, or a complete replacement of the modules 
511     configuration with the module defaults and directives explicitly listed in 
512     the later context.</p>
513
514 <note><title>Technical Note</title>
515       There is actually a
516       <code>&lt;Location&gt;</code>/<code>&lt;LocationMatch&gt;</code>
517       sequence performed just before the name translation phase
518       (where <code>Aliases</code> and <code>DocumentRoots</code>
519       are used to map URLs to filenames). The results of this
520       sequence are completely thrown away after the translation has
521       completed.
522 </note>
523
524 <section id="merge-examples"><title>Some Examples</title>
525
526 <p>Below is an artificial example to show the order of
527 merging. Assuming they all apply to the request, the directives in
528 this example will be applied in the order A &gt; B &gt; C &gt; D &gt;
529 E.</p>
530
531 <highlight language="config">
532 &lt;Location /&gt;
533     E
534 &lt;/Location&gt;
535
536 &lt;Files f.html&gt;
537     D
538 &lt;/Files&gt;
539
540 &lt;VirtualHost *&gt;
541 &lt;Directory /a/b&gt;
542     B
543 &lt;/Directory&gt;
544 &lt;/VirtualHost&gt;
545
546 &lt;DirectoryMatch "^.*b$"&gt;
547     C
548 &lt;/DirectoryMatch&gt;
549
550 &lt;Directory /a/b&gt;
551     A
552 &lt;/Directory&gt;
553
554 </highlight>
555
556 <p>For a more concrete example, consider the following.  Regardless of
557 any access restrictions placed in <directive module="core"
558 type="section">Directory</directive> sections, the <directive
559 module="core" type="section">Location</directive> section will be
560 evaluated last and will allow unrestricted access to the server.  In
561 other words, order of merging is important, so be careful!</p>
562
563 <highlight language="config">
564 &lt;Location /&gt;
565     Order deny,allow
566     Allow from all
567 &lt;/Location&gt;
568
569 # Woops!  This &lt;Directory&gt; section will have no effect
570 &lt;Directory /&gt;
571     Order allow,deny
572     Allow from all
573     Deny from badguy.example.com
574 &lt;/Directory&gt;
575 </highlight>
576
577 </section>
578
579 </section>
580 </manualpage>