]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_deflate.xml
Update the copyright year in all .c, .h and .xml files
[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  Copyright 2002-2006 The Apache Software Foundation or its licensors, as
8  applicable.
9
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
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="recommended"><title>Sample Configurations</title>
41     <p>This is a simple sample configuration for the impatient.</p>
42
43     <example><title>Compress only a few types</title>
44       AddOutputFilterByType DEFLATE text/html text/plain text/xml
45     </example>
46
47     <p>The following configuration, while resulting in more compressed content,
48     is also much more complicated.  Do not use this unless you fully understand
49     all the configuration details.</p>
50
51     <example><title>Compress everything except images</title>
52       &lt;Location /&gt;<br />
53       <indent>
54         # Insert filter<br />
55         SetOutputFilter DEFLATE<br />
56         <br />
57         # Netscape 4.x has some problems...<br />
58         BrowserMatch ^Mozilla/4         gzip-only-text/html<br />
59         <br />
60         # Netscape 4.06-4.08 have some more problems<br />
61         BrowserMatch ^Mozilla/4\.0[678] no-gzip<br />
62         <br />
63         # MSIE masquerades as Netscape, but it is fine<br />
64         BrowserMatch \bMSIE             !no-gzip !gzip-only-text/html<br />
65         # Don't compress images<br />
66         SetEnvIfNoCase Request_URI \<br />
67         <indent>
68           \.(?:gif|jpe?g|png)$ no-gzip dont-vary<br />
69         </indent>
70         <br />
71         # Make sure proxies don't deliver the wrong content<br />
72         Header append Vary User-Agent env=!dont-vary<br />
73       </indent>
74       &lt;/Location&gt;
75     </example>
76
77 </section>
78
79 <section id="enable"><title>Enabling Compression</title>
80
81     <section id="output"><title>Output Compression</title>
82       <p>Compression is implemented by the <code>DEFLATE</code>
83       <a href="../filter.html">filter</a>. The following directive
84       will enable compression for documents in the container where it
85       is placed:</p>
86
87       <example>
88         SetOutputFilter DEFLATE
89       </example>
90
91       <p>Some popular browsers cannot handle compression of all content
92       so you may want to set the <code>gzip-only-text/html</code> note to
93       <code>1</code> to only allow html files to be compressed (see
94       below). If you set this to <em>anything but <code>1</code></em> it
95       will be ignored.</p>
96       
97       <p>If you want to restrict the compression to particular MIME types
98       in general, you may use the <directive module="core"
99       >AddOutputFilterByType</directive> directive. Here is an example of
100       enabling compression only for the html files of the Apache
101       documentation:</p>
102
103       <example>
104         &lt;Directory "/your-server-root/manual"&gt;<br />
105         <indent>
106           AddOutputFilterByType DEFLATE text/html<br />
107         </indent>
108         &lt;/Directory&gt;
109       </example>
110
111       <p>For browsers that have problems even with compression of all file
112       types, use the <directive module="mod_setenvif"
113       >BrowserMatch</directive> directive to set the <code>no-gzip</code>
114       note for that particular browser so that no compression will be
115       performed. You may combine <code>no-gzip</code> with <code
116       >gzip-only-text/html</code> to get the best results. In that case
117       the former overrides the latter. Take a look at the following
118       excerpt from the <a href="#recommended">configuration example</a>
119       defined in the section above:</p>
120
121       <example>
122         BrowserMatch ^Mozilla/4         gzip-only-text/html<br />
123         BrowserMatch ^Mozilla/4\.0[678] no-gzip<br />
124         BrowserMatch \bMSIE             !no-gzip !gzip-only-text/html
125       </example>
126
127       <p>At first we probe for a <code>User-Agent</code> string that
128       indicates a Netscape Navigator version of 4.x. These versions
129       cannot handle compression of types other than
130       <code>text/html</code>. The versions 4.06, 4.07 and 4.08 also
131       have problems with decompressing html files. Thus, we completely
132       turn off the deflate filter for them.</p>
133
134       <p>The third <directive module="mod_setenvif">BrowserMatch</directive>
135       directive fixes the guessed identity of the user agent, because
136       the Microsoft Internet Explorer identifies itself also as "Mozilla/4"
137       but is actually able to handle requested compression. Therefore we
138       match against the additional string "MSIE" (<code>\b</code> means
139       "word boundary") in the <code>User-Agent</code> Header and turn off
140       the restrictions defined before.</p>
141
142       <note><title>Note</title>
143         The <code>DEFLATE</code> filter is always inserted after RESOURCE
144         filters like PHP or SSI. It never touches internal subrequests.
145       </note>
146       <note><title>Note</title>
147         There is a environment variable <code>force-gzip</code>,
148         set via <directive module="core">SetEnv</directive>, which
149         will ignore the accept-encoding setting of your browser and will
150         send compressed output.
151       </note>
152
153     </section>
154     <section id="inflate"><title>Output Decompression</title>
155       <p>The <module>mod_deflate</module> module also provides a filter for
156       inflating/uncompressing a gzip compressed response body. In order to activate
157       this feature you have to insert the <code>INFLATE</code> filter into
158       the outputfilter chain using <directive module="core"
159       >SetOutputFilter</directive> or <directive module="mod_mime"
160       >AddOutputFilter</directive>, for example:</p>
161
162       <example>
163         &lt;Location /dav-area&gt;<br />
164         <indent>
165           ProxyPass http://example.com/<br />
166           SetOutputFilter INFLATE<br />
167         </indent>
168         &lt;/Location&gt;
169       </example>
170
171       <p>This Example will uncompress gzip'ed output from example.com, so other
172       filters can do further processing with it.
173       </p>
174       
175     </section>
176     <section id="input"><title>Input Decompression</title>
177       <p>The <module>mod_deflate</module> module also provides a filter for
178       decompressing a gzip compressed request body . In order to activate
179       this feature you have to insert the <code>DEFLATE</code> filter into
180       the input filter chain using <directive module="core"
181       >SetInputFilter</directive> or <directive module="mod_mime"
182       >AddInputFilter</directive>, for example:</p>
183
184       <example>
185         &lt;Location /dav-area&gt;<br />
186         <indent>
187           SetInputFilter DEFLATE<br />
188         </indent>
189         &lt;/Location&gt;
190       </example>
191       
192       <p>Now if a request contains a <code>Content-Encoding:
193       gzip</code> header, the body will be automatically decompressed.
194       Few browsers have the ability to gzip request bodies. However,
195       some special applications actually do support request
196       compression, for instance some <a
197       href="http://www.webdav.org">WebDAV</a> clients.</p>
198
199       <note type="warning"><title>Note on Content-Length</title>
200         <p>If you evaluate the request body yourself, <em>don't trust
201         the <code>Content-Length</code> header!</em>
202         The Content-Length header reflects the length of the
203         incoming data from the client and <em>not</em> the byte count of
204         the decompressed data stream.</p>
205       </note>
206     </section>
207 </section>
208
209 <section id="proxies"><title>Dealing with proxy servers</title>
210
211     <p>The <module>mod_deflate</module> module sends a <code>Vary:
212     Accept-Encoding</code> HTTP response header to alert proxies that
213     a cached response should be sent only to clients that send the
214     appropriate <code>Accept-Encoding</code> request header.  This
215     prevents compressed content from being sent to a client that will
216     not understand it.</p>
217
218     <p>If you use some special exclusions dependent
219     on, for example, the <code>User-Agent</code> header, you must 
220     manually configure an addition to the <code>Vary</code> header
221     to alert proxies of the additional restrictions.  For example,
222     in a typical configuration where the addition of the <code>DEFLATE</code>
223     filter depends on the <code>User-Agent</code>, you should add:</p>
224
225     <example>
226       Header append Vary User-Agent
227     </example>
228     
229     <p>If your decision about compression depends on other information
230     than request headers (<em>e.g.</em> HTTP version), you have to set the
231     <code>Vary</code> header to the value <code>*</code>. This prevents
232     compliant proxies from caching entirely.</p>
233
234     <example><title>Example</title>
235       Header set Vary *
236     </example>
237 </section>
238
239 <directivesynopsis>
240 <name>DeflateFilterNote</name>
241 <description>Places the compression ratio in a note for logging</description>
242 <syntax>DeflateFilterNote [<var>type</var>] <var>notename</var></syntax>
243 <contextlist><context>server config</context><context>virtual host</context>
244 </contextlist>
245 <compatibility><var>type</var> is available since Apache 2.0.45</compatibility>
246
247 <usage>
248     <p>The <directive>DeflateFilterNote</directive> directive
249     specifies that a note about compression ratios should be attached
250     to the request. The name of the note is the value specified for
251     the directive. You can use that note for statistical purposes by
252     adding the value to your <a href="../logs.html#accesslog"
253     >access log</a>.</p>
254
255     <example><title>Example</title>
256       DeflateFilterNote ratio<br />
257       <br />
258       LogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' deflate<br />
259       CustomLog logs/deflate_log deflate
260     </example>
261
262     <p>If you want to extract more accurate values from your logs, you
263     can use the <var>type</var> argument to specify the type of data
264     left as note for logging. <var>type</var> can be one of:</p>
265
266     <dl>
267       <dt><code>Input</code></dt>
268       <dd>Store the byte count of the filter's input stream in the note.</dd>
269
270       <dt><code>Output</code></dt>
271       <dd>Store the byte count of the filter's output stream in the note.</dd>
272
273       <dt><code>Ratio</code></dt>
274       <dd>Store the compression ratio (<code>output/input * 100</code>)
275       in the note. This is the default, if the <var>type</var> argument
276       is omitted.</dd>
277     </dl>
278
279     <p>Thus you may log it this way:</p>
280
281     <example><title>Accurate Logging</title>
282       DeflateFilterNote Input instream<br />
283       DeflateFilterNote Output outstream<br />
284       DeflateFilterNote Ratio ratio<br />
285       <br />
286       LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate<br />
287       CustomLog logs/deflate_log deflate
288     </example>
289 </usage>
290 <seealso><module>mod_log_config</module></seealso>
291 </directivesynopsis>
292
293 <directivesynopsis>
294 <name>DeflateBufferSize</name>
295 <description>Fragment size to be compressed at one time by zlib</description>
296 <syntax>DeflateBufferSize <var>value</var></syntax>
297 <default>DeflateBufferSize 8096</default>
298 <contextlist><context>server config</context><context>virtual host</context>
299 </contextlist>
300
301 <usage>
302     <p>The <directive>DeflateBufferSize</directive> directive specifies
303     the size in bytes of the fragments that zlib should compress at one
304     time.</p>
305 </usage>
306 </directivesynopsis>
307
308 <directivesynopsis>
309 <name>DeflateWindowSize</name>
310 <description>Zlib compression window size</description>
311 <syntax>DeflateWindowSize <var>value</var></syntax>
312 <default>DeflateWindowSize 15</default>
313 <contextlist><context>server config</context><context>virtual host</context>
314 </contextlist>
315
316 <usage>
317     <p>The <directive>DeflateWindowSize</directive> directive specifies the
318     zlib compression window size (a value between 1 and 15). Generally, the
319     higher the window size, the higher can the compression ratio be expected.</p>
320 </usage>
321 </directivesynopsis>
322
323 <directivesynopsis>
324
325 <name>DeflateMemLevel</name>
326 <description>How much memory should be used by zlib for compression</description>
327 <syntax>DeflateMemLevel <var>value</var></syntax>
328 <default>DeflateMemLevel 9</default>
329 <contextlist><context>server config</context><context>virtual host</context>
330 </contextlist>
331
332 <usage>
333     <p>The <directive>DeflateMemLevel</directive> directive specifies
334     how much memory should be used by zlib for compression
335     (a value between 1 and 9).</p>
336 </usage>
337 </directivesynopsis>
338
339 <directivesynopsis>
340 <name>DeflateCompressionLevel</name>
341 <description>How much compression do we apply to the output</description>
342 <syntax>DeflateCompressionLevel <var>value</var></syntax>
343 <default>Zlib's default</default>
344 <contextlist><context>server config</context><context>virtual host</context>
345 </contextlist>
346 <compatibility>This directive is available since Apache 2.0.45</compatibility>
347
348 <usage>
349     <p>The <directive>DeflateCompressionLevel</directive> directive specifies
350         what level of compression should be used, the higher the value, 
351         the better the compression, but the more CPU time is required to
352         achieve this.</p>
353     <p>The value must between 1 (less compression) and 9 (more compression).</p>
354 </usage>
355 </directivesynopsis>
356
357
358 </modulesynopsis>
359