]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_deflate.xml
Merge in APR[-util] macros from branches/trunk-buildconf-noapr
[apache] / docs / manual / mod / mod_deflate.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_deflate.xml.meta">
24
25 <name>mod_deflate</name>
26 <description>Compress content before it is delivered to the
27 client</description>
28 <status>Extension</status>
29 <sourcefile>mod_deflate.c</sourcefile>
30 <identifier>deflate_module</identifier>
31
32 <summary>
33     <p>The <module>mod_deflate</module> module provides
34     the <code>DEFLATE</code> output filter that allows output from
35     your server to be compressed before being sent to the client over
36     the network.</p>
37 </summary>
38 <seealso><a href="../filter.html">Filters</a></seealso>
39
40 <section id="supportedencodings"><title>Supported Encodings</title>
41   <p>The <code>gzip</code> encoding is the only one supported to ensure complete compatibility
42   with old browser implementations. The <code>deflate</code> encoding is not supported, 
43   please check the <a href="http://www.gzip.org/zlib/zlib_faq.html#faq38">zlib's documentation</a>
44   for a complete explanation.
45   </p>
46 </section>
47
48 <section id="recommended"><title>Sample Configurations</title>
49     <note type="warning"><title>Compression and TLS</title>
50         <p>Some web applications are vulnerable to an information disclosure
51         attack when a TLS connection carries deflate compressed data. For more
52         information, review the details of the "BREACH" family of attacks.</p>
53     </note>
54     <p>This is a simple configuration that compresses common text-based content types.</p>
55
56     <example><title>Compress only a few types</title>
57     <highlight language="config">
58       AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
59       </highlight>
60     </example>
61
62 </section>
63
64 <section id="enable"><title>Enabling Compression</title>
65     <note type="warning"><title>Compression and TLS</title>
66         <p>Some web applications are vulnerable to an information disclosure
67         attack when a TLS connection carries deflate compressed data. For more
68         information, review the details of the "BREACH" family of attacks.</p>
69     </note>
70
71     <section id="output"><title>Output Compression</title>
72       <p>Compression is implemented by the <code>DEFLATE</code>
73       <a href="../filter.html">filter</a>. The following directive
74       will enable compression for documents in the container where it
75       is placed:</p>
76
77       <highlight language="config">
78 SetOutputFilter DEFLATE
79 SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
80       </highlight>
81
82       <p>If you want to restrict the compression to particular MIME types
83       in general, you may use the <directive module="mod_filter"
84       >AddOutputFilterByType</directive> directive. Here is an example of
85       enabling compression only for the html files of the Apache
86       documentation:</p>
87
88       <highlight language="config">
89 &lt;Directory "/your-server-root/manual"&gt;
90     AddOutputFilterByType DEFLATE text/html
91 &lt;/Directory&gt;
92       </highlight>
93
94       <note><title>Note</title>
95         The <code>DEFLATE</code> filter is always inserted after RESOURCE
96         filters like PHP or SSI. It never touches internal subrequests.
97       </note>
98       <note><title>Note</title>
99         There is an environment variable <code>force-gzip</code>,
100         set via <directive module="mod_env">SetEnv</directive>, which
101         will ignore the accept-encoding setting of your browser and will
102         send compressed output.
103       </note>
104
105     </section>
106     <section id="inflate"><title>Output Decompression</title>
107       <p>The <module>mod_deflate</module> module also provides a filter for
108       inflating/uncompressing a gzip compressed response body. In order to activate
109       this feature you have to insert the <code>INFLATE</code> filter into
110       the output filter chain using <directive module="core"
111       >SetOutputFilter</directive> or <directive module="mod_mime"
112       >AddOutputFilter</directive>, for example:</p>
113
114       <highlight language="config">
115 &lt;Location "/dav-area"&gt;
116     ProxyPass "http://example.com/"
117     SetOutputFilter INFLATE
118 &lt;/Location&gt;
119       </highlight>
120
121       <p>This Example will uncompress gzip'ed output from example.com, so other
122       filters can do further processing with it.
123       </p>
124
125     </section>
126     <section id="input"><title>Input Decompression</title>
127       <p>The <module>mod_deflate</module> module also provides a filter for
128       decompressing a gzip compressed request body . In order to activate
129       this feature you have to insert the <code>DEFLATE</code> filter into
130       the input filter chain using <directive module="core"
131       >SetInputFilter</directive> or <directive module="mod_mime"
132       >AddInputFilter</directive>, for example:</p>
133
134       <highlight language="config">
135 &lt;Location "/dav-area"&gt;
136     SetInputFilter DEFLATE
137 &lt;/Location&gt;
138       </highlight>
139
140       <p>Now if a request contains a <code>Content-Encoding:
141       gzip</code> header, the body will be automatically decompressed.
142       Few browsers have the ability to gzip request bodies. However,
143       some special applications actually do support request
144       compression, for instance some <a
145       href="http://www.webdav.org">WebDAV</a> clients.</p>
146
147       <note type="warning"><title>Note on Content-Length</title>
148         <p>If you evaluate the request body yourself, <em>don't trust
149         the <code>Content-Length</code> header!</em>
150         The Content-Length header reflects the length of the
151         incoming data from the client and <em>not</em> the byte count of
152         the decompressed data stream.</p>
153       </note>
154     </section>
155 </section>
156
157 <section id="proxies"><title>Dealing with proxy servers</title>
158
159     <p>The <module>mod_deflate</module> module sends a <code>Vary:
160     Accept-Encoding</code> HTTP response header to alert proxies that
161     a cached response should be sent only to clients that send the
162     appropriate <code>Accept-Encoding</code> request header.  This
163     prevents compressed content from being sent to a client that will
164     not understand it.</p>
165
166     <p>If you use some special exclusions dependent
167     on, for example, the <code>User-Agent</code> header, you must
168     manually configure an addition to the <code>Vary</code> header
169     to alert proxies of the additional restrictions.  For example,
170     in a typical configuration where the addition of the <code>DEFLATE</code>
171     filter depends on the <code>User-Agent</code>, you should add:</p>
172
173     <highlight language="config">
174       Header append Vary User-Agent
175     </highlight>
176
177     <p>If your decision about compression depends on other information
178     than request headers (<em>e.g.</em> HTTP version), you have to set the
179     <code>Vary</code> header to the value <code>*</code>. This prevents
180     compliant proxies from caching entirely.</p>
181
182     <example><title>Example</title>
183     <highlight language="config">
184       Header set Vary *
185       </highlight>
186     </example>
187 </section>
188
189 <section id="precompressed"><title>Serving pre-compressed
190 content</title>
191
192     <p>Since <module>mod_deflate</module> re-compresses content each
193     time a request is made, some performance benefit can be derived by
194     pre-compressing the content and telling mod_deflate to serve them
195     without re-compressing them. This may be accomplished using a
196     configuration like the following:</p>
197
198     <highlight language="config">
199 &lt;IfModule mod_headers.c&gt;
200     # Serve gzip compressed CSS files if they exist
201     # and the client accepts gzip.
202     RewriteCond "%{HTTP:Accept-encoding}" "gzip"
203     RewriteCond "%{REQUEST_FILENAME}\.gz" "-s"
204     RewriteRule "^(.*)\.css"              "$1\.css\.gz" [QSA]
205
206     # Serve gzip compressed JS files if they exist
207     # and the client accepts gzip.
208     RewriteCond "%{HTTP:Accept-encoding}" "gzip"
209     RewriteCond "%{REQUEST_FILENAME}\.gz" "-s"
210     RewriteRule "^(.*)\.js"               "$1\.js\.gz" [QSA]
211
212
213     # Serve correct content types, and prevent mod_deflate double gzip.
214     RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
215     RewriteRule "\.js\.gz$"  "-" [T=text/javascript,E=no-gzip:1]
216
217
218     &lt;FilesMatch "(\.js\.gz|\.css\.gz)$"&gt;
219       # Serve correct encoding type.
220       Header append Content-Encoding gzip
221
222       # Force proxies to cache gzipped &amp;
223       # non-gzipped css/js files separately.
224       Header append Vary Accept-Encoding
225     &lt;/FilesMatch&gt;
226 &lt;/IfModule&gt;
227     </highlight>
228
229 </section>
230
231 <directivesynopsis>
232 <name>DeflateFilterNote</name>
233 <description>Places the compression ratio in a note for logging</description>
234 <syntax>DeflateFilterNote [<var>type</var>] <var>notename</var></syntax>
235 <contextlist><context>server config</context><context>virtual host</context>
236 </contextlist>
237
238 <usage>
239     <p>The <directive>DeflateFilterNote</directive> directive
240     specifies that a note about compression ratios should be attached
241     to the request. The name of the note is the value specified for
242     the directive. You can use that note for statistical purposes by
243     adding the value to your <a href="../logs.html#accesslog"
244     >access log</a>.</p>
245
246     <example><title>Example</title>
247     <highlight language="config">
248       DeflateFilterNote ratio
249
250       LogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' deflate
251       CustomLog "logs/deflate_log" deflate
252       </highlight>
253     </example>
254
255     <p>If you want to extract more accurate values from your logs, you
256     can use the <var>type</var> argument to specify the type of data
257     left as a note for logging. <var>type</var> can be one of:</p>
258
259     <dl>
260       <dt><code>Input</code></dt>
261       <dd>Store the byte count of the filter's input stream in the note.</dd>
262
263       <dt><code>Output</code></dt>
264       <dd>Store the byte count of the filter's output stream in the note.</dd>
265
266       <dt><code>Ratio</code></dt>
267       <dd>Store the compression ratio (<code>output/input * 100</code>)
268       in the note. This is the default, if the <var>type</var> argument
269       is omitted.</dd>
270     </dl>
271
272     <p>Thus you may log it this way:</p>
273
274     <example><title>Accurate Logging</title>
275     <highlight language="config">
276 DeflateFilterNote Input instream
277 DeflateFilterNote Output outstream
278 DeflateFilterNote Ratio ratio
279
280 LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
281 CustomLog "logs/deflate_log" deflate
282 </highlight>
283     </example>
284 </usage>
285 <seealso><module>mod_log_config</module></seealso>
286 </directivesynopsis>
287
288 <directivesynopsis>
289 <name>DeflateBufferSize</name>
290 <description>Fragment size to be compressed at one time by zlib</description>
291 <syntax>DeflateBufferSize <var>value</var></syntax>
292 <default>DeflateBufferSize 8096</default>
293 <contextlist><context>server config</context><context>virtual host</context>
294 </contextlist>
295
296 <usage>
297     <p>The <directive>DeflateBufferSize</directive> directive specifies
298     the size in bytes of the fragments that zlib should compress at one
299     time. If the compressed response size is bigger than the one specified
300     by this directive then httpd will switch to chunked encoding 
301     (HTTP header <code>Transfer-Encoding</code> set to <code>Chunked</code>), with the
302     side effect of not setting any <code>Content-Length</code> HTTP header. This is particularly 
303     important when httpd works behind reverse caching proxies or when httpd is configured with 
304     <module>mod_cache</module> and <module>mod_cache_disk</module> because
305     HTTP responses without any <code>Content-Length</code> header might not be cached.
306   </p>
307 </usage>
308 </directivesynopsis>
309
310 <directivesynopsis>
311 <name>DeflateWindowSize</name>
312 <description>Zlib compression window size</description>
313 <syntax>DeflateWindowSize <var>value</var></syntax>
314 <default>DeflateWindowSize 15</default>
315 <contextlist><context>server config</context><context>virtual host</context>
316 </contextlist>
317
318 <usage>
319     <p>The <directive>DeflateWindowSize</directive> directive specifies the
320     zlib compression window size (a value between 1 and 15). Generally, the
321     higher the window size, the higher can the compression ratio be expected.</p>
322 </usage>
323 </directivesynopsis>
324
325 <directivesynopsis>
326
327 <name>DeflateMemLevel</name>
328 <description>How much memory should be used by zlib for compression</description>
329 <syntax>DeflateMemLevel <var>value</var></syntax>
330 <default>DeflateMemLevel 9</default>
331 <contextlist><context>server config</context><context>virtual host</context>
332 </contextlist>
333
334 <usage>
335     <p>The <directive>DeflateMemLevel</directive> directive specifies
336     how much memory should be used by zlib for compression
337     (a value between 1 and 9).</p>
338 </usage>
339 </directivesynopsis>
340
341 <directivesynopsis>
342 <name>DeflateCompressionLevel</name>
343 <description>How much compression do we apply to the output</description>
344 <syntax>DeflateCompressionLevel <var>value</var></syntax>
345 <default>Zlib's default</default>
346 <contextlist><context>server config</context><context>virtual host</context>
347 </contextlist>
348
349 <usage>
350     <p>The <directive>DeflateCompressionLevel</directive> directive specifies
351         what level of compression should be used, the higher the value,
352         the better the compression, but the more CPU time is required to
353         achieve this.</p>
354     <p>The value must between 1 (less compression) and 9 (more compression).</p>
355 </usage>
356 </directivesynopsis>
357
358 <directivesynopsis>
359 <name>DeflateAlterETag</name>
360 <description>How the outgoing ETag header should be modified during compression</description>
361 <syntax>DeflateAlterETag AddSuffix|NoChange|Remove</syntax>
362 <default>DeflateAlterETag AddSuffix</default>
363 <contextlist><context>server config</context><context>virtual host</context>
364 </contextlist>
365
366 <usage>
367     <p>The <directive>DeflateAlterETag</directive> directive specifies
368     how the ETag hader should be altered when a response is compressed.</p>
369     <dl>
370     <dt>AddSuffix</dt>
371     <dd><p>Append the compression method onto the end of the ETag, causing
372         compressed and uncompressed representations to have unique ETags.
373         This has been the default since 2.4.0, but prevents serving
374         "HTTP Not Modified" (304) responses to conditional requests for
375         compressed content.</p></dd>
376     <dt>NoChange</dt>
377     <dd><p>Don't change the ETag on a compressed response. This was the default
378         prior to 2.4.0, but does not satisfy the HTTP/1.1 property that all
379         representations of the same resource have unique ETags. </p></dd>
380     <dt>Remove</dt>
381     <dd><p>Remove the ETag header from compressed responses. This prevents
382         some conditional requests from being possible, but avoids the
383         shortcomings of the preceding options.  </p></dd>
384     </dl>
385 </usage>
386 </directivesynopsis>
387
388 <directivesynopsis>
389 <name>DeflateInflateLimitRequestBody</name>
390 <description>Maximum size of inflated request bodies</description>
391 <syntax>DeflateInflateLimitRequestBody<var>value</var></syntax>
392 <default>None, but LimitRequestBody applies after deflation</default>
393 <contextlist><context>server config</context><context>virtual host</context>
394 <context>directory</context><context>.htaccess</context></contextlist>
395 <override>All</override>
396 <compatibility>2.4.10 and later</compatibility>
397
398 <usage>
399     <p>The <directive>DeflateInflateLimitRequestBody</directive> directive
400         specifies the maximum size of an inflated request body. If it is unset,
401         <directive module="core">LimitRequestBody</directive> is applied to the
402         inflated body.</p>
403 </usage>
404 </directivesynopsis>
405
406 <directivesynopsis>
407 <name>DeflateInflateRatioLimit</name>
408 <description>Maximum inflation ratio for request bodies</description>
409 <syntax>DeflateInflateRatioLimit <var>value</var></syntax>
410 <default>200</default>
411 <contextlist><context>server config</context><context>virtual host</context>
412 <context>directory</context><context>.htaccess</context></contextlist>
413 <override>All</override>
414 <compatibility>2.4.10 and later</compatibility>
415
416 <usage>
417     <p>The <directive>DeflateInflateRatioLimit</directive> directive
418         specifies the maximum ratio of deflated to inflated size of an
419         inflated request body. This ratio is checked as the body is
420         streamed in, and if crossed more than
421         <directive>DeflateInflateRatioBurst</directive> times, the request
422         will be terminated.</p>
423 </usage>
424 </directivesynopsis>
425
426 <directivesynopsis>
427 <name>DeflateInflateRatioBurst</name>
428 <description>Maximum number of times the inflation ratio for request bodies
429              can be crossed</description>
430 <syntax>DeflateInflateRatioBurst <var>value</var></syntax>
431 <default>3</default>
432 <contextlist><context>server config</context><context>virtual host</context>
433 <context>directory</context><context>.htaccess</context></contextlist>
434 <override>All</override>
435 <compatibility>2.4.10 and later</compatibility>
436
437 <usage>
438     <p>The <directive>DeflateInflateRatioBurst</directive> directive
439        specifies the maximum number of times the
440        <directive>DeflateInflateRatioLimit</directive> can be crossed before
441        terminating the request.</p>
442 </usage>
443 </directivesynopsis>
444
445 </modulesynopsis>