]> granicus.if.org Git - apache/blob - docs/manual/sections.xml
Rebuild.
[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 <p>Regular expressions containing <strong>named groups and
300 backreferences</strong> are added to the environment with the
301 corresponding name in uppercase. This allows elements of filename paths
302 and URLs to be referenced from within <a href="expr.html">expressions</a>
303 and modules like <module>mod_rewrite</module>.</p>
304
305 <highlight language="config">
306 &lt;DirectoryMatch ^/var/www/combined/(?&lt;SITENAME&gt;[^/]+)&gt;
307     require ldap-group cn=%{env:MATCH_SITENAME},ou=combined,o=Example
308 &lt;/DirectoryMatch&gt;
309 </highlight>
310
311 </section>
312
313 <section id="expressions"><title>Boolean expressions</title>
314 <p>The <directive type="section" module="core">If</directive>
315 directive change the configuration depending on a condition which can be
316 expressed by a boolean expression. For example, the following configuration
317 denies access if the HTTP Referer header does not start with
318 "http://www.example.com/".</p>
319 <highlight language="config">
320 &lt;If "!(%{HTTP_REFERER} -strmatch 'http://www.example.com/*')"&gt;
321     Require all denied
322 &lt;/If&gt;
323 </highlight>
324
325 </section>
326
327 <section id="whichwhen"><title>What to use When</title>
328
329 <p>Choosing between filesystem containers and webspace containers is
330 actually quite easy.  When applying directives to objects that reside
331 in the filesystem always use <directive type="section"
332 module="core">Directory</directive> or <directive type="section"
333 module="core">Files</directive>.  When applying directives to objects
334 that do not reside in the filesystem (such as a webpage generated from
335 a database), use <directive type="section"
336 module="core">Location</directive>.</p>
337
338 <p>It is important to never use <directive type="section"
339 module="core">Location</directive> when trying to restrict
340 access to objects in the filesystem.  This is because many
341 different webspace locations (URLs) could map to the same filesystem
342 location, allowing your restrictions to be circumvented.
343 For example, consider the following configuration:</p>
344
345 <highlight language="config">
346 &lt;Location /dir/&gt;
347     Require all denied
348 &lt;/Location&gt;
349 </highlight>
350
351 <p>This works fine if the request is for
352 <code>http://yoursite.example.com/dir/</code>.  But what if you are on
353 a case-insensitive filesystem?  Then your restriction could be easily
354 circumvented by requesting
355 <code>http://yoursite.example.com/DIR/</code>.  The <directive
356 type="section" module="core">Directory</directive> directive, in
357 contrast, will apply to any content served from that location,
358 regardless of how it is called.  (An exception is filesystem links.
359 The same directory can be placed in more than one part of the
360 filesystem using symbolic links.  The <directive type="section"
361 module="core">Directory</directive> directive will follow the symbolic
362 link without resetting the pathname.  Therefore, for the highest level
363 of security, symbolic links should be disabled with the appropriate
364 <directive module="core">Options</directive> directive.)</p>
365
366 <p>If you are, perhaps, thinking that none of this applies to you
367 because you use a case-sensitive filesystem, remember that there are
368 many other ways to map multiple webspace locations to the same
369 filesystem location.  Therefore you should always use the filesystem
370 containers when you can.  There is, however, one exception to this
371 rule.  Putting configuration restrictions in a <code>&lt;Location
372 /&gt;</code> section is perfectly safe because this section will apply
373 to all requests regardless of the specific URL.</p>
374 </section>
375
376 <section id="nesting"><title>Nesting of sections</title>
377
378 <p>Some section types can be nested inside other section types. On the one
379 hand, <directive type="section" module="core">Files</directive> can be used
380 inside <directive type="section" module="core">Directory</directive>.  On
381 the other hand, <directive type="section" module="core">If</directive> can
382 be used inside <directive type="section" module="core">Directory</directive>,
383 <directive type="section" module="core">Location</directive>, and <directive
384 type="section" module="core">Files</directive> sections. The regex
385 counterparts of the named section behave identically.</p>
386
387 <p>Nested sections are merged after non-nested sections of the same type.</p>
388
389 </section>
390
391 </section>
392
393 <section id="virtualhost"><title>Virtual Hosts</title>
394
395 <p>The <directive type="section" module="core">VirtualHost</directive>
396 container encloses directives that apply to specific hosts.
397 This is useful when serving multiple hosts from the same machine
398 with a different configuration for each.  For more information,
399 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
400 </section>
401
402 <section id="proxy"><title>Proxy</title>
403
404 <p>The <directive type="section" module="mod_proxy">Proxy</directive>
405 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
406 containers apply enclosed configuration directives only
407 to sites accessed through <module>mod_proxy</module>'s proxy server
408 that match the specified URL.  For example, the following configuration
409 will prevent the proxy server from being used to access the
410 <code>www.example.com</code> website.</p>
411
412 <highlight language="config">
413 &lt;Proxy http://www.example.com/*&gt;
414     Require all granted
415 &lt;/Proxy&gt;
416 </highlight>
417 </section>
418
419 <section id="whatwhere"><title>What Directives are Allowed?</title>
420
421 <p>To find out what directives are allowed in what types of
422 configuration sections, check the <a
423 href="mod/directive-dict.html#Context">Context</a> of the directive.
424 Everything that is allowed in
425 <directive type="section" module="core">Directory</directive>
426 sections is also syntactically allowed in
427 <directive type="section" module="core">DirectoryMatch</directive>,
428 <directive type="section" module="core">Files</directive>,
429 <directive type="section" module="core">FilesMatch</directive>,
430 <directive type="section" module="core">Location</directive>,
431 <directive type="section" module="core">LocationMatch</directive>,
432 <directive type="section" module="mod_proxy">Proxy</directive>,
433 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
434 sections.  There are some exceptions, however:</p>
435
436 <ul>
437 <li>The <directive module="core">AllowOverride</directive> directive
438 works only in <directive type="section" module="core">Directory</directive>
439 sections.</li>
440
441 <li>The <code>FollowSymLinks</code> and
442 <code>SymLinksIfOwnerMatch</code> <directive
443 module="core">Options</directive> work only in <directive
444 type="section" module="core">Directory</directive> sections or
445 <code>.htaccess</code> files.</li>
446
447 <li>The <directive module="core">Options</directive> directive cannot
448 be used in <directive type="section" module="core">Files</directive>
449 and <directive type="section" module="core">FilesMatch</directive>
450 sections.</li>
451 </ul>
452 </section>
453
454 <section id="merging"><title>How the sections are merged</title>
455
456 <p>The configuration sections are applied in a very particular order.
457 Since this can have important effects on how configuration directives
458 are interpreted, it is important to understand how this works.</p>
459
460     <p>The order of merging is:</p>
461
462     <ol>
463       <li> <directive type="section"
464       module="core">Directory</directive> (except regular expressions)
465       and <code>.htaccess</code> done simultaneously (with
466       <code>.htaccess</code>, if allowed, overriding
467       <directive type="section" module="core">Directory</directive>)</li>
468
469       <li><directive type="section" module="core">DirectoryMatch</directive>
470       (and <code>&lt;Directory ~&gt;</code>)</li>
471
472       <li><directive type="section"
473       module="core">Files</directive> and <directive
474       type="section" module="core">FilesMatch</directive> done
475       simultaneously</li>
476
477       <li><directive type="section" module="core">Location</directive>
478       and <directive type="section"
479       module="core">LocationMatch</directive> done simultaneously</li>
480
481       <li><directive type="section" module="core">If</directive>
482       </li>
483
484     </ol>
485
486     <p>Apart from <directive type="section"
487     module="core">Directory</directive>, each group is processed in
488     the order that they appear in the configuration files.  <directive
489     type="section" module="core">Directory</directive> (group 1 above)
490     is processed in the order shortest directory component to longest.
491     So for example, <code>&lt;Directory /var/web/dir&gt;</code> will
492     be processed before <code>&lt;Directory
493     /var/web/dir/subdir&gt;</code>.  If multiple <directive
494     type="section" module="core">Directory</directive> sections apply
495     to the same directory they are processed in the configuration file
496     order. Configurations included via the <directive
497     module="core">Include</directive> directive will be treated as if
498     they were inside the including file at the location of the
499     <directive module="core">Include</directive> directive.</p>
500
501     <p>Sections inside <directive type="section"
502     module="core">VirtualHost</directive> sections
503     are applied <em>after</em> the corresponding sections outside
504     the virtual host definition. This allows virtual hosts to
505     override the main server configuration.</p>
506
507     <p>When the request is served by <module>mod_proxy</module>, the
508     <directive module="mod_proxy" type="section">Proxy</directive>
509     container takes the place of the <directive module="core"
510     type="section">Directory</directive> container in the processing
511     order.</p>
512
513     <p>Later sections override earlier ones, however each module is responsible
514     for interpreting what form this override takes.  A later configuration section 
515     with directives from a given module might cause a conceptual "merge" of some
516     directives, all directives, or a complete replacement of the modules 
517     configuration with the module defaults and directives explicitly listed in 
518     the later context.</p>
519
520 <note><title>Technical Note</title>
521       There is actually a
522       <code>&lt;Location&gt;</code>/<code>&lt;LocationMatch&gt;</code>
523       sequence performed just before the name translation phase
524       (where <code>Aliases</code> and <code>DocumentRoots</code>
525       are used to map URLs to filenames). The results of this
526       sequence are completely thrown away after the translation has
527       completed.
528 </note>
529
530 <section id="merge-examples"><title>Some Examples</title>
531
532 <p>Below is an artificial example to show the order of
533 merging. Assuming they all apply to the request, the directives in
534 this example will be applied in the order A &gt; B &gt; C &gt; D &gt;
535 E.</p>
536
537 <highlight language="config">
538 &lt;Location /&gt;
539     E
540 &lt;/Location&gt;
541
542 &lt;Files f.html&gt;
543     D
544 &lt;/Files&gt;
545
546 &lt;VirtualHost *&gt;
547 &lt;Directory /a/b&gt;
548     B
549 &lt;/Directory&gt;
550 &lt;/VirtualHost&gt;
551
552 &lt;DirectoryMatch "^.*b$"&gt;
553     C
554 &lt;/DirectoryMatch&gt;
555
556 &lt;Directory /a/b&gt;
557     A
558 &lt;/Directory&gt;
559
560 </highlight>
561
562 <p>For a more concrete example, consider the following.  Regardless of
563 any access restrictions placed in <directive module="core"
564 type="section">Directory</directive> sections, the <directive
565 module="core" type="section">Location</directive> section will be
566 evaluated last and will allow unrestricted access to the server.  In
567 other words, order of merging is important, so be careful!</p>
568
569 <highlight language="config">
570 &lt;Location /&gt;
571     Require all granted
572 &lt;/Location&gt;
573
574 # Woops!  This &lt;Directory&gt; section will have no effect
575 &lt;Directory /&gt;
576     &lt;RequireAll&gt;
577         Require all granted
578         Require not host badguy.example.com
579     &lt;/RequireAll&gt;
580 &lt;/Directory&gt;
581 </highlight>
582
583 </section>
584
585 </section>
586 </manualpage>