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