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_env</module>
63 <module>mod_rewrite</module>
64 <module>mod_setenvif</module>
65 <module>mod_unique_id</module>
68 <directive module="mod_setenvif">BrowserMatch</directive>
69 <directive module="mod_setenvif">BrowserMatchNoCase</directive>
70 <directive module="mod_env">PassEnv</directive>
71 <directive module="mod_rewrite">RewriteRule</directive>
72 <directive module="mod_env">SetEnv</directive>
73 <directive module="mod_setenvif">SetEnvIf</directive>
74 <directive module="mod_setenvif">SetEnvIfNoCase</directive>
75 <directive module="mod_env">UnsetEnv</directive>
79 <section id="basic-manipulation">
80 <title>Basic Environment Manipulation</title>
82 <p>The most basic way to set an environment variable in Apache
83 is using the unconditional <directive module="mod_env"
84 >SetEnv</directive> directive. Variables may also be passed from
85 the environment of the shell which started the server using the
86 <directive module="mod_env">PassEnv</directive> directive.</p>
89 <section id="conditional">
90 <title>Conditional Per-Request Settings</title>
92 <p>For additional flexibility, the directives provided by
93 <module>mod_setenvif</module> allow environment variables to be set
94 on a per-request basis, conditional on characteristics of particular
95 requests. For example, a variable could be set only when a
96 specific browser (User-Agent) is making a request, or only when
97 a specific Referer [sic] header is found. Even more flexibility
98 is available through the <module>mod_rewrite</module>'s <directive
99 module="mod_rewrite">RewriteRule</directive> which uses the
100 <code>[E=...]</code> option to set environment variables.</p>
103 <section id="unique-identifiers">
104 <title>Unique Identifiers</title>
106 <p>Finally, <module>mod_unique_id</module> sets the environment
107 variable <code>UNIQUE_ID</code> for each request to a value which is
108 guaranteed to be unique across "all" requests under very
109 specific conditions.</p>
112 <section id="standard-cgi">
113 <title>Standard CGI Variables</title>
115 <p>In addition to all environment variables set within the
116 Apache configuration and passed from the shell, CGI scripts and
117 SSI pages are provided with a set of environment variables
118 containing meta-information about the request as required by
119 the <a href="http://www.w3.org/CGI/">CGI
120 specification</a>.</p>
123 <section id="caveats">
124 <title>Some Caveats</title>
127 <li>It is not possible to override or change the standard CGI
128 variables using the environment manipulation directives.</li>
130 <li>When <program>suexec</program> is used to launch
131 CGI scripts, the environment will be cleaned down to a set of
132 <em>safe</em> variables before CGI scripts are launched. The
133 list of <em>safe</em> variables is defined at compile-time in
134 <code>suexec.c</code>.</li>
136 <li>For portability reasons, the names of environment
137 variables may contain only letters, numbers, and the
138 underscore character. In addition, the first character may
139 not be a number. Characters which do not match this
140 restriction will be replaced by an underscore when passed to
141 CGI scripts and SSI pages.</li>
143 <li>The <directive module="mod_env">SetEnv</directive> directive runs
144 late during request processing meaning that directives such as
145 <directive module="mod_setenvif">SetEnvIf</directive> and <directive
146 module="mod_rewrite">RewriteCond</directive> will not see the
147 variables set with it.</li>
152 <title>Using Environment Variables</title>
156 <module>mod_authz_host</module>
157 <module>mod_cgi</module>
158 <module>mod_ext_filter</module>
159 <module>mod_headers</module>
160 <module>mod_include</module>
161 <module>mod_log_config</module>
162 <module>mod_rewrite</module>
165 <directive module="mod_authz_host">Allow</directive>
166 <directive module="mod_log_config">CustomLog</directive>
167 <directive module="mod_authz_host">Deny</directive>
168 <directive module="mod_ext_filter">ExtFilterDefine</directive>
169 <directive module="mod_headers">Header</directive>
170 <directive module="mod_log_config">LogFormat</directive>
171 <directive module="mod_rewrite">RewriteCond</directive>
172 <directive module="mod_rewrite">RewriteRule</directive>
176 <section id="cgi-scripts">
177 <title>CGI Scripts</title>
179 <p>One of the primary uses of environment variables is to
180 communicate information to CGI scripts. As discussed above, the
181 environment passed to CGI scripts includes standard
182 meta-information about the request in addition to any variables
183 set within the Apache configuration. For more details, see the
184 <a href="howto/cgi.html">CGI tutorial</a>.</p>
187 <section id="ssi-pages">
188 <title>SSI Pages</title>
190 <p>Server-parsed (SSI) documents processed by
191 <module>mod_include</module>'s
192 <code>INCLUDES</code> filter can print environment variables
193 using the <code>echo</code> element, and can use environment
194 variables in flow control elements to makes parts of a page
195 conditional on characteristics of a request. Apache also
196 provides SSI pages with the standard CGI environment variables
197 as discussed above. For more details, see the <a
198 href="howto/ssi.html">SSI tutorial</a>.</p>
201 <section id="access-control">
202 <title>Access Control</title>
204 <p>Access to the server can be controlled based on the value of
205 environment variables using the <code>allow from env=</code>
206 and <code>deny from env=</code> directives. In combination with
207 <directive module="mod_setenvif">SetEnvIf</directive>, this
208 allows for flexible control of access to the server based on
209 characteristics of the client. For example, you can use these
210 directives to deny access to a particular browser (User-Agent).
214 <section id="logging">
215 <title>Conditional Logging</title>
217 <p>Environment variables can be logged in the access log using
218 the <directive module="mod_log_config">LogFormat</directive>
219 option <code>%e</code>. In addition, the decision on whether
220 or not to log requests can be made based on the status of
221 environment variables using the conditional form of the
222 <directive module="mod_log_config">CustomLog</directive>
223 directive. In combination with <directive module="mod_setenvif"
224 >SetEnvIf</directive> this allows for flexible control of which
225 requests are logged. For example, you can choose not to log
226 requests for filenames ending in <code>gif</code>, or you can
227 choose to only log requests from clients which are outside your
231 <section id="response-headers">
232 <title>Conditional Response Headers</title>
234 <p>The <directive module="mod_headers">Header</directive>
235 directive can use the presence or
236 absence of an environment variable to determine whether or not
237 a certain HTTP header will be placed in the response to the
238 client. This allows, for example, a certain response header to
239 be sent only if a corresponding header is received in the
240 request from the client.</p>
244 <section id="external-filter">
245 <title>External Filter Activation</title>
247 <p>External filters configured by <module>mod_ext_filter</module>
249 module="mod_ext_filter">ExtFilterDefine</directive> directive can
250 by activated conditional on an environment variable using the
251 <code>disableenv=</code> and <code>enableenv=</code> options.</p>
254 <section id="url-rewriting">
255 <title>URL Rewriting</title>
257 <p>The <code>%{ENV:<em>variable</em>}</code> form of
258 <em>TestString</em> in the <directive module="mod_rewrite"
259 >RewriteCond</directive> allows <module>mod_rewrite</module>'s rewrite
260 engine to make decisions conditional on environment variables.
261 Note that the variables accessible in <module>mod_rewrite</module>
262 without the <code>ENV:</code> prefix are not actually environment
263 variables. Rather, they are variables special to
264 <module>mod_rewrite</module> which cannot be accessed from other
269 <section id="special">
270 <title>Special Purpose Environment Variables</title>
272 <p>Interoperability problems have led to the introduction of
273 mechanisms to modify the way Apache behaves when talking to
274 particular clients. To make these mechanisms as flexible as
275 possible, they are invoked by defining environment variables,
276 typically with <directive module="mod_setenvif"
277 >BrowserMatch</directive>, though <directive module="mod_env"
278 >SetEnv</directive> and <directive module="mod_env"
279 >PassEnv</directive> could also be used, for example.</p>
281 <section id="downgrade">
282 <title>downgrade-1.0</title>
284 <p>This forces the request to be treated as a HTTP/1.0 request
285 even if it was in a later dialect.</p>
288 <section id="force-gzip">
289 <title>force-gzip</title>
290 <p>If you have the <code>DEFLATE</code> filter activated, this
291 environment variable will ignore the accept-encoding setting of
292 your browser and will send compressed output unconditionally.</p>
294 <section id="force-no-vary">
295 <title>force-no-vary</title>
297 <p>This causes any <code>Vary</code> fields to be removed from
298 the response header before it is sent back to the client. Some
299 clients don't interpret this field correctly; setting this
300 variable can work around this problem. Setting this variable
301 also implies <strong>force-response-1.0</strong>.</p>
304 <section id="force-response">
305 <title>force-response-1.0</title>
307 <p>This forces an HTTP/1.0 response to clients making an HTTP/1.0
308 request. It was originally
309 implemented as a result of a problem with AOL's proxies. Some
310 HTTP/1.0 clients may not behave correctly when given an HTTP/1.1
311 response, and this can be used to interoperate with them.</p>
315 <section id="gzip-only-text-html">
316 <title>gzip-only-text/html</title>
318 <p>When set to a value of "1", this variable disables the
319 <code>DEFLATE</code> output filter provided by
320 <module>mod_deflate</module> for content-types other than
321 <code>text/html</code>. If you'd rather
322 use statically compressed files, <module>mod_negotiation</module>
323 evaluates the variable as well (not only for gzip, but for all
324 encodings that differ from "identity").</p>
327 <section id="no-gzip"><title>no-gzip</title>
329 <p>When set, the <code>DEFLATE</code> filter of
330 <module>mod_deflate</module> will be turned off and
331 <module>mod_negotiation</module> will refuse to deliver encoded
336 <section id="no-cache"><title>no-cache</title>
337 <p><em>Available in versions 2.2.12 and later</em></p>
339 <p>When set, <module>mod_cache</module> will not save an otherwise
340 cacheable response. This environment variable does not influence
341 whether a response already in the cache will be served for the current
346 <section id="nokeepalive">
347 <title>nokeepalive</title>
349 <p>This disables <directive module="core">KeepAlive</directive>
354 <section id="prefer-language"><title>prefer-language</title>
356 <p>This influences <module>mod_negotiation</module>'s behaviour. If
357 it contains a language tag (such as <code>en</code>, <code>ja</code>
358 or <code>x-klingon</code>), <module>mod_negotiation</module> tries
359 to deliver a variant with that language. If there's no such variant,
360 the normal <a href="content-negotiation.html">negotiation</a> process
365 <section id="redirect-carefully">
366 <title>redirect-carefully</title>
368 <p>This forces the server to be more careful when sending a redirect
369 to the client. This is typically used when a client has a known
370 problem handling redirects. This was originally implemented as a
371 result of a problem with Microsoft's WebFolders software which has
372 a problem handling redirects on directory resources via DAV
377 <section id="suppress-error-charset">
378 <title>suppress-error-charset</title>
380 <p><em>Available in versions after 2.0.54</em></p>
382 <p>When Apache issues a redirect in response to a client request,
383 the response includes some actual text to be displayed in case
384 the client can't (or doesn't) automatically follow the redirection.
385 Apache ordinarily labels this text according to the character set
386 which it uses, which is ISO-8859-1.</p>
388 <p> However, if the redirection is to a page that uses a different
389 character set, some broken browser versions will try to use the
390 character set from the redirection text rather than the actual page.
391 This can result in Greek, for instance, being incorrectly rendered.</p>
393 <p>Setting this environment variable causes Apache to omit the character
394 set for the redirection text, and these broken browsers will then correctly
395 use that of the destination page.</p>
397 <note type="warning">
398 <title>Security note</title>
400 <p>Sending error pages without a specified character set may
401 allow a cross-site-scripting attack for existing browsers (MSIE)
402 which do not follow the HTTP/1.1 specification and attempt to
403 "guess" the character set from the content. Such browsers can
404 be easily fooled into using the UTF-7 character set, and UTF-7
405 content from input data (such as the request-URI) will not be
406 escaped by the usual escaping mechanisms designed to prevent
407 cross-site-scripting attacks.</p>
412 <section id="proxy"><title>force-proxy-request-1.0, proxy-nokeepalive, proxy-sendchunked,
413 proxy-sendcl, proxy-chain-auth, proxy-interim-response, proxy-initial-not-pooled</title>
415 <p>These directives alter the protocol behavior of
416 <module>mod_proxy</module>. See the <module>mod_proxy</module> and <module>mod_proxy_http</module>
417 documentation for more details.</p>
422 <section id="examples">
423 <title>Examples</title>
425 <section id="misbehaving">
426 <title>Changing protocol behavior with misbehaving clients</title>
428 <p>Earlier versions recommended that the following lines be included in
429 httpd.conf to deal with known client problems. Since the affected clients
430 are no longer seen in the wild, this configuration is likely no-longer
434 # The following directives modify normal HTTP response behavior.
435 # The first directive disables keepalive for Netscape 2.x and browsers that
436 # spoof it. There are known problems with these browser implementations.
437 # The second directive is for Microsoft Internet Explorer 4.0b2
438 # which has a broken HTTP/1.1 implementation and does not properly
439 # support keepalive when it is used on 301 or 302 (redirect) responses.
441 BrowserMatch "Mozilla/2" nokeepalive
442 BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
445 # The following directive disables HTTP/1.1 responses to browsers which
446 # are in violation of the HTTP/1.0 spec by not being able to grok a
447 # basic 1.1 response.
449 BrowserMatch "RealPlayer 4\.0" force-response-1.0
450 BrowserMatch "Java/1\.0" force-response-1.0
451 BrowserMatch "JDK/1\.0" force-response-1.0</pre></example>
454 <section id="no-img-log">
455 <title>Do not log requests for images in the access log</title>
457 <p>This example keeps requests for images from appearing in the
458 access log. It can be easily modified to prevent logging of
459 particular directories, or to prevent logging of requests
460 coming from particular hosts.</p>
463 SetEnvIf Request_URI \.gif image-request<br />
464 SetEnvIf Request_URI \.jpg image-request<br />
465 SetEnvIf Request_URI \.png image-request<br />
466 CustomLog logs/access_log common env=!image-request
470 <section id="image-theft">
471 <title>Prevent "Image Theft"</title>
473 <p>This example shows how to keep people not on your server
474 from using images on your server as inline-images on their
475 pages. This is not a recommended configuration, but it can work
476 in limited circumstances. We assume that all your images are in
477 a directory called <code>/web/images</code>.</p>
480 SetEnvIf Referer "^http://www\.example\.com/" local_referal<br />
481 # Allow browsers that do not send Referer info<br />
482 SetEnvIf Referer "^$" local_referal<br />
483 <Directory /web/images><br />
485 Order Deny,Allow<br />
487 Allow from env=local_referal
492 <p>For more information about this technique, see the
493 "<a href="http://www.serverwatch.com/tutorials/article.php/1132731"
494 >Keeping Your Images from Adorning Other Sites</a>"
495 tutorial on ServerWatch.</p>