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>The Apache HTTP Server provides a mechanism for storing
29 information in named variables that are called <em>environment
30 variables</em>. This information can be used to control various
31 operations such as logging or access control. The variables are
32 also used as a mechanism to communicate with external programs
33 such as CGI scripts. This document discusses different ways to
34 manipulate and use these variables.</p>
36 <p>Although these variables are referred to as <em>environment
37 variables</em>, they are not the same as the environment
38 variables controlled by the underlying operating system.
39 Instead, these variables are stored and manipulated in an
40 internal Apache structure. They only become actual operating
41 system environment variables when they are provided to CGI
42 scripts and Server Side Include scripts. If you wish to
43 manipulate the operating system environment under which the
44 server itself runs, you must use the standard environment
45 manipulation mechanisms provided by your operating system
49 <section id="setting">
50 <title>Setting Environment Variables</title>
53 <module>mod_env</module>
54 <module>mod_rewrite</module>
55 <module>mod_setenvif</module>
56 <module>mod_unique_id</module>
59 <directive module="mod_setenvif">BrowserMatch</directive>
60 <directive module="mod_setenvif">BrowserMatchNoCase</directive>
61 <directive module="mod_env">PassEnv</directive>
62 <directive module="mod_rewrite">RewriteRule</directive>
63 <directive module="mod_env">SetEnv</directive>
64 <directive module="mod_setenvif">SetEnvIf</directive>
65 <directive module="mod_setenvif">SetEnvIfNoCase</directive>
66 <directive module="mod_env">UnsetEnv</directive>
70 <section id="basic-manipulation">
71 <title>Basic Environment Manipulation</title>
73 <p>The most basic way to set an environment variable in Apache
74 is using the unconditional <directive module="mod_env"
75 >SetEnv</directive> directive. Variables may also be passed from
76 the environment of the shell which started the server using the
77 <directive module="mod_env">PassEnv</directive> directive.</p>
80 <section id="conditional">
81 <title>Conditional Per-Request Settings</title>
83 <p>For additional flexibility, the directives provided by
84 <module>mod_setenvif</module> allow environment variables to be set
85 on a per-request basis, conditional on characteristics of particular
86 requests. For example, a variable could be set only when a
87 specific browser (User-Agent) is making a request, or only when
88 a specific Referer [sic] header is found. Even more flexibility
89 is available through the <module>mod_rewrite</module>'s <directive
90 module="mod_rewrite">RewriteRule</directive> which uses the
91 <code>[E=...]</code> option to set environment variables.</p>
94 <section id="unique-identifiers">
95 <title>Unique Identifiers</title>
97 <p>Finally, <module>mod_unique_id</module> sets the environment
98 variable <code>UNIQUE_ID</code> for each request to a value which is
99 guaranteed to be unique across "all" requests under very
100 specific conditions.</p>
103 <section id="standard-cgi">
104 <title>Standard CGI Variables</title>
106 <p>In addition to all environment variables set within the
107 Apache configuration and passed from the shell, CGI scripts and
108 SSI pages are provided with a set of environment variables
109 containing meta-information about the request as required by
110 the <a href="http://www.w3.org/CGI/">CGI
111 specification</a>.</p>
114 <section id="caveats">
115 <title>Some Caveats</title>
118 <li>It is not possible to override or change the standard CGI
119 variables using the environment manipulation directives.</li>
121 <li>When <program>suexec</program> is used to launch
122 CGI scripts, the environment will be cleaned down to a set of
123 <em>safe</em> variables before CGI scripts are launched. The
124 list of <em>safe</em> variables is defined at compile-time in
125 <code>suexec.c</code>.</li>
127 <li>For portability reasons, the names of environment
128 variables may contain only letters, numbers, and the
129 underscore character. In addition, the first character may
130 not be a number. Characters which do not match this
131 restriction will be replaced by an underscore when passed to
132 CGI scripts and SSI pages.</li>
134 <li>The <directive module="mod_env">SetEnv</directive> directive runs
135 late during request processing meaning that directives such as
136 <directive module="mod_setenvif">SetEnvIf</directive> and <directive
137 module="mod_rewrite">RewriteCond</directive> will not see the
138 variables set with it.</li>
143 <title>Using Environment Variables</title>
147 <module>mod_authz_host</module>
148 <module>mod_cgi</module>
149 <module>mod_ext_filter</module>
150 <module>mod_headers</module>
151 <module>mod_include</module>
152 <module>mod_log_config</module>
153 <module>mod_rewrite</module>
156 <directive module="mod_authz_host">Allow</directive>
157 <directive module="mod_log_config">CustomLog</directive>
158 <directive module="mod_authz_host">Deny</directive>
159 <directive module="mod_ext_filter">ExtFilterDefine</directive>
160 <directive module="mod_headers">Header</directive>
161 <directive module="mod_log_config">LogFormat</directive>
162 <directive module="mod_rewrite">RewriteCond</directive>
163 <directive module="mod_rewrite">RewriteRule</directive>
167 <section id="cgi-scripts">
168 <title>CGI Scripts</title>
170 <p>One of the primary uses of environment variables is to
171 communicate information to CGI scripts. As discussed above, the
172 environment passed to CGI scripts includes standard
173 meta-information about the request in addition to any variables
174 set within the Apache configuration. For more details, see the
175 <a href="howto/cgi.html">CGI tutorial</a>.</p>
178 <section id="ssi-pages">
179 <title>SSI Pages</title>
181 <p>Server-parsed (SSI) documents processed by
182 <module>mod_include</module>'s
183 <code>INCLUDES</code> filter can print environment variables
184 using the <code>echo</code> element, and can use environment
185 variables in flow control elements to makes parts of a page
186 conditional on characteristics of a request. Apache also
187 provides SSI pages with the standard CGI environment variables
188 as discussed above. For more details, see the <a
189 href="howto/ssi.html">SSI tutorial</a>.</p>
192 <section id="access-control">
193 <title>Access Control</title>
195 <p>Access to the server can be controlled based on the value of
196 environment variables using the <code>allow from env=</code>
197 and <code>deny from env=</code> directives. In combination with
198 <directive module="mod_setenvif">SetEnvIf</directive>, this
199 allows for flexible control of access to the server based on
200 characteristics of the client. For example, you can use these
201 directives to deny access to a particular browser (User-Agent).
205 <section id="logging">
206 <title>Conditional Logging</title>
208 <p>Environment variables can be logged in the access log using
209 the <directive module="mod_log_config">LogFormat</directive>
210 option <code>%e</code>. In addition, the decision on whether
211 or not to log requests can be made based on the status of
212 environment variables using the conditional form of the
213 <directive module="mod_log_config">CustomLog</directive>
214 directive. In combination with <directive module="mod_setenvif"
215 >SetEnvIf</directive> this allows for flexible control of which
216 requests are logged. For example, you can choose not to log
217 requests for filenames ending in <code>gif</code>, or you can
218 choose to only log requests from clients which are outside your
222 <section id="response-headers">
223 <title>Conditional Response Headers</title>
225 <p>The <directive module="mod_headers">Header</directive>
226 directive can use the presence or
227 absence of an environment variable to determine whether or not
228 a certain HTTP header will be placed in the response to the
229 client. This allows, for example, a certain response header to
230 be sent only if a corresponding header is received in the
231 request from the client.</p>
235 <section id="external-filter">
236 <title>External Filter Activation</title>
238 <p>External filters configured by <module>mod_ext_filter</module>
240 module="mod_ext_filter">ExtFilterDefine</directive> directive can
241 by activated conditional on an environment variable using the
242 <code>disableenv=</code> and <code>enableenv=</code> options.</p>
245 <section id="url-rewriting">
246 <title>URL Rewriting</title>
248 <p>The <code>%{ENV:<em>variable</em>}</code> form of
249 <em>TestString</em> in the <directive module="mod_rewrite"
250 >RewriteCond</directive> allows <module>mod_rewrite</module>'s rewrite
251 engine to make decisions conditional on environment variables.
252 Note that the variables accessible in <module>mod_rewrite</module>
253 without the <code>ENV:</code> prefix are not actually environment
254 variables. Rather, they are variables special to
255 <module>mod_rewrite</module> which cannot be accessed from other
260 <section id="special">
261 <title>Special Purpose Environment Variables</title>
263 <p>Interoperability problems have led to the introduction of
264 mechanisms to modify the way Apache behaves when talking to
265 particular clients. To make these mechanisms as flexible as
266 possible, they are invoked by defining environment variables,
267 typically with <directive module="mod_setenvif"
268 >BrowserMatch</directive>, though <directive module="mod_env"
269 >SetEnv</directive> and <directive module="mod_env"
270 >PassEnv</directive> could also be used, for example.</p>
272 <section id="downgrade">
273 <title>downgrade-1.0</title>
275 <p>This forces the request to be treated as a HTTP/1.0 request
276 even if it was in a later dialect.</p>
279 <section id="force-gzip">
280 <title>force-gzip</title>
281 <p>If you have the <code>DEFLATE</code> filter activated, this
282 environment variable will ignore the accept-encoding setting of
283 your browser and will send compressed output unconditionally.</p>
285 <section id="force-no-vary">
286 <title>force-no-vary</title>
288 <p>This causes any <code>Vary</code> fields to be removed from
289 the response header before it is sent back to the client. Some
290 clients don't interpret this field correctly; setting this
291 variable can work around this problem. Setting this variable
292 also implies <strong>force-response-1.0</strong>.</p>
295 <section id="force-response">
296 <title>force-response-1.0</title>
298 <p>This forces an HTTP/1.0 response to clients making an HTTP/1.0
299 request. It was originally
300 implemented as a result of a problem with AOL's proxies. Some
301 HTTP/1.0 clients may not behave correctly when given an HTTP/1.1
302 response, and this can be used to interoperate with them.</p>
306 <section id="gzip-only-text-html">
307 <title>gzip-only-text/html</title>
309 <p>When set to a value of "1", this variable disables the
310 <code>DEFLATE</code> output filter provided by
311 <module>mod_deflate</module> for content-types other than
312 <code>text/html</code>. If you'd rather
313 use statically compressed files, <module>mod_negotiation</module>
314 evaluates the variable as well (not only for gzip, but for all
315 encodings that differ from "identity").</p>
318 <section id="no-gzip"><title>no-gzip</title>
320 <p>When set, the <code>DEFLATE</code> filter of
321 <module>mod_deflate</module> will be turned off and
322 <module>mod_negotiation</module> will refuse to deliver encoded
327 <section id="no-cache"><title>no-cache</title>
328 <p><em>Available in versions after 2.2.11</em></p>
330 <p>When set, <module>mod_cache</module> will not save an otherwise
331 cacheable response. This environment variable does not influence
332 whether a response already in the cache will be served for the current
337 <section id="nokeepalive">
338 <title>nokeepalive</title>
340 <p>This disables <directive module="core">KeepAlive</directive>
345 <section id="prefer-language"><title>prefer-language</title>
347 <p>This influences <module>mod_negotiation</module>'s behaviour. If
348 it contains a language tag (such as <code>en</code>, <code>ja</code>
349 or <code>x-klingon</code>), <module>mod_negotiation</module> tries
350 to deliver a variant with that language. If there's no such variant,
351 the normal <a href="content-negotiation.html">negotiation</a> process
356 <section id="redirect-carefully">
357 <title>redirect-carefully</title>
359 <p>This forces the server to be more careful when sending a redirect
360 to the client. This is typically used when a client has a known
361 problem handling redirects. This was originally implemented as a
362 result of a problem with Microsoft's WebFolders software which has
363 a problem handling redirects on directory resources via DAV
368 <section id="suppress-error-charset">
369 <title>suppress-error-charset</title>
371 <p><em>Available in versions after 2.0.54</em></p>
373 <p>When Apache issues a redirect in response to a client request,
374 the response includes some actual text to be displayed in case
375 the client can't (or doesn't) automatically follow the redirection.
376 Apache ordinarily labels this text according to the character set
377 which it uses, which is ISO-8859-1.</p>
379 <p> However, if the redirection is to a page that uses a different
380 character set, some broken browser versions will try to use the
381 character set from the redirection text rather than the actual page.
382 This can result in Greek, for instance, being incorrectly rendered.</p>
384 <p>Setting this environment variable causes Apache to omit the character
385 set for the redirection text, and these broken browsers will then correctly
386 use that of the destination page.</p>
388 <note type="warning">
389 <title>Security note</title>
391 <p>Sending error pages without a specified character set may
392 allow a cross-site-scripting attack for existing browsers (MSIE)
393 which do not follow the HTTP/1.1 specification and attempt to
394 "guess" the character set from the content. Such browsers can
395 be easily fooled into using the UTF-7 character set, and UTF-7
396 content from input data (such as the request-URI) will not be
397 escaped by the usual escaping mechanisms designed to prevent
398 cross-site-scripting attacks.</p>
403 <section id="proxy"><title>force-proxy-request-1.0, proxy-nokeepalive, proxy-sendchunked,
404 proxy-sendcl, proxy-chain-auth, proxy-interim-response, proxy-initial-not-pooled</title>
406 <p>These directives alter the protocol behavior of
407 <module>mod_proxy</module>. See the <module>mod_proxy</module> and <module>mod_proxy_http</module>
408 documentation for more details.</p>
413 <section id="examples">
414 <title>Examples</title>
416 <section id="misbehaving">
417 <title>Changing protocol behavior with misbehaving clients</title>
419 <p>Earlier versions recommended that the following lines be included in
420 httpd.conf to deal with known client problems. Since the affected clients
421 are no longer seen in the wild, this configuration is likely no-longer
425 # The following directives modify normal HTTP response behavior.
426 # The first directive disables keepalive for Netscape 2.x and browsers that
427 # spoof it. There are known problems with these browser implementations.
428 # The second directive is for Microsoft Internet Explorer 4.0b2
429 # which has a broken HTTP/1.1 implementation and does not properly
430 # support keepalive when it is used on 301 or 302 (redirect) responses.
432 BrowserMatch "Mozilla/2" nokeepalive
433 BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
436 # The following directive disables HTTP/1.1 responses to browsers which
437 # are in violation of the HTTP/1.0 spec by not being able to grok a
438 # basic 1.1 response.
440 BrowserMatch "RealPlayer 4\.0" force-response-1.0
441 BrowserMatch "Java/1\.0" force-response-1.0
442 BrowserMatch "JDK/1\.0" force-response-1.0</pre></example>
445 <section id="no-img-log">
446 <title>Do not log requests for images in the access log</title>
448 <p>This example keeps requests for images from appearing in the
449 access log. It can be easily modified to prevent logging of
450 particular directories, or to prevent logging of requests
451 coming from particular hosts.</p>
454 SetEnvIf Request_URI \.gif image-request<br />
455 SetEnvIf Request_URI \.jpg image-request<br />
456 SetEnvIf Request_URI \.png image-request<br />
457 CustomLog logs/access_log common env=!image-request
461 <section id="image-theft">
462 <title>Prevent "Image Theft"</title>
464 <p>This example shows how to keep people not on your server
465 from using images on your server as inline-images on their
466 pages. This is not a recommended configuration, but it can work
467 in limited circumstances. We assume that all your images are in
468 a directory called <code>/web/images</code>.</p>
471 SetEnvIf Referer "^http://www\.example\.com/" local_referal
472 # Allow browsers that do not send Referer info
473 SetEnvIf Referer "^$" local_referal
474 <Directory /web/images>
476 Order Deny,Allow<br />
478 Allow from env=local_referal
483 <p>For more information about this technique, see the
484 "<a href="http://www.serverwatch.com/tutorials/article.php/1132731"
485 >Keeping Your Images from Adorning Other Sites</a>"
486 tutorial on ServerWatch.</p>