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$ -->
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
14 http://www.apache.org/licenses/LICENSE-2.0
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.
23 <manualpage metafile="env.xml.meta">
25 <title>Environment Variables in Apache</title>
28 <p>There are two kinds of environment variables that affect
29 the Apache HTTP Server.</p>
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>
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>
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
58 <section id="setting">
59 <title>Setting Environment Variables</title>
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>
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>
80 <section id="basic-manipulation">
81 <title>Basic Environment Manipulation</title>
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>
90 <section id="conditional">
91 <title>Conditional Per-Request Settings</title>
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>
104 <section id="unique-identifiers">
105 <title>Unique Identifiers</title>
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>
113 <section id="standard-cgi">
114 <title>Standard CGI Variables</title>
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>
124 <section id="caveats">
125 <title>Some Caveats</title>
128 <li>It is not possible to override or change the standard CGI
129 variables using the environment manipulation directives.</li>
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>
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>
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>
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>
160 <title>Using Environment Variables</title>
164 <module>mod_authz_host</module>
165 <module>mod_cgi</module>
166 <module>mod_ext_filter</module>
167 <module>mod_headers</module>
168 <module>mod_include</module>
169 <module>mod_log_config</module>
170 <module>mod_rewrite</module>
173 <directive module="mod_access_compat">Allow</directive>
174 <directive module="mod_log_config">CustomLog</directive>
175 <directive module="mod_access_compat">Deny</directive>
176 <directive module="mod_ext_filter">ExtFilterDefine</directive>
177 <directive module="mod_headers">Header</directive>
178 <directive module="mod_log_config">LogFormat</directive>
179 <directive module="mod_rewrite">RewriteCond</directive>
180 <directive module="mod_rewrite">RewriteRule</directive>
184 <section id="cgi-scripts">
185 <title>CGI Scripts</title>
187 <p>One of the primary uses of environment variables is to
188 communicate information to CGI scripts. As discussed above, the
189 environment passed to CGI scripts includes standard
190 meta-information about the request in addition to any variables
191 set within the Apache configuration. For more details, see the
192 <a href="howto/cgi.html">CGI tutorial</a>.</p>
195 <section id="ssi-pages">
196 <title>SSI Pages</title>
198 <p>Server-parsed (SSI) documents processed by
199 <module>mod_include</module>'s
200 <code>INCLUDES</code> filter can print environment variables
201 using the <code>echo</code> element, and can use environment
202 variables in flow control elements to makes parts of a page
203 conditional on characteristics of a request. Apache also
204 provides SSI pages with the standard CGI environment variables
205 as discussed above. For more details, see the <a
206 href="howto/ssi.html">SSI tutorial</a>.</p>
209 <section id="access-control">
210 <title>Access Control</title>
212 <p>Access to the server can be controlled based on the value of
213 environment variables using the <code>allow from env=</code>
214 and <code>deny from env=</code> directives. In combination with
215 <directive module="mod_setenvif">SetEnvIf</directive>, this
216 allows for flexible control of access to the server based on
217 characteristics of the client. For example, you can use these
218 directives to deny access to a particular browser (User-Agent).
222 <section id="logging">
223 <title>Conditional Logging</title>
225 <p>Environment variables can be logged in the access log using
226 the <directive module="mod_log_config">LogFormat</directive>
227 option <code>%e</code>. In addition, the decision on whether
228 or not to log requests can be made based on the status of
229 environment variables using the conditional form of the
230 <directive module="mod_log_config">CustomLog</directive>
231 directive. In combination with <directive module="mod_setenvif"
232 >SetEnvIf</directive> this allows for flexible control of which
233 requests are logged. For example, you can choose not to log
234 requests for filenames ending in <code>gif</code>, or you can
235 choose to only log requests from clients which are outside your
239 <section id="response-headers">
240 <title>Conditional Response Headers</title>
242 <p>The <directive module="mod_headers">Header</directive>
243 directive can use the presence or
244 absence of an environment variable to determine whether or not
245 a certain HTTP header will be placed in the response to the
246 client. This allows, for example, a certain response header to
247 be sent only if a corresponding header is received in the
248 request from the client.</p>
252 <section id="external-filter">
253 <title>External Filter Activation</title>
255 <p>External filters configured by <module>mod_ext_filter</module>
257 module="mod_ext_filter">ExtFilterDefine</directive> directive can
258 by activated conditional on an environment variable using the
259 <code>disableenv=</code> and <code>enableenv=</code> options.</p>
262 <section id="url-rewriting">
263 <title>URL Rewriting</title>
265 <p>The <code>%{ENV:<em>variable</em>}</code> form of
266 <em>TestString</em> in the <directive module="mod_rewrite"
267 >RewriteCond</directive> allows <module>mod_rewrite</module>'s rewrite
268 engine to make decisions conditional on environment variables.
269 Note that the variables accessible in <module>mod_rewrite</module>
270 without the <code>ENV:</code> prefix are not actually environment
271 variables. Rather, they are variables special to
272 <module>mod_rewrite</module> which cannot be accessed from other
277 <section id="special">
278 <title>Special Purpose Environment Variables</title>
280 <p>Interoperability problems have led to the introduction of
281 mechanisms to modify the way Apache behaves when talking to
282 particular clients. To make these mechanisms as flexible as
283 possible, they are invoked by defining environment variables,
284 typically with <directive module="mod_setenvif"
285 >BrowserMatch</directive>, though <directive module="mod_env"
286 >SetEnv</directive> and <directive module="mod_env"
287 >PassEnv</directive> could also be used, for example.</p>
289 <section id="downgrade">
290 <title>downgrade-1.0</title>
292 <p>This forces the request to be treated as a HTTP/1.0 request
293 even if it was in a later dialect.</p>
296 <section id="force-gzip">
297 <title>force-gzip</title>
298 <p>If you have the <code>DEFLATE</code> filter activated, this
299 environment variable will ignore the accept-encoding setting of
300 your browser and will send compressed output unconditionally.</p>
302 <section id="force-no-vary">
303 <title>force-no-vary</title>
305 <p>This causes any <code>Vary</code> fields to be removed from
306 the response header before it is sent back to the client. Some
307 clients don't interpret this field correctly; setting this
308 variable can work around this problem. Setting this variable
309 also implies <strong>force-response-1.0</strong>.</p>
312 <section id="force-response">
313 <title>force-response-1.0</title>
315 <p>This forces an HTTP/1.0 response to clients making an HTTP/1.0
316 request. It was originally
317 implemented as a result of a problem with AOL's proxies. Some
318 HTTP/1.0 clients may not behave correctly when given an HTTP/1.1
319 response, and this can be used to interoperate with them.</p>
323 <section id="gzip-only-text-html">
324 <title>gzip-only-text/html</title>
326 <p>When set to a value of "1", this variable disables the
327 <code>DEFLATE</code> output filter provided by
328 <module>mod_deflate</module> for content-types other than
329 <code>text/html</code>. If you'd rather
330 use statically compressed files, <module>mod_negotiation</module>
331 evaluates the variable as well (not only for gzip, but for all
332 encodings that differ from "identity").</p>
335 <section id="no-gzip"><title>no-gzip</title>
337 <p>When set, the <code>DEFLATE</code> filter of
338 <module>mod_deflate</module> will be turned off and
339 <module>mod_negotiation</module> will refuse to deliver encoded
344 <section id="no-cache"><title>no-cache</title>
345 <p><em>Available in versions 2.2.12 and later</em></p>
347 <p>When set, <module>mod_cache</module> will not save an otherwise
348 cacheable response. This environment variable does not influence
349 whether a response already in the cache will be served for the current
354 <section id="nokeepalive">
355 <title>nokeepalive</title>
357 <p>This disables <directive module="core">KeepAlive</directive>
362 <section id="prefer-language"><title>prefer-language</title>
364 <p>This influences <module>mod_negotiation</module>'s behaviour. If
365 it contains a language tag (such as <code>en</code>, <code>ja</code>
366 or <code>x-klingon</code>), <module>mod_negotiation</module> tries
367 to deliver a variant with that language. If there's no such variant,
368 the normal <a href="content-negotiation.html">negotiation</a> process
373 <section id="redirect-carefully">
374 <title>redirect-carefully</title>
376 <p>This forces the server to be more careful when sending a redirect
377 to the client. This is typically used when a client has a known
378 problem handling redirects. This was originally implemented as a
379 result of a problem with Microsoft's WebFolders software which has
380 a problem handling redirects on directory resources via DAV
385 <section id="suppress-error-charset">
386 <title>suppress-error-charset</title>
388 <p><em>Available in versions after 2.0.54</em></p>
390 <p>When Apache issues a redirect in response to a client request,
391 the response includes some actual text to be displayed in case
392 the client can't (or doesn't) automatically follow the redirection.
393 Apache ordinarily labels this text according to the character set
394 which it uses, which is ISO-8859-1.</p>
396 <p> However, if the redirection is to a page that uses a different
397 character set, some broken browser versions will try to use the
398 character set from the redirection text rather than the actual page.
399 This can result in Greek, for instance, being incorrectly rendered.</p>
401 <p>Setting this environment variable causes Apache to omit the character
402 set for the redirection text, and these broken browsers will then correctly
403 use that of the destination page.</p>
405 <note type="warning">
406 <title>Security note</title>
408 <p>Sending error pages without a specified character set may
409 allow a cross-site-scripting attack for existing browsers (MSIE)
410 which do not follow the HTTP/1.1 specification and attempt to
411 "guess" the character set from the content. Such browsers can
412 be easily fooled into using the UTF-7 character set, and UTF-7
413 content from input data (such as the request-URI) will not be
414 escaped by the usual escaping mechanisms designed to prevent
415 cross-site-scripting attacks.</p>
420 <section id="proxy"><title>force-proxy-request-1.0, proxy-nokeepalive, proxy-sendchunked,
421 proxy-sendcl, proxy-chain-auth, proxy-interim-response, proxy-initial-not-pooled</title>
423 <p>These directives alter the protocol behavior of
424 <module>mod_proxy</module>. See the <module>mod_proxy</module> and <module>mod_proxy_http</module>
425 documentation for more details.</p>
430 <section id="examples">
431 <title>Examples</title>
433 <section id="fixheader">
434 <title>Passing broken headers to CGI scripts</title>
436 <p>Starting with version 2.4, Apache is more strict about how HTTP
437 headers are converted to environment variables in <module>mod_cgi
438 </module> and other modules: Previously any invalid characters
439 in header names were simply translated to underscores. This allowed
440 for some potential cross-site-scripting attacks via header injection
441 (see <a href="http://events.ccc.de/congress/2007/Fahrplan/events/2212.en.html">
442 Unusual Web Bugs</a>, slide 19/20).</p>
444 <p>If you have to support a client which sends broken headers and
445 which can't be fixed, a simple workaround involving <module>mod_setenvif
446 </module> and <module>mod_header</module> allows you to still accept
449 <highlight language="config">
451 # The following works around a client sending a broken Accept_Encoding
454 SetEnvIfNoCase ^Accept.Encoding$ ^(.*)$ fix_accept_encoding=$1
455 RequestHeader set Accept-Encoding %{fix_accept_encoding}e env=fix_accept_encoding
460 <section id="misbehaving">
461 <title>Changing protocol behavior with misbehaving clients</title>
463 <p>Earlier versions recommended that the following lines be included in
464 httpd.conf to deal with known client problems. Since the affected clients
465 are no longer seen in the wild, this configuration is likely no-longer
467 <highlight language="config">
469 # The following directives modify normal HTTP response behavior.
470 # The first directive disables keepalive for Netscape 2.x and browsers that
471 # spoof it. There are known problems with these browser implementations.
472 # The second directive is for Microsoft Internet Explorer 4.0b2
473 # which has a broken HTTP/1.1 implementation and does not properly
474 # support keepalive when it is used on 301 or 302 (redirect) responses.
476 BrowserMatch "Mozilla/2" nokeepalive
477 BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
480 # The following directive disables HTTP/1.1 responses to browsers which
481 # are in violation of the HTTP/1.0 spec by not being able to understand a
482 # basic 1.1 response.
484 BrowserMatch "RealPlayer 4\.0" force-response-1.0
485 BrowserMatch "Java/1\.0" force-response-1.0
486 BrowserMatch "JDK/1\.0" force-response-1.0
490 <section id="no-img-log">
491 <title>Do not log requests for images in the access log</title>
493 <p>This example keeps requests for images from appearing in the
494 access log. It can be easily modified to prevent logging of
495 particular directories, or to prevent logging of requests
496 coming from particular hosts.</p>
498 <highlight language="config">
499 SetEnvIf Request_URI \.gif image-request
500 SetEnvIf Request_URI \.jpg image-request
501 SetEnvIf Request_URI \.png image-request
502 CustomLog logs/access_log common env=!image-request
506 <section id="image-theft">
507 <title>Prevent "Image Theft"</title>
509 <p>This example shows how to keep people not on your server
510 from using images on your server as inline-images on their
511 pages. This is not a recommended configuration, but it can work
512 in limited circumstances. We assume that all your images are in
513 a directory called <code>/web/images</code>.</p>
515 <highlight language="config">
516 SetEnvIf Referer "^http://www\.example\.com/" local_referal
517 # Allow browsers that do not send Referer info
518 SetEnvIf Referer "^$" local_referal
519 <Directory /web/images>
522 Allow from env=local_referal
526 <p>For more information about this technique, see the
527 "<a href="http://www.serverwatch.com/tutorials/article.php/1132731"
528 >Keeping Your Images from Adorning Other Sites</a>"
529 tutorial on ServerWatch.</p>