]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_headers.xml
Merge in APR[-util] macros from branches/trunk-buildconf-noapr
[apache] / docs / manual / mod / mod_headers.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
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
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
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.
21 -->
22
23 <modulesynopsis metafile="mod_headers.xml.meta">
24
25 <name>mod_headers</name>
26 <description>Customization of HTTP request and response
27 headers</description>
28 <status>Extension</status>
29 <sourcefile>mod_headers.c</sourcefile>
30 <identifier>headers_module</identifier>
31
32 <summary>
33     <p>This module provides directives to control and modify HTTP
34     request and response headers. Headers can be merged, replaced
35     or removed.</p>
36 </summary>
37
38 <section id="order"><title>Order of Processing</title>
39
40     <p>The directives provided by <module>mod_headers</module> can
41     occur almost anywhere within the server configuration, and can be
42     limited in scope by enclosing them in <a
43     href="../sections.html">configuration sections</a>.</p>
44
45     <p>Order of processing is important and is affected both by the
46     order in the configuration file and by placement in <a
47     href="../sections.html#mergin">configuration sections</a>. These
48     two directives have a different effect if reversed:</p>
49
50     <highlight language="config">
51 RequestHeader append MirrorID "mirror 12"
52 RequestHeader unset MirrorID
53     </highlight>
54
55     <p>This way round, the <code>MirrorID</code> header is not set. If
56     reversed, the MirrorID header is set to "mirror 12".</p>
57 </section>
58
59 <section id="early"><title>Early and Late Processing</title>
60     <p><module>mod_headers</module> can be applied either early or late
61     in the request.  The normal mode is late, when <em>Request</em> Headers are
62     set immediately before running the content generator and <em>Response</em>
63     Headers just as the response is sent down the wire.  Always use
64     Late mode in an operational server.</p>
65
66     <p>Early mode is designed as a test/debugging aid for developers.
67     Directives defined using the <code>early</code> keyword are set
68     right at the beginning of processing the request.  This means
69     they can be used to simulate different requests and set up test
70     cases, but it also means that headers may be changed at any time
71     by other modules before generating a Response.</p>
72
73     <p>Because early directives are processed before the request path's
74     configuration is traversed, early headers can only be set in a
75     main server or virtual host context.  Early directives cannot depend
76     on a request path, so they will fail in contexts such as
77     <directive type="section" module="core">Directory</directive> or
78     <directive type="section" module="core">Location</directive>.</p>
79 </section>
80
81 <section id="examples"><title>Examples</title>
82
83     <ol>
84       <li>
85         Copy all request headers that begin with "TS" to the
86         response headers:
87
88         <highlight language="config">
89           Header echo ^TS
90         </highlight>
91       </li>
92
93       <li>
94         Add a header, <code>MyHeader</code>, to the response including a
95         timestamp for when the request was received and how long it
96         took to begin serving the request. This header can be used by
97         the client to intuit load on the server or in isolating
98         bottlenecks between the client and the server.
99
100         <highlight language="config">
101           Header set MyHeader "%D %t"
102         </highlight>
103
104         <p>results in this header being added to the response:</p>
105
106         <example>
107           MyHeader: D=3775428 t=991424704447256
108         </example>
109       </li>
110
111       <li>
112         Say hello to Joe
113
114         <highlight language="config">
115 Header set MyHeader "Hello Joe. It took %D microseconds for Apache to serve this request."
116         </highlight>
117
118         <p>results in this header being added to the response:</p>
119
120         <example>
121           MyHeader: Hello Joe. It took D=3775428 microseconds for Apache
122           to serve this request.
123         </example>
124       </li>
125
126       <li>
127         Conditionally send <code>MyHeader</code> on the response if and
128         only if header <code>MyRequestHeader</code> is present on the request.
129         This is useful for constructing headers in response to some client
130         stimulus. Note that this example requires the services of the
131         <module>mod_setenvif</module> module.
132
133         <highlight language="config">
134 SetEnvIf MyRequestHeader myvalue HAVE_MyRequestHeader
135 Header set MyHeader "%D %t mytext" env=HAVE_MyRequestHeader
136         </highlight>
137
138         <p>If the header <code>MyRequestHeader: myvalue</code> is present on
139         the HTTP request, the response will contain the following header:</p>
140
141         <example>
142           MyHeader: D=3775428 t=991424704447256 mytext
143         </example>
144       </li>
145
146       <li>
147         Enable DAV to work with Apache running HTTP through SSL hardware
148         (<a href="http://svn.haxx.se/users/archive-2006-03/0549.shtml">problem
149         description</a>) by replacing <var>https:</var> with
150         <var>http:</var> in the <var>Destination</var> header:
151
152         <highlight language="config">
153           RequestHeader edit Destination ^https: http: early
154         </highlight>
155       </li>
156
157       <li>
158         Set the same header value under multiple nonexclusive conditions,
159         but do not duplicate the value in the final header.
160         If all of the following conditions applied to a request (i.e.,
161         if the <code>CGI</code>, <code>NO_CACHE</code> and
162         <code>NO_STORE</code> environment variables all existed for the
163         request):
164
165         <highlight language="config">
166 Header merge Cache-Control no-cache env=CGI
167 Header merge Cache-Control no-cache env=NO_CACHE
168 Header merge Cache-Control no-store env=NO_STORE
169         </highlight>
170
171         <p>then the response would contain the following header:</p>
172
173         <example>
174           Cache-Control: no-cache, no-store
175         </example>
176
177         <p>If <code>append</code> was used instead of <code>merge</code>,
178         then the response would contain the following header:</p>
179
180         <example>
181           Cache-Control: no-cache, no-cache, no-store
182         </example>
183       </li>
184       <li>
185         Set a test cookie if and only if the client didn't send us a cookie
186         <highlight language="config">
187           Header set Set-Cookie testcookie "expr=-z %{req:Cookie}"
188         </highlight>
189       </li>
190       <li>
191         Append a Caching header for responses with a HTTP status code of 200
192         <highlight language="config">
193           Header append Cache-Control s-maxage=600 "expr=%{REQUEST_STATUS} == 200"
194         </highlight>
195       </li>
196
197     </ol>
198 </section>
199
200 <directivesynopsis>
201 <name>RequestHeader</name>
202 <description>Configure HTTP request headers</description>
203 <syntax>RequestHeader add|append|edit|edit*|merge|set|setifempty|unset
204 <var>header</var> [[expr=]<var>value</var> [<var>replacement</var>]
205 [early|env=[!]<var>varname</var>|expr=<var>expression</var>]]
206 </syntax>
207 <contextlist><context>server config</context><context>virtual host</context>
208 <context>directory</context><context>.htaccess</context></contextlist>
209 <override>FileInfo</override>
210 <compatibility>SetIfEmpty available in 2.4.7 and later, expr=value
211 available in 2.4.10 and later</compatibility>
212
213 <usage>
214     <p>This directive can replace, merge, change or remove HTTP request
215     headers. The header is modified just before the content handler
216     is run, allowing incoming headers to be modified. The action it
217     performs is determined by the first argument. This can be one
218     of the following values:</p>
219
220     <dl>
221
222     <dt><code>add</code></dt>
223     <dd>The request header is added to the existing set of headers,
224     even if this header already exists. This can result in two
225     (or more) headers having the same name. This can lead to
226     unforeseen consequences, and in general <code>set</code>,
227     <code>append</code> or <code>merge</code> should be used instead.</dd>
228
229     <dt><code>append</code></dt>
230     <dd>The request header is appended to any existing header of the
231     same name. When a new value is merged onto an existing header
232     it is separated from the existing header with a comma. This
233     is the HTTP standard way of giving a header multiple
234     values.</dd>
235
236     <dt><code>edit</code></dt>
237     <dt><code>edit*</code></dt>
238     <dd>If this request header exists, its value is transformed according
239     to a <glossary ref="regex">regular expression</glossary>
240     search-and-replace.  The <var>value</var> argument is a <glossary
241     ref="regex">regular expression</glossary>, and the <var>replacement</var>
242     is a replacement string, which may contain backreferences or format specifiers.
243     The <code>edit</code> form will match and replace exactly once
244     in a header value, whereas the <code>edit*</code> form will replace
245     <em>every</em> instance of the search pattern if it appears more
246     than once.</dd>
247
248     <dt><code>merge</code></dt>
249     <dd>The request header is appended to any existing header of
250     the same name, unless the value to be appended already appears in the
251     existing header's comma-delimited list of values.  When a new value is
252     merged onto an existing header it is separated from the existing header
253     with a comma.  This is the HTTP standard way of giving a header multiple
254     values.  Values are compared in a case sensitive manner, and after
255     all format specifiers have been processed.  Values in double quotes
256     are considered different from otherwise identical unquoted values.</dd>
257
258     <dt><code>set</code></dt>
259     <dd>The request header is set, replacing any previous header
260     with this name</dd>
261
262     <dt><code>setifempty</code></dt>
263     <dd>The request header is set, but only if there is no previous header
264     with this name.<br />
265     Available in 2.4.7 and later.</dd>
266
267     <dt><code>unset</code></dt>
268     <dd>The request header of this name is removed, if it exists. If
269     there are multiple headers of the same name, all will be removed.
270     <var>value</var> must be omitted.</dd>
271     </dl>
272
273     <p>This argument is followed by a header name, which can
274     include the final colon, but it is not required. Case is
275     ignored. For <code>set</code>, <code>append</code>, <code>merge</code> and
276     <code>add</code> a <var>value</var> is given as the third argument. If a
277     <var>value</var> contains spaces, it should be surrounded by double
278     quotes. For <code>unset</code>, no <var>value</var> should be given.
279     <var>value</var> may be a character string, a string containing format
280     specifiers or a combination of both. The supported format specifiers
281     are the same as for the <directive module="mod_headers">Header</directive>,
282     please have a look there for details.  For <code>edit</code> both
283     a <var>value</var> and a <var>replacement</var> are required, and are
284     a <glossary ref="regex">regular expression</glossary> and a
285     replacement string respectively.</p>
286
287     <p>The <directive>RequestHeader</directive> directive may be followed by
288     an additional argument, which may be any of:</p>
289     <dl>
290     <dt><code>early</code></dt>
291     <dd>Specifies <a href="#early">early processing</a>.</dd>
292     <dt><code>env=[!]<var>varname</var></code></dt>
293     <dd>The directive is applied if and only if the <a href="../env.html"
294         >environment variable</a> <code>varname</code> exists.
295         A <code>!</code> in front of <code>varname</code> reverses the test,
296         so the directive applies only if <code>varname</code> is unset.</dd>
297     <dt><code>expr=<var>expression</var></code></dt>
298     <dd>The directive is applied if and only if <var>expression</var>
299         evaluates to true. Details of expression syntax and evaluation are
300         documented in the <a href="../expr.html">ap_expr</a> documentation.</dd>
301     </dl>
302
303     <p>Except in <a href="#early">early</a> mode, the
304     <directive>RequestHeader</directive> directive is processed
305     just before the request is run by its handler in the fixup phase.
306     This should allow headers generated by the browser, or by Apache
307     input filters to be overridden or modified.</p>
308 </usage>
309 </directivesynopsis>
310
311 <directivesynopsis>
312 <name>Header</name>
313 <description>Configure HTTP response headers</description>
314 <syntax>Header [<var>condition</var>] add|append|echo|edit|edit*|merge|set|setifempty|unset|note
315 <var>header</var> [[expr=]<var>value</var> [<var>replacement</var>]
316 [early|env=[!]<var>varname</var>|expr=<var>expression</var>]]
317 </syntax>
318 <contextlist><context>server config</context><context>virtual host</context>
319 <context>directory</context><context>.htaccess</context></contextlist>
320 <override>FileInfo</override>
321 <compatibility>SetIfEmpty available in 2.4.7 and later, expr=value
322 available in 2.4.10 and later</compatibility>
323
324 <usage>
325     <p>This directive can replace, merge or remove HTTP response
326     headers. The header is modified just after the content handler
327     and output filters are run, allowing outgoing headers to be
328     modified.</p>
329
330     <p> The optional <var>condition</var> argument determines which internal
331     table of responses headers this directive will operate against. Despite the
332     name, the default value of <code>onsuccess</code> does <em>not</em> limit
333     an <var>action</var> to responses with a 2xx status code.  Headers set under
334     this condition are still used when, for example, a request is <em>successfully</em>
335     proxied or generated by CGI, even when they have generated a failing status code.</p>
336
337     <p>When your action is a function of an existing header, you may need to specify
338     a condition of <code>always</code>, depending on which internal table the
339     original header was set in.  The table that corresponds to <code>always</code> is
340     used for locally generated error responses as well as successful responses.
341     Note also that repeating this directive with both conditions makes sense in
342     some scenarios because <code>always</code> is not a superset of
343     <code>onsuccess</code> with respect to existing headers:</p>
344
345     <ul>
346        <li> You're adding a header to a locally generated non-success (non-2xx) response, such
347             as a redirect, in which case only the table corresponding to
348             <code>always</code> is used in the ultimate response.</li>
349        <li> You're modifying or removing a header generated by a CGI script,
350             in which case the CGI scripts are in the table corresponding to
351             <code>always</code> and not in the default table.</li>
352        <li> You're modifying or removing a header generated by some piece of
353             the server but that header is not being found by the default
354             <code>onsuccess</code> condition.</li>
355     </ul>
356
357     <p>Separately from the <var>condition</var> parameter described above, you
358     can limit an action based on HTTP status codes for e.g. proxied or CGI
359     requests. See the example that uses %{REQUEST_STATUS} in the section above.</p>
360
361     <p>The action it performs is determined by the first
362     argument (second argument if a <var>condition</var> is specified).
363     This can be one of the following values:</p>
364
365     <dl>
366     <dt><code>add</code></dt>
367     <dd>The response header is added to the existing set of headers,
368     even if this header already exists. This can result in two
369     (or more) headers having the same name. This can lead to
370     unforeseen consequences, and in general <code>set</code>,
371     <code>append</code> or <code>merge</code> should be used instead.</dd>
372
373     <dt><code>append</code></dt>
374     <dd>The response header is appended to any existing header of
375     the same name. When a new value is merged onto an existing
376     header it is separated from the existing header with a comma.
377     This is the HTTP standard way of giving a header multiple values.</dd>
378
379     <dt><code>echo</code></dt>
380     <dd>Request headers with this name are echoed back in the
381     response headers. <var>header</var> may be a
382     <glossary ref="regex">regular expression</glossary>.
383     <var>value</var> must be omitted.</dd>
384
385     <dt><code>edit</code></dt>
386     <dt><code>edit*</code></dt>
387     <dd>If this response header exists, its value is transformed according
388     to a <glossary ref="regex">regular expression</glossary>
389     search-and-replace.  The <var>value</var> argument is a <glossary
390     ref="regex">regular expression</glossary>, and the <var>replacement</var>
391     is a replacement string, which may contain backreferences or format specifiers.
392     The <code>edit</code> form will match and replace exactly once
393     in a header value, whereas the <code>edit*</code> form will replace
394     <em>every</em> instance of the search pattern if it appears more
395     than once.</dd>
396
397     <dt><code>merge</code></dt>
398     <dd>The response header is appended to any existing header of
399     the same name, unless the value to be appended already appears in the
400     header's comma-delimited list of values.  When a new value is merged onto
401     an existing header it is separated from the existing header with a comma.
402     This is the HTTP standard way of giving a header multiple values.
403     Values are compared in a case sensitive manner, and after
404     all format specifiers have been processed.  Values in double quotes
405     are considered different from otherwise identical unquoted values.</dd>
406
407     <dt><code>set</code></dt>
408     <dd>The response header is set, replacing any previous header
409     with this name. The <var>value</var> may be a format string.</dd>
410
411     <dt><code>setifempty</code></dt>
412     <dd>The request header is set, but only if there is no previous header
413     with this name.
414     <note>
415     The Content-Type header is a special use case since there might be
416     the chance that its value have been determined but the header is not part
417     of the response when <code>setifempty</code> is evaluated.
418     It is safer to use <code>set</code> for this use case like in the
419     following example:
420     <highlight language="config">
421     Header set Content-Type "text/plain" "expr=-z %{CONTENT_TYPE}"
422     </highlight>
423     </note></dd>
424
425     <dt><code>unset</code></dt>
426     <dd>The response header of this name is removed, if it exists.
427     If there are multiple headers of the same name, all will be
428     removed. <var>value</var> must be omitted.</dd>
429
430     <dt><code>note</code></dt>
431     <dd>The value of the named response <var>header</var> is copied into an
432     internal note whose name is given by <var>value</var>.  This is useful
433     if a header sent by a CGI or proxied resource is configured to be unset
434     but should also be logged.<br />
435     Available in 2.4.7 and later.</dd>
436
437     </dl>
438
439     <p>This argument is followed by a <var>header</var> name, which
440     can include the final colon, but it is not required. Case is
441     ignored for <code>set</code>, <code>append</code>, <code>merge</code>,
442     <code>add</code>, <code>unset</code> and <code>edit</code>.
443     The <var>header</var> name for <code>echo</code>
444     is case sensitive and may be a <glossary ref="regex">regular
445     expression</glossary>.</p>
446
447     <p>For <code>set</code>, <code>append</code>, <code>merge</code> and
448     <code>add</code> a <var>value</var> is specified as the next argument.
449     If <var>value</var>
450     contains spaces, it should be surrounded by double quotes.
451     <var>value</var> may be a character string, a string containing
452     <module>mod_headers</module> specific format specifiers (and character
453     literals), or an <a href="../expr.html">ap_expr</a> expression prefixed
454     with <em>expr=</em></p>
455
456     <p> The following format specifiers are supported in <var>value</var>:</p>
457
458     <table border="1" style="zebra">
459     <columnspec><column width=".25"/><column width=".75"/></columnspec>
460     <tr><th>Format</th><th>Description</th></tr>
461     <tr><td><code>%%</code></td>
462         <td>The percent sign</td></tr>
463
464     <tr><td><code>%t</code></td>
465         <td>The time the request was received in Universal Coordinated Time
466         since the epoch (Jan. 1, 1970) measured in microseconds. The value
467         is preceded by <code>t=</code>.</td></tr>
468
469     <tr><td><code>%D</code></td>
470         <td>The time from when the request was received to the time the
471         headers are sent on the wire. This is a measure of the duration
472         of the request. The value is preceded by <code>D=</code>.
473         The value is measured in microseconds.</td></tr>
474
475     <tr><td><code>%l</code></td>
476         <td>The current load averages of the actual server itself. It is
477         designed to expose the values obtained by <code>getloadavg()</code>
478         and this represents the current load average, the 5 minute average, and
479         the 15 minute average. The value is preceded by <code>l=</code> with each
480         average separated by <code>/</code>.<br />
481         Available in 2.4.4 and later.
482         </td></tr>
483
484     <tr><td><code>%i</code></td>
485         <td>The current idle percentage of httpd (0 to 100) based on available
486         processes and threads. The value is preceded by <code>i=</code>.<br />
487         Available in 2.4.4 and later.
488         </td></tr>
489
490     <tr><td><code>%b</code></td>
491         <td>The current busy percentage of httpd (0 to 100) based on available
492         processes and threads. The value is preceded by <code>b=</code>.<br />
493         Available in 2.4.4 and later.
494         </td></tr>
495
496     <tr><td><code>%{VARNAME}e</code></td>
497         <td>The contents of the <a href="../env.html">environment
498         variable</a> <code>VARNAME</code>.</td></tr>
499
500     <tr><td><code>%{VARNAME}s</code></td>
501         <td>The contents of the <a href="mod_ssl.html#envvars">SSL environment
502         variable</a> <code>VARNAME</code>, if <module>mod_ssl</module> is enabled.</td></tr>
503
504     </table>
505
506     <note><title>Note</title>
507       <p>The <code>%s</code> format specifier is only available in
508       Apache 2.1 and later; it can be used instead of <code>%e</code>
509       to avoid the overhead of enabling <code>SSLOptions
510       +StdEnvVars</code>.  If <code>SSLOptions +StdEnvVars</code> must
511       be enabled anyway for some other reason, <code>%e</code> will be
512       more efficient than <code>%s</code>.</p>
513     </note>
514
515     <note><title>Note on expression values</title>
516     <p> When the value parameter uses the <a href="../expr.html">ap_expr</a>
517     parser, some expression syntax will differ from examples that evaluate
518     <em>boolean</em> expressions such as &lt;If&gt;:</p>
519     <ul>
520       <li>The starting point of the grammar is 'string' rather than 'expr'.</li>
521       <li>Function calls use the %{funcname:arg} syntax rather than
522           funcname(arg).</li>
523       <li>Multi-argument functions are not currently accessible from this
524           starting point</li>
525       <li>Quote the entire parameter, such as
526           <highlight language="config">
527         Header set foo-checksum "expr=%{md5:foo}"
528           </highlight>
529        </li>
530
531     </ul>
532     </note>
533
534     <p>For <code>edit</code> there is both a <var>value</var> argument
535     which is a <glossary ref="regex">regular expression</glossary>,
536     and an additional <var>replacement</var> string. As of version 2.4.7
537     the replacement string may also contain format specifiers.</p>
538
539     <p>The <directive>Header</directive> directive may be followed by
540     an additional argument, which may be any of:</p>
541     <dl>
542     <dt><code>early</code></dt>
543     <dd>Specifies <a href="#early">early processing</a>.</dd>
544     <dt><code>env=[!]<var>varname</var></code></dt>
545     <dd>The directive is applied if and only if the <a href="../env.html"
546         >environment variable</a> <code>varname</code> exists.
547         A <code>!</code> in front of <code>varname</code> reverses the test,
548         so the directive applies only if <code>varname</code> is unset.</dd>
549     <dt><code>expr=<var>expression</var></code></dt>
550     <dd>The directive is applied if and only if <var>expression</var>
551         evaluates to true. Details of expression syntax and evaluation are
552         documented in the <a href="../expr.html">ap_expr</a> documentation.
553         <highlight language="config">
554 # This delays the evaluation of the condition clause compared to &lt;If&gt;
555 Header always set CustomHeader my-value "expr=%{REQUEST_URI} =~ m#^/special_path.php$#"
556         </highlight>   
557         </dd>
558     </dl>
559
560     <p>Except in <a href="#early">early</a> mode, the
561     <directive>Header</directive> directives are processed just
562     before the response is sent to the network. This means that it is
563     possible to set and/or override most headers, except for some headers
564     added by the HTTP header filter.  Prior to 2.2.12, it was not possible
565     to change the Content-Type header with this directive.</p>
566
567 </usage>
568 </directivesynopsis>
569
570 </modulesynopsis>