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