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