]> granicus.if.org Git - apache/blob - docs/manual/env.xml
mark async websockets tunnel stuff as experimental
[apache] / docs / manual / env.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="env.xml.meta">
24
25   <title>Environment Variables in Apache</title>
26
27   <summary>
28     <p>There are two kinds of environment variables that affect
29     the Apache HTTP Server.</p>
30
31     <p>First, there are the environment variables controlled by
32     the underlying operating system.  These are set before the
33     server starts.  They can be used in expansions in configuration
34     files, and can optionally be passed to CGI scripts and SSI
35     using the PassEnv directive.</p>
36
37     <p>Second, the Apache HTTP Server provides a mechanism for storing
38     information in named variables that are also called <em>environment
39     variables</em>. This information can be used to control various
40     operations such as logging or access control. The variables are
41     also used as a mechanism to communicate with external programs
42     such as CGI scripts. This document discusses different ways to
43     manipulate and use these variables.</p>
44
45     <p>Although these variables are referred to as <em>environment
46     variables</em>, they are not the same as the environment
47     variables controlled by the underlying operating system.
48     Instead, these variables are stored and manipulated in an
49     internal Apache structure. They only become actual operating
50     system environment variables when they are provided to CGI
51     scripts and Server Side Include scripts. If you wish to
52     manipulate the operating system environment under which the
53     server itself runs, you must use the standard environment
54     manipulation mechanisms provided by your operating system
55     shell.</p>
56   </summary>
57
58   <section id="setting">
59     <title>Setting Environment Variables</title>
60     <related>
61       <modulelist>
62         <module>mod_cache</module>
63         <module>mod_env</module>
64         <module>mod_rewrite</module>
65         <module>mod_setenvif</module>
66         <module>mod_unique_id</module>
67       </modulelist>
68       <directivelist>
69         <directive module="mod_setenvif">BrowserMatch</directive>
70         <directive module="mod_setenvif">BrowserMatchNoCase</directive>
71         <directive module="mod_env">PassEnv</directive>
72         <directive module="mod_rewrite">RewriteRule</directive>
73         <directive module="mod_env">SetEnv</directive>
74         <directive module="mod_setenvif">SetEnvIf</directive>
75         <directive module="mod_setenvif">SetEnvIfNoCase</directive>
76         <directive module="mod_env">UnsetEnv</directive>
77       </directivelist>
78     </related>
79
80     <section id="basic-manipulation">
81         <title>Basic Environment Manipulation</title>
82
83         <p>The most basic way to set an environment variable in Apache
84         is using the unconditional <directive module="mod_env"
85         >SetEnv</directive> directive. Variables may also be passed from
86         the environment of the shell which started the server using the
87         <directive module="mod_env">PassEnv</directive> directive.</p>
88
89     </section>
90     <section id="conditional">
91         <title>Conditional Per-Request Settings</title>
92
93         <p>For additional flexibility, the directives provided by
94         <module>mod_setenvif</module> allow environment variables to be set
95         on a per-request basis, conditional on characteristics of particular
96         requests. For example, a variable could be set only when a
97         specific browser (User-Agent) is making a request, or only when
98         a specific Referer [sic] header is found. Even more flexibility
99         is available through the <module>mod_rewrite</module>'s <directive
100         module="mod_rewrite">RewriteRule</directive> which uses the
101         <code>[E=...]</code> option to set environment variables.</p>
102
103     </section>
104     <section id="unique-identifiers">
105         <title>Unique Identifiers</title>
106
107         <p>Finally, <module>mod_unique_id</module> sets the environment
108         variable <code>UNIQUE_ID</code> for each request to a value which is
109         guaranteed to be unique across "all" requests under very
110         specific conditions.</p>
111
112     </section>
113     <section id="standard-cgi">
114         <title>Standard CGI Variables</title>
115
116         <p>In addition to all environment variables set within the
117         Apache configuration and passed from the shell, CGI scripts and
118         SSI pages are provided with a set of environment variables
119         containing meta-information about the request as required by
120         the <a href="http://www.ietf.org/rfc/rfc3875">CGI
121         specification</a>.</p>
122
123     </section>
124     <section id="caveats">
125         <title>Some Caveats</title>
126
127         <ul>
128           <li>It is not possible to override or change the standard CGI
129           variables using the environment manipulation directives.</li>
130
131           <li>When <program>suexec</program> is used to launch
132           CGI scripts, the environment will be cleaned down to a set of
133           <em>safe</em> variables before CGI scripts are launched. The
134           list of <em>safe</em> variables is defined at compile-time in
135           <code>suexec.c</code>.</li>
136
137           <li>For portability reasons, the names of environment
138           variables may contain only letters, numbers, and the
139           underscore character. In addition, the first character may
140           not be a number. Characters which do not match this
141           restriction will be replaced by an underscore when passed to
142           CGI scripts and SSI pages.</li>
143
144           <li>A special case are HTTP headers which are passed to CGI
145           scripts and the like via environment variables (see below).
146           They are converted to uppercase and only dashes are replaced with
147           underscores; if the header contains any other (invalid) character,
148           the whole header is silently dropped. See <a href="#fixheader">
149           below</a> for a workaround.</li>
150
151           <li>The <directive module="mod_env">SetEnv</directive> directive runs
152           late during request processing meaning that directives such as
153           <directive module="mod_setenvif">SetEnvIf</directive> and <directive
154           module="mod_rewrite">RewriteCond</directive> will not see the
155           variables set with it.</li>
156  
157           <li>When the server looks up a path via an internal 
158           <glossary ref="subrequest">subrequest</glossary> such as looking 
159           for a <directive module="mod_dir" >DirectoryIndex</directive> 
160           or generating a directory listing with <module>mod_autoindex</module>,
161           per-request environment variables are <em>not</em> inherited in the 
162           subrequest. Additionally, 
163           <directive module="mod_setenvif">SetEnvIf</directive> directives
164           are not separately evaluated in the subrequest due to the API phases
165            <module>mod_setenvif</module> takes action in.</li>
166         </ul>
167     </section>
168   </section>
169   <section id="using">
170     <title>Using Environment Variables</title>
171
172     <related>
173       <modulelist>
174         <module>mod_authz_host</module>
175         <module>mod_cgi</module>
176         <module>mod_ext_filter</module>
177         <module>mod_headers</module>
178         <module>mod_include</module>
179         <module>mod_log_config</module>
180         <module>mod_rewrite</module>
181       </modulelist>
182       <directivelist>
183         <directive module="mod_authz_core">Require</directive>
184         <directive module="mod_log_config">CustomLog</directive>
185         <directive module="mod_access_compat">Deny</directive>
186         <directive module="mod_ext_filter">ExtFilterDefine</directive>
187         <directive module="mod_headers">Header</directive>
188         <directive module="mod_log_config">LogFormat</directive>
189         <directive module="mod_rewrite">RewriteCond</directive>
190         <directive module="mod_rewrite">RewriteRule</directive>
191       </directivelist>
192     </related>
193
194     <section id="cgi-scripts">
195         <title>CGI Scripts</title>
196
197         <p>One of the primary uses of environment variables is to
198         communicate information to CGI scripts. As discussed above, the
199         environment passed to CGI scripts includes standard
200         meta-information about the request in addition to any variables
201         set within the Apache configuration. For more details, see the
202         <a href="howto/cgi.html">CGI tutorial</a>.</p>
203
204     </section>
205     <section id="ssi-pages">
206         <title>SSI Pages</title>
207
208         <p>Server-parsed (SSI) documents processed by
209         <module>mod_include</module>'s
210         <code>INCLUDES</code> filter can print environment variables
211         using the <code>echo</code> element, and can use environment
212         variables in flow control elements to makes parts of a page
213         conditional on characteristics of a request. Apache also
214         provides SSI pages with the standard CGI environment variables
215         as discussed above. For more details, see the <a
216         href="howto/ssi.html">SSI tutorial</a>.</p>
217
218     </section>
219     <section id="access-control">
220         <title>Access Control</title>
221
222         <p>Access to the server can be controlled based on the value of
223         environment variables using the <code>allow from env=</code>
224         and <code>deny from env=</code> directives. In combination with
225         <directive module="mod_setenvif">SetEnvIf</directive>, this
226         allows for flexible control of access to the server based on
227         characteristics of the client. For example, you can use these
228         directives to deny access to a particular browser (User-Agent).
229         </p>
230
231     </section>
232     <section id="logging">
233         <title>Conditional Logging</title>
234
235         <p>Environment variables can be logged in the access log using
236         the <directive module="mod_log_config">LogFormat</directive>
237         option <code>%e</code>. In addition, the decision on whether
238         or not to log requests can be made based on the status of
239         environment variables using the conditional form of the
240         <directive module="mod_log_config">CustomLog</directive>
241         directive. In combination with <directive module="mod_setenvif"
242         >SetEnvIf</directive> this allows for flexible control of which
243         requests are logged. For example, you can choose not to log
244         requests for filenames ending in <code>gif</code>, or you can
245         choose to only log requests from clients which are outside your
246         subnet.</p>
247
248     </section>
249     <section id="response-headers">
250         <title>Conditional Response Headers</title>
251
252         <p>The <directive module="mod_headers">Header</directive>
253         directive can use the presence or
254         absence of an environment variable to determine whether or not
255         a certain HTTP header will be placed in the response to the
256         client. This allows, for example, a certain response header to
257         be sent only if a corresponding header is received in the
258         request from the client.</p>
259
260     </section>
261
262     <section id="external-filter">
263         <title>External Filter Activation</title>
264
265         <p>External filters configured by <module>mod_ext_filter</module>
266         using the <directive
267         module="mod_ext_filter">ExtFilterDefine</directive> directive can
268         by activated conditional on an environment variable using the
269         <code>disableenv=</code> and <code>enableenv=</code> options.</p>
270     </section>
271
272     <section id="url-rewriting">
273         <title>URL Rewriting</title>
274
275         <p>The <code>%{ENV:<em>variable</em>}</code> form of
276         <em>TestString</em> in the <directive module="mod_rewrite"
277         >RewriteCond</directive> allows <module>mod_rewrite</module>'s rewrite
278         engine to make decisions conditional on environment variables.
279         Note that the variables accessible in <module>mod_rewrite</module>
280         without the <code>ENV:</code> prefix are not actually environment
281         variables. Rather, they are variables special to
282         <module>mod_rewrite</module> which cannot be accessed from other
283         modules.</p>
284     </section>
285   </section>
286
287   <section id="special">
288     <title>Special Purpose Environment Variables</title>
289
290         <p>Interoperability problems have led to the introduction of
291         mechanisms to modify the way Apache behaves when talking to
292         particular clients. To make these mechanisms as flexible as
293         possible, they are invoked by defining environment variables,
294         typically with <directive module="mod_setenvif"
295         >BrowserMatch</directive>, though <directive module="mod_env"
296         >SetEnv</directive> and  <directive module="mod_env"
297         >PassEnv</directive> could also be used, for example.</p>
298
299     <section id="downgrade">
300         <title>downgrade-1.0</title>
301
302         <p>This forces the request to be treated as a HTTP/1.0 request
303         even if it was in a later dialect.</p>
304
305     </section>
306     <section id="force-gzip">
307         <title>force-gzip</title>
308           <p>If you have the <code>DEFLATE</code> filter activated, this
309           environment variable will ignore the accept-encoding setting of
310           your browser and will send compressed output unconditionally.</p>
311     </section>
312     <section id="force-no-vary">
313         <title>force-no-vary</title>
314
315         <p>This causes any <code>Vary</code> fields to be removed from
316         the response header before it is sent back to the client. Some
317         clients don't interpret this field correctly; setting this
318         variable can work around this problem. Setting this variable
319         also implies <strong>force-response-1.0</strong>.</p>
320
321     </section>
322     <section id="force-response">
323         <title>force-response-1.0</title>
324
325       <p>This forces an HTTP/1.0 response to clients making an HTTP/1.0
326       request. It was originally
327       implemented as a result of a problem with AOL's proxies. Some
328       HTTP/1.0 clients may not behave correctly when given an HTTP/1.1
329       response, and this can be used to interoperate with them.</p>
330
331     </section>
332
333     <section id="gzip-only-text-html">
334         <title>gzip-only-text/html</title>
335
336         <p>When set to a value of "1", this variable disables the
337         <code>DEFLATE</code> output filter provided by
338         <module>mod_deflate</module> for content-types other than
339         <code>text/html</code>. If you'd rather
340         use statically compressed files, <module>mod_negotiation</module>
341         evaluates the variable as well (not only for gzip, but for all
342         encodings that differ from "identity").</p>
343     </section>
344
345     <section id="no-gzip"><title>no-gzip</title>
346
347         <p>When set, the <code>DEFLATE</code> filter of
348         <module>mod_deflate</module> will be turned off and
349         <module>mod_negotiation</module> will refuse to deliver encoded
350         resources.</p>
351
352     </section>
353
354     <section id="no-cache"><title>no-cache</title>
355         <p><em>Available in versions 2.2.12 and later</em></p>
356
357         <p>When set, <module>mod_cache</module> will not save an otherwise
358         cacheable response.  This environment variable does not influence
359         whether a response already in the cache will be served for the current
360         request.</p>
361
362     </section>
363
364     <section id="nokeepalive">
365         <title>nokeepalive</title>
366
367         <p>This disables <directive module="core">KeepAlive</directive>
368         when set.</p>
369
370     </section>
371
372     <section id="prefer-language"><title>prefer-language</title>
373
374         <p>This influences <module>mod_negotiation</module>'s behaviour. If
375         it contains a language tag (such as <code>en</code>, <code>ja</code>
376         or <code>x-klingon</code>), <module>mod_negotiation</module> tries
377         to deliver a variant with that language. If there's no such variant,
378         the normal <a href="content-negotiation.html">negotiation</a> process
379         applies.</p>
380
381     </section>
382
383     <section id="redirect-carefully">
384         <title>redirect-carefully</title>
385
386         <p>This forces the server to be more careful when sending a redirect
387         to the client.  This is typically used when a client has a known
388         problem handling redirects.  This was originally implemented as a
389         result of a problem with Microsoft's WebFolders software which has
390         a problem handling redirects on directory resources via DAV
391         methods.</p>
392
393     </section>
394
395    <section id="suppress-error-charset">
396        <title>suppress-error-charset</title>
397
398     <p><em>Available in versions after 2.0.54</em></p>
399
400     <p>When Apache issues a redirect in response to a client request,
401     the response includes some actual text to be displayed in case
402     the client can't (or doesn't) automatically follow the redirection.
403     Apache ordinarily labels this text according to the character set
404     which it uses, which is ISO-8859-1.</p>
405
406     <p> However, if the redirection is to a page that uses a different
407     character set, some broken browser versions will try to use the
408     character set from the redirection text rather than the actual page.
409     This can result in Greek, for instance, being incorrectly rendered.</p>
410
411     <p>Setting this environment variable causes Apache to omit the character
412     set for the redirection text, and these broken browsers will then correctly
413     use that of the destination page.</p>
414
415     <note type="warning">
416       <title>Security note</title>
417
418       <p>Sending error pages without a specified character set may
419       allow a cross-site-scripting attack for existing browsers (MSIE)
420       which do not follow the HTTP/1.1 specification and attempt to
421       "guess" the character set from the content.  Such browsers can
422       be easily fooled into using the UTF-7 character set, and UTF-7
423       content from input data (such as the request-URI) will not be
424       escaped by the usual escaping mechanisms designed to prevent
425       cross-site-scripting attacks.</p>
426     </note>
427
428    </section>
429
430    <section id="proxy"><title>force-proxy-request-1.0, proxy-nokeepalive, proxy-sendchunked,
431    proxy-sendcl, proxy-chain-auth, proxy-interim-response, proxy-initial-not-pooled</title>
432
433    <p>These directives alter the protocol behavior of
434    <module>mod_proxy</module>.  See the <module>mod_proxy</module> and <module>mod_proxy_http</module>
435    documentation for more details.</p>
436    </section>
437
438   </section>
439
440   <section id="examples">
441     <title>Examples</title>
442
443     <section id="fixheader">
444       <title>Passing broken headers to CGI scripts</title>
445
446       <p>Starting with version 2.4, Apache is more strict about how HTTP
447       headers are converted to environment variables in <module>mod_cgi
448       </module> and other modules:  Previously any invalid characters
449       in header names were simply translated to underscores.  This allowed
450       for some potential cross-site-scripting attacks via header injection
451       (see <a href="http://events.ccc.de/congress/2007/Fahrplan/events/2212.en.html">
452       Unusual Web Bugs</a>, slide 19/20).</p>
453
454       <p>If you have to support a client which sends broken headers and
455       which can't be fixed, a simple workaround involving <module>mod_setenvif
456       </module> and <module>mod_headers</module> allows you to still accept
457       these headers:</p>
458
459 <highlight language="config">
460
461 # The following works around a client sending a broken Accept_Encoding
462 # header.
463 #
464 SetEnvIfNoCase ^Accept.Encoding$ ^(.*)$ fix_accept_encoding=$1
465 RequestHeader set Accept-Encoding %{fix_accept_encoding}e env=fix_accept_encoding
466 </highlight>
467
468     </section>
469
470     <section id="misbehaving">
471         <title>Changing protocol behavior with misbehaving clients</title>
472
473         <p>Earlier versions recommended that the following lines be included in
474         httpd.conf to deal with known client problems.  Since the affected clients
475         are no longer seen in the wild, this configuration is likely no-longer
476         necessary.</p>
477 <highlight language="config">
478 #
479 # The following directives modify normal HTTP response behavior.
480 # The first directive disables keepalive for Netscape 2.x and browsers that
481 # spoof it. There are known problems with these browser implementations.
482 # The second directive is for Microsoft Internet Explorer 4.0b2
483 # which has a broken HTTP/1.1 implementation and does not properly
484 # support keepalive when it is used on 301 or 302 (redirect) responses.
485 #
486 BrowserMatch "Mozilla/2" nokeepalive
487 BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
488
489 #
490 # The following directive disables HTTP/1.1 responses to browsers which
491 # are in violation of the HTTP/1.0 spec by not being able to understand a
492 # basic 1.1 response.
493 #
494 BrowserMatch "RealPlayer 4\.0" force-response-1.0
495 BrowserMatch "Java/1\.0" force-response-1.0
496 BrowserMatch "JDK/1\.0" force-response-1.0
497 </highlight>
498
499     </section>
500     <section id="no-img-log">
501         <title>Do not log requests for images in the access log</title>
502
503         <p>This example keeps requests for images from appearing in the
504         access log. It can be easily modified to prevent logging of
505         particular directories, or to prevent logging of requests
506         coming from particular hosts.</p>
507
508         <highlight language="config">
509 SetEnvIf Request_URI \.gif image-request
510 SetEnvIf Request_URI \.jpg image-request
511 SetEnvIf Request_URI \.png image-request
512 CustomLog logs/access_log common env=!image-request
513         </highlight>
514
515     </section>
516     <section id="image-theft">
517         <title>Prevent "Image Theft"</title>
518
519         <p>This example shows how to keep people not on your server
520         from using images on your server as inline-images on their
521         pages. This is not a recommended configuration, but it can work
522         in limited circumstances. We assume that all your images are in
523         a directory called <code>/web/images</code>.</p>
524
525         <highlight language="config">
526 SetEnvIf Referer "^http://www\.example\.com/" local_referal
527 # Allow browsers that do not send Referer info
528 SetEnvIf Referer "^$" local_referal
529 &lt;Directory /web/images&gt;
530     Require env local_referal
531 &lt;/Directory&gt;
532         </highlight>
533
534         <p>For more information about this technique, see the
535         "<a href="http://www.serverwatch.com/tutorials/article.php/1132731"
536         >Keeping Your Images from Adorning Other Sites</a>"
537         tutorial on ServerWatch.</p>
538     </section>
539   </section>
540 </manualpage>