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 <!-- $Revision: 1.18 $ -->
7 Copyright 2002-2004 The Apache Software Foundation
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
13 http://www.apache.org/licenses/LICENSE-2.0
15 Unless required by applicable law or agreed to in writing, software
16 distributed under the License is distributed on an "AS IS" BASIS,
17 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 See the License for the specific language governing permissions and
19 limitations under the License.
22 <manualpage metafile="env.xml.meta">
24 <title>Environment Variables in Apache</title>
27 <p>The Apache HTTP Server provides a mechanism for storing
28 information in named variables that are called <em>environment
29 variables</em>. This information can be used to control various
30 operations such as logging or access control. The variables are
31 also used as a mechanism to communicate with external programs
32 such as CGI scripts. This document discusses different ways to
33 manipulate and use these variables.</p>
35 <p>Although these variables are referred to as <em>environment
36 variables</em>, they are not the same as the environment
37 variables controlled by the underlying operating system.
38 Instead, these variables are stored and manipulated in an
39 internal Apache structure. They only become actual operating
40 system environment variables when they are provided to CGI
41 scripts and Server Side Include scripts. If you wish to
42 manipulate the operating system environment under which the
43 server itself runs, you must use the standard environment
44 manipulation mechanisms provided by your operating system
48 <section id="setting">
49 <title>Setting Environment Variables</title>
52 <module>mod_env</module>
53 <module>mod_rewrite</module>
54 <module>mod_setenvif</module>
55 <module>mod_unique_id</module>
58 <directive module="mod_setenvif">BrowserMatch</directive>
59 <directive module="mod_setenvif">BrowserMatchNoCase</directive>
60 <directive module="mod_env">PassEnv</directive>
61 <directive module="mod_rewrite">RewriteRule</directive>
62 <directive module="mod_env">SetEnv</directive>
63 <directive module="mod_setenvif">SetEnvIf</directive>
64 <directive module="mod_setenvif">SetEnvIfNoCase</directive>
65 <directive module="mod_env">UnsetEnv</directive>
69 <section id="basic-manipulation">
70 <title>Basic Environment Manipulation</title>
72 <p>The most basic way to set an environment variable in Apache
73 is using the unconditional <directive module="mod_env"
74 >SetEnv</directive> directive. Variables may also be passed from
75 the environment of the shell which started the server using the
76 <directive module="mod_env">PassEnv</directive> directive.</p>
79 <section id="conditional">
80 <title>Conditional Per-Request Settings</title>
82 <p>For additional flexibility, the directives provided by
83 mod_setenvif allow environment variables to be set on a
84 per-request basis, conditional on characteristics of particular
85 requests. For example, a variable could be set only when a
86 specific browser (User-Agent) is making a request, or only when
87 a specific Referer [sic] header is found. Even more flexibility
88 is available through the mod_rewrite's <directive
89 module="mod_rewrite">RewriteRule</directive> which uses the
90 <code>[E=...]</code> option to set environment variables.</p>
93 <section id="unique-identifiers">
94 <title>Unique Identifiers</title>
96 <p>Finally, mod_unique_id sets the environment variable
97 <code>UNIQUE_ID</code> for each request to a value which is
98 guaranteed to be unique across "all" requests under very
99 specific conditions.</p>
102 <section id="standard-cgi">
103 <title>Standard CGI Variables</title>
105 <p>In addition to all environment variables set within the
106 Apache configuration and passed from the shell, CGI scripts and
107 SSI pages are provided with a set of environment variables
108 containing meta-information about the request as required by
109 the <a href="http://cgi-spec.golux.com/">CGI
110 specification</a>.</p>
113 <section id="caveats">
114 <title>Some Caveats</title>
117 <li>It is not possible to override or change the standard CGI
118 variables using the environment manipulation directives.</li>
120 <li>When <a href="suexec.html">suexec</a> is used to launch
121 CGI scripts, the environment will be cleaned down to a set of
122 <em>safe</em> variables before CGI scripts are launched. The
123 list of <em>safe</em> variables is defined at compile-time in
124 <code>suexec.c</code>.</li>
126 <li>For portability reasons, the names of environment
127 variables may contain only letters, numbers, and the
128 underscore character. In addition, the first character may
129 not be a number. Characters which do not match this
130 restriction will be replaced by an underscore when passed to
131 CGI scripts and SSI pages.</li>
136 <title>Using Environment Variables</title>
140 <module>mod_authz_host</module>
141 <module>mod_cgi</module>
142 <module>mod_ext_filter</module>
143 <module>mod_headers</module>
144 <module>mod_include</module>
145 <module>mod_log_config</module>
146 <module>mod_rewrite</module>
149 <directive module="mod_authz_host">Allow</directive>
150 <directive module="mod_log_config">CustomLog</directive>
151 <directive module="mod_authz_host">Deny</directive>
152 <directive module="mod_ext_filter">ExtFilterDefine</directive>
153 <directive module="mod_headers">Header</directive>
154 <directive module="mod_log_config">LogFormat</directive>
155 <directive module="mod_rewrite">RewriteCond</directive>
156 <directive module="mod_rewrite">RewriteRule</directive>
160 <section id="cgi-scripts">
161 <title>CGI Scripts</title>
163 <p>One of the primary uses of environment variables is to
164 communicate information to CGI scripts. As discussed above, the
165 environment passed to CGI scripts includes standard
166 meta-information about the request in addition to any variables
167 set within the Apache configuration. For more details, see the
168 <a href="howto/cgi.html">CGI tutorial</a>.</p>
171 <section id="ssi-pages">
172 <title>SSI Pages</title>
174 <p>Server-parsed (SSI) documents processed by mod_include's
175 <code>INCLUDES</code> filter can print environment variables
176 using the <code>echo</code> element, and can use environment
177 variables in flow control elements to makes parts of a page
178 conditional on characteristics of a request. Apache also
179 provides SSI pages with the standard CGI environment variables
180 as discussed above. For more details, see the <a
181 href="howto/ssi.html">SSI tutorial</a>.</p>
184 <section id="access-control">
185 <title>Access Control</title>
187 <p>Access to the server can be controlled based on the value of
188 environment variables using the <code>allow from env=</code>
189 and <code>deny from env=</code> directives. In combination with
190 <directive module="mod_setenvif">SetEnvIf</directive>, this
191 allows for flexible control of access to the server based on
192 characteristics of the client. For example, you can use these
193 directives to deny access to a particular browser (User-Agent).
197 <section id="logging">
198 <title>Conditional Logging</title>
200 <p>Environment variables can be logged in the access log using
201 the <directive module="mod_log_config">LogFormat</directive>
202 option <code>%e</code>. In addition, the decision on whether
203 or not to log requests can be made based on the status of
204 environment variables using the conditional form of the
205 <directive module="mod_log_config">CustomLog</directive>
206 directive. In combination with <directive module="mod_setenvif"
207 >SetEnvIf</directive> this allows for flexible control of which
208 requests are logged. For example, you can choose not to log
209 requests for filenames ending in <code>gif</code>, or you can
210 choose to only log requests from clients which are outside your
214 <section id="response-headers">
215 <title>Conditional Response Headers</title>
217 <p>The <directive module="mod_headers">Header</directive>
218 directive can use the presence or
219 absence of an environment variable to determine whether or not
220 a certain HTTP header will be placed in the response to the
221 client. This allows, for example, a certain response header to
222 be sent only if a corresponding header is received in the
223 request from the client.</p>
227 <section id="external-filter">
228 <title>External Filter Activation</title>
230 <p>External filters configured by <module>mod_ext_filter</module>
232 module="mod_ext_filter">ExtFilterDefine</directive> directive can
233 by activated conditional on an environment variable using the
234 <code>disableenv=</code> and <code>enableenv=</code> options.</p>
237 <section id="url-rewriting">
238 <title>URL Rewriting</title>
240 <p>The <code>%{ENV:...}</code> form of <em>TestString</em> in
241 the <directive module="mod_rewrite">RewriteCond</directive>
242 allows mod_rewrite's rewrite
243 engine to make decisions conditional on environment variables.
244 Note that the variables accessible in mod_rewrite without the
245 <code>ENV:</code> prefix are not actually environment
246 variables. Rather, they are variables special to mod_rewrite
247 which cannot be accessed from other modules.</p>
251 <section id="special">
252 <title>Special Purpose Environment Variables</title>
254 <p>Interoperability problems have led to the introduction of
255 mechanisms to modify the way Apache behaves when talking to
256 particular clients. To make these mechanisms as flexible as
257 possible, they are invoked by defining environment variables,
258 typically with <directive module="mod_setenvif"
259 >BrowserMatch</directive>, though <directive module="mod_env"
260 >SetEnv</directive> and <directive module="mod_env"
261 >PassEnv</directive> could also be used, for example.</p>
263 <section id="downgrade">
264 <title>downgrade-1.0</title>
266 <p>This forces the request to be treated as a HTTP/1.0 request
267 even if it was in a later dialect.</p>
270 <section id="force-gzip">
271 <title>force-gzip</title>
272 <p>If you have the <code>DEFLATE</code> filter activated, this
273 environment variable will ignore the accept-encoding setting of
274 your browser and will send compressed output unconditionally.</p>
276 <section id="force-no-vary">
277 <title>force-no-vary</title>
279 <p>This causes any <code>Vary</code> fields to be removed from
280 the response header before it is sent back to the client. Some
281 clients don't interpret this field correctly; setting this
282 variable can work around this problem. Setting this variable
283 also implies <strong>force-response-1.0</strong>.</p>
286 <section id="force-response">
287 <title>force-response-1.0</title>
289 <p>This forces an HTTP/1.0 response to clients making an HTTP/1.0
290 request. It was originally
291 implemented as a result of a problem with AOL's proxies. Some
292 HTTP/1.0 clients may not behave correctly when given an HTTP/1.1
293 response, and this can be used to interoperate with them.</p>
297 <section id="gzip-only-text-html">
298 <title>gzip-only-text/html</title>
300 <p>When set to a value of "1", this variable disables the DEFLATE
301 output filter provided by <module>mod_deflate</module> for
302 content-types other than <code>text/html</code>. If you'd rather
303 use statically compressed files; <module>mod_negotiation</module>
304 evaluates the variable as well (not only for gzip, but for all
305 encodings that differ from "identity").</p>
308 <section id="no-gzip"><title>no-gzip</title>
310 <p>When set, the <code>DEFLATE</code> filter of
311 <module>mod_deflate</module> will be turned off and
312 <module>mod_negotiation</module> will refuse to deliver encoded
317 <section id="nokeepalive">
318 <title>nokeepalive</title>
320 <p>This disables <directive module="core">KeepAlive</directive>
325 <section id="prefer-language"><title>prefer-language</title>
327 <p>This influences <module>mod_negotiation</module>'s behaviour. If
328 it contains a language tag (such as <code>en</code>, <code>ja</code>
329 or <code>x-klingon</code>), <module>mod_negotiation</module> tries
330 to deliver a variant with that language. If there's no such variant,
331 the normal <a href="content-negotiation.html">negotiation</a> process
336 <section id="redirect-carefully">
337 <title>redirect-carefully</title>
339 <p>This forces the server to be more careful when sending a redirect
340 to the client. This is typically used when a client has a known
341 problem handling redirects. This was originally implemented as a
342 result of a problem with Microsoft's WebFolders software which has
343 a problem handling redirects on directory resources via DAV
348 <section id="suppress-error-charset">
349 <title>suppress-error-charset</title>
351 <p><em>Available in versions after 2.0.40</em></p>
353 <p>When Apache issues a redirect in response to a client request,
354 the response includes some actual text to be displayed in case
355 the client can't (or doesn't) automatically follow the redirection.
356 Apache ordinarily labels this text according to the character set
357 which it uses, which is ISO-8859-1.</p>
358 <p> However, if the redirection is to a page that uses a different
359 character set, some broken browser versions will try to use the
360 character set from the redirection text rather than the actual page.
361 This can result in Greek, for instance, being incorrectly rendered.</p>
362 <p>Setting this environment variable causes Apache to omit the character
363 set for the redirection text, and these broken browsers will then correctly
364 use that of the destination page.</p>
370 <section id="examples">
371 <title>Examples</title>
373 <section id="misbehaving">
374 <title>Changing protocol behavior with misbehaving clients</title>
376 <p>We recommend that the following lines be included in
377 httpd.conf to deal with known client problems.</p>
380 # The following directives modify normal HTTP response behavior.
381 # The first directive disables keepalive for Netscape 2.x and browsers that
382 # spoof it. There are known problems with these browser implementations.
383 # The second directive is for Microsoft Internet Explorer 4.0b2
384 # which has a broken HTTP/1.1 implementation and does not properly
385 # support keepalive when it is used on 301 or 302 (redirect) responses.
387 BrowserMatch "Mozilla/2" nokeepalive
388 BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
391 # The following directive disables HTTP/1.1 responses to browsers which
392 # are in violation of the HTTP/1.0 spec by not being able to grok a
393 # basic 1.1 response.
395 BrowserMatch "RealPlayer 4\.0" force-response-1.0
396 BrowserMatch "Java/1\.0" force-response-1.0
397 BrowserMatch "JDK/1\.0" force-response-1.0</pre></example>
400 <section id="no-img-log">
401 <title>Do not log requests for images in the access log</title>
403 <p>This example keeps requests for images from appearing in the
404 access log. It can be easily modified to prevent logging of
405 particular directories, or to prevent logging of requests
406 coming from particular hosts.</p>
408 SetEnvIf Request_URI \.gif image-request
409 SetEnvIf Request_URI \.jpg image-request
410 SetEnvIf Request_URI \.png image-request
411 CustomLog logs/access_log common env=!image-request</pre></example>
414 <section id="image-theft">
415 <title>Prevent "Image Theft"</title>
417 <p>This example shows how to keep people not on your server
418 from using images on your server as inline-images on their
419 pages. This is not a recommended configuration, but it can work
420 in limited circumstances. We assume that all your images are in
421 a directory called /web/images.</p>
423 SetEnvIf Referer "^http://www.example.com/" local_referal
424 # Allow browsers that do not send Referer info
425 SetEnvIf Referer "^$" local_referal
426 <Directory /web/images>
429 Allow from env=local_referal
430 </Directory></pre></example>
432 <p>For more information about this technique, see the
433 ApacheToday tutorial " <a
434 href="http://apachetoday.com/news_story.php3?ltsn=2000-06-14-002-01-PS">
435 Keeping Your Images from Adorning Other Sites</a>".</p>