2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Copyright 2002-2006 The Apache Software Foundation or its licensors, as
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
14 http://www.apache.org/licenses/LICENSE-2.0
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.
23 <modulesynopsis metafile="mod_deflate.xml.meta">
25 <name>mod_deflate</name>
26 <description>Compress content before it is delivered to the
28 <status>Extension</status>
29 <sourcefile>mod_deflate.c</sourcefile>
30 <identifier>deflate_module</identifier>
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
38 <seealso><a href="../filter.html">Filters</a></seealso>
40 <section id="recommended"><title>Sample Configurations</title>
41 <p>This is a simple sample configuration for the impatient.</p>
43 <example><title>Compress only a few types</title>
44 AddOutputFilterByType DEFLATE text/html text/plain text/xml
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>
51 <example><title>Compress everything except images</title>
52 <Location /><br />
55 SetOutputFilter DEFLATE<br />
57 # Netscape 4.x has some problems...<br />
58 BrowserMatch ^Mozilla/4 gzip-only-text/html<br />
60 # Netscape 4.06-4.08 have some more problems<br />
61 BrowserMatch ^Mozilla/4\.0[678] no-gzip<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 />
68 \.(?:gif|jpe?g|png)$ no-gzip dont-vary<br />
71 # Make sure proxies don't deliver the wrong content<br />
72 Header append Vary User-Agent env=!dont-vary<br />
79 <section id="enable"><title>Enabling Compression</title>
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
88 SetOutputFilter DEFLATE
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
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
104 <Directory "/your-server-root/manual"><br />
106 AddOutputFilterByType DEFLATE text/html<br />
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>
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
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>
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>
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.
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.
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>
163 <Location /dav-area><br />
165 ProxyPass http://example.com/<br />
166 SetOutputFilter INFLATE<br />
171 <p>This Example will uncompress gzip'ed output from example.com, so other
172 filters can do further processing with it.
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>
185 <Location /dav-area><br />
187 SetInputFilter DEFLATE<br />
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>
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>
209 <section id="proxies"><title>Dealing with proxy servers</title>
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>
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>
226 Header append Vary User-Agent
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>
234 <example><title>Example</title>
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>
245 <compatibility><var>type</var> is available since Apache 2.0.45</compatibility>
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"
255 <example><title>Example</title>
256 DeflateFilterNote ratio<br />
258 LogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' deflate<br />
259 CustomLog logs/deflate_log deflate
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>
267 <dt><code>Input</code></dt>
268 <dd>Store the byte count of the filter's input stream in the note.</dd>
270 <dt><code>Output</code></dt>
271 <dd>Store the byte count of the filter's output stream in the note.</dd>
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
279 <p>Thus you may log it this way:</p>
281 <example><title>Accurate Logging</title>
282 DeflateFilterNote Input instream<br />
283 DeflateFilterNote Output outstream<br />
284 DeflateFilterNote Ratio ratio<br />
286 LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate<br />
287 CustomLog logs/deflate_log deflate
290 <seealso><module>mod_log_config</module></seealso>
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>
302 <p>The <directive>DeflateBufferSize</directive> directive specifies
303 the size in bytes of the fragments that zlib should compress at one
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>
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>
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>
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>
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>
346 <compatibility>This directive is available since Apache 2.0.45</compatibility>
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
353 <p>The value must between 1 (less compression) and 9 (more compression).</p>