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 Copyright 2002-2005 The Apache Software Foundation or its licensors, as
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 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 mod_setenvif allow environment variables to be set on a
85 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 mod_rewrite'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, mod_unique_id sets the environment variable
98 <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://cgi-spec.golux.com/">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>
137 <title>Using Environment Variables</title>
141 <module>mod_authz_host</module>
142 <module>mod_cgi</module>
143 <module>mod_ext_filter</module>
144 <module>mod_headers</module>
145 <module>mod_include</module>
146 <module>mod_log_config</module>
147 <module>mod_rewrite</module>
150 <directive module="mod_authz_host">Allow</directive>
151 <directive module="mod_log_config">CustomLog</directive>
152 <directive module="mod_authz_host">Deny</directive>
153 <directive module="mod_ext_filter">ExtFilterDefine</directive>
154 <directive module="mod_headers">Header</directive>
155 <directive module="mod_log_config">LogFormat</directive>
156 <directive module="mod_rewrite">RewriteCond</directive>
157 <directive module="mod_rewrite">RewriteRule</directive>
161 <section id="cgi-scripts">
162 <title>CGI Scripts</title>
164 <p>One of the primary uses of environment variables is to
165 communicate information to CGI scripts. As discussed above, the
166 environment passed to CGI scripts includes standard
167 meta-information about the request in addition to any variables
168 set within the Apache configuration. For more details, see the
169 <a href="howto/cgi.html">CGI tutorial</a>.</p>
172 <section id="ssi-pages">
173 <title>SSI Pages</title>
175 <p>Server-parsed (SSI) documents processed by mod_include's
176 <code>INCLUDES</code> filter can print environment variables
177 using the <code>echo</code> element, and can use environment
178 variables in flow control elements to makes parts of a page
179 conditional on characteristics of a request. Apache also
180 provides SSI pages with the standard CGI environment variables
181 as discussed above. For more details, see the <a
182 href="howto/ssi.html">SSI tutorial</a>.</p>
185 <section id="access-control">
186 <title>Access Control</title>
188 <p>Access to the server can be controlled based on the value of
189 environment variables using the <code>allow from env=</code>
190 and <code>deny from env=</code> directives. In combination with
191 <directive module="mod_setenvif">SetEnvIf</directive>, this
192 allows for flexible control of access to the server based on
193 characteristics of the client. For example, you can use these
194 directives to deny access to a particular browser (User-Agent).
198 <section id="logging">
199 <title>Conditional Logging</title>
201 <p>Environment variables can be logged in the access log using
202 the <directive module="mod_log_config">LogFormat</directive>
203 option <code>%e</code>. In addition, the decision on whether
204 or not to log requests can be made based on the status of
205 environment variables using the conditional form of the
206 <directive module="mod_log_config">CustomLog</directive>
207 directive. In combination with <directive module="mod_setenvif"
208 >SetEnvIf</directive> this allows for flexible control of which
209 requests are logged. For example, you can choose not to log
210 requests for filenames ending in <code>gif</code>, or you can
211 choose to only log requests from clients which are outside your
215 <section id="response-headers">
216 <title>Conditional Response Headers</title>
218 <p>The <directive module="mod_headers">Header</directive>
219 directive can use the presence or
220 absence of an environment variable to determine whether or not
221 a certain HTTP header will be placed in the response to the
222 client. This allows, for example, a certain response header to
223 be sent only if a corresponding header is received in the
224 request from the client.</p>
228 <section id="external-filter">
229 <title>External Filter Activation</title>
231 <p>External filters configured by <module>mod_ext_filter</module>
233 module="mod_ext_filter">ExtFilterDefine</directive> directive can
234 by activated conditional on an environment variable using the
235 <code>disableenv=</code> and <code>enableenv=</code> options.</p>
238 <section id="url-rewriting">
239 <title>URL Rewriting</title>
241 <p>The <code>%{ENV:...}</code> form of <em>TestString</em> in
242 the <directive module="mod_rewrite">RewriteCond</directive>
243 allows mod_rewrite's rewrite
244 engine to make decisions conditional on environment variables.
245 Note that the variables accessible in mod_rewrite without the
246 <code>ENV:</code> prefix are not actually environment
247 variables. Rather, they are variables special to mod_rewrite
248 which cannot be accessed from other modules.</p>
252 <section id="special">
253 <title>Special Purpose Environment Variables</title>
255 <p>Interoperability problems have led to the introduction of
256 mechanisms to modify the way Apache behaves when talking to
257 particular clients. To make these mechanisms as flexible as
258 possible, they are invoked by defining environment variables,
259 typically with <directive module="mod_setenvif"
260 >BrowserMatch</directive>, though <directive module="mod_env"
261 >SetEnv</directive> and <directive module="mod_env"
262 >PassEnv</directive> could also be used, for example.</p>
264 <section id="downgrade">
265 <title>downgrade-1.0</title>
267 <p>This forces the request to be treated as a HTTP/1.0 request
268 even if it was in a later dialect.</p>
271 <section id="force-gzip">
272 <title>force-gzip</title>
273 <p>If you have the <code>DEFLATE</code> filter activated, this
274 environment variable will ignore the accept-encoding setting of
275 your browser and will send compressed output unconditionally.</p>
277 <section id="force-no-vary">
278 <title>force-no-vary</title>
280 <p>This causes any <code>Vary</code> fields to be removed from
281 the response header before it is sent back to the client. Some
282 clients don't interpret this field correctly; setting this
283 variable can work around this problem. Setting this variable
284 also implies <strong>force-response-1.0</strong>.</p>
287 <section id="force-response">
288 <title>force-response-1.0</title>
290 <p>This forces an HTTP/1.0 response to clients making an HTTP/1.0
291 request. It was originally
292 implemented as a result of a problem with AOL's proxies. Some
293 HTTP/1.0 clients may not behave correctly when given an HTTP/1.1
294 response, and this can be used to interoperate with them.</p>
298 <section id="gzip-only-text-html">
299 <title>gzip-only-text/html</title>
301 <p>When set to a value of "1", this variable disables the DEFLATE
302 output filter provided by <module>mod_deflate</module> for
303 content-types other than <code>text/html</code>. If you'd rather
304 use statically compressed files; <module>mod_negotiation</module>
305 evaluates the variable as well (not only for gzip, but for all
306 encodings that differ from "identity").</p>
309 <section id="no-gzip"><title>no-gzip</title>
311 <p>When set, the <code>DEFLATE</code> filter of
312 <module>mod_deflate</module> will be turned off and
313 <module>mod_negotiation</module> will refuse to deliver encoded
318 <section id="nokeepalive">
319 <title>nokeepalive</title>
321 <p>This disables <directive module="core">KeepAlive</directive>
326 <section id="prefer-language"><title>prefer-language</title>
328 <p>This influences <module>mod_negotiation</module>'s behaviour. If
329 it contains a language tag (such as <code>en</code>, <code>ja</code>
330 or <code>x-klingon</code>), <module>mod_negotiation</module> tries
331 to deliver a variant with that language. If there's no such variant,
332 the normal <a href="content-negotiation.html">negotiation</a> process
337 <section id="redirect-carefully">
338 <title>redirect-carefully</title>
340 <p>This forces the server to be more careful when sending a redirect
341 to the client. This is typically used when a client has a known
342 problem handling redirects. This was originally implemented as a
343 result of a problem with Microsoft's WebFolders software which has
344 a problem handling redirects on directory resources via DAV
349 <section id="suppress-error-charset">
350 <title>suppress-error-charset</title>
352 <p><em>Available in versions after 2.0.40</em></p>
354 <p>When Apache issues a redirect in response to a client request,
355 the response includes some actual text to be displayed in case
356 the client can't (or doesn't) automatically follow the redirection.
357 Apache ordinarily labels this text according to the character set
358 which it uses, which is ISO-8859-1.</p>
359 <p> However, if the redirection is to a page that uses a different
360 character set, some broken browser versions will try to use the
361 character set from the redirection text rather than the actual page.
362 This can result in Greek, for instance, being incorrectly rendered.</p>
363 <p>Setting this environment variable causes Apache to omit the character
364 set for the redirection text, and these broken browsers will then correctly
365 use that of the destination page.</p>
371 <section id="examples">
372 <title>Examples</title>
374 <section id="misbehaving">
375 <title>Changing protocol behavior with misbehaving clients</title>
377 <p>We recommend that the following lines be included in
378 httpd.conf to deal with known client problems.</p>
381 # The following directives modify normal HTTP response behavior.
382 # The first directive disables keepalive for Netscape 2.x and browsers that
383 # spoof it. There are known problems with these browser implementations.
384 # The second directive is for Microsoft Internet Explorer 4.0b2
385 # which has a broken HTTP/1.1 implementation and does not properly
386 # support keepalive when it is used on 301 or 302 (redirect) responses.
388 BrowserMatch "Mozilla/2" nokeepalive
389 BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
392 # The following directive disables HTTP/1.1 responses to browsers which
393 # are in violation of the HTTP/1.0 spec by not being able to grok a
394 # basic 1.1 response.
396 BrowserMatch "RealPlayer 4\.0" force-response-1.0
397 BrowserMatch "Java/1\.0" force-response-1.0
398 BrowserMatch "JDK/1\.0" force-response-1.0</pre></example>
401 <section id="no-img-log">
402 <title>Do not log requests for images in the access log</title>
404 <p>This example keeps requests for images from appearing in the
405 access log. It can be easily modified to prevent logging of
406 particular directories, or to prevent logging of requests
407 coming from particular hosts.</p>
409 SetEnvIf Request_URI \.gif image-request
410 SetEnvIf Request_URI \.jpg image-request
411 SetEnvIf Request_URI \.png image-request
412 CustomLog logs/access_log common env=!image-request</pre></example>
415 <section id="image-theft">
416 <title>Prevent "Image Theft"</title>
418 <p>This example shows how to keep people not on your server
419 from using images on your server as inline-images on their
420 pages. This is not a recommended configuration, but it can work
421 in limited circumstances. We assume that all your images are in
422 a directory called /web/images.</p>
424 SetEnvIf Referer "^http://www.example.com/" local_referal
425 # Allow browsers that do not send Referer info
426 SetEnvIf Referer "^$" local_referal
427 <Directory /web/images>
430 Allow from env=local_referal
431 </Directory></pre></example>
433 <p>For more information about this technique, see the
434 ApacheToday tutorial " <a
435 href="http://apachetoday.com/news_story.php3?ltsn=2000-06-14-002-01-PS">
436 Keeping Your Images from Adorning Other Sites</a>".</p>