2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
6 Copyright 2002-2004 The Apache Software Foundation
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
12 http://www.apache.org/licenses/LICENSE-2.0
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.
21 <modulesynopsis metafile="mod_deflate.xml.meta">
23 <name>mod_deflate</name>
24 <description>Compress content before it is delivered to the
26 <status>Extension</status>
27 <sourcefile>mod_deflate.c</sourcefile>
28 <identifier>deflate_module</identifier>
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
36 <seealso><a href="../filter.html">Filters</a></seealso>
38 <section id="recommended"><title>Sample Configurations</title>
39 <p>This is a simple sample configuration for the impatient.</p>
41 <example><title>Compress only a few types</title>
42 AddOutputFilterByType DEFLATE text/html text/plain text/xml
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>
49 <example><title>Compress everything except images</title>
50 <Location /><br />
53 SetOutputFilter DEFLATE<br />
55 # Netscape 4.x has some problems...<br />
56 BrowserMatch ^Mozilla/4 gzip-only-text/html<br />
58 # Netscape 4.06-4.08 have some more problems<br />
59 BrowserMatch ^Mozilla/4\.0[678] no-gzip<br />
61 # MSIE masquerades as Netscape, but it is fine<br />
62 # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html<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 />
69 # Don't compress images<br />
70 SetEnvIfNoCase Request_URI \<br />
72 \.(?:gif|jpe?g|png)$ no-gzip dont-vary<br />
75 # Make sure proxies don't deliver the wrong content<br />
76 Header append Vary User-Agent env=!dont-vary<br />
83 <section id="enable"><title>Enabling Compression</title>
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
92 SetOutputFilter DEFLATE
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
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
108 <Directory "/your-server-root/manual"><br />
110 AddOutputFilterByType DEFLATE text/html<br />
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>
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
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>
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>
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.
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>
161 <Location /dav-area><br />
163 SetInputFilter DEFLATE<br />
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>
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>
185 <section id="proxies"><title>Dealing with proxy servers</title>
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>
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>
202 Header append Vary User-Agent
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>
210 <example><title>Example</title>
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>
221 <compatibility><var>type</var> is available since Apache 2.0.45</compatibility>
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"
231 <example><title>Example</title>
232 DeflateFilterNote ratio<br />
234 LogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' deflate<br />
235 CustomLog logs/deflate_log deflate
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>
243 <dt><code>Input</code></dt>
244 <dd>Store the byte count of the filter's input stream in the note.</dd>
246 <dt><code>Output</code></dt>
247 <dd>Store the byte count of the filter's output stream in the note.</dd>
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
255 <p>Thus you may log it this way:</p>
257 <example><title>Accurate Logging</title>
258 DeflateFilterNote Input instream<br />
259 DeflateFilterNote Output outstream<br />
260 DeflateFilterNote Ratio ratio<br />
262 LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate<br />
263 CustomLog logs/deflate_log deflate
266 <seealso><module>mod_log_config</module></seealso>
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>
278 <p>The <directive>DeflateBufferSize</directive> directive specifies
279 the size in bytes of the fragments that zlib should compress at one
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>
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>
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>
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>
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>
322 <compatibility>This directive is available since Apache 2.0.45</compatibility>
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
329 <p>The value must between 1 (less compression) and 9 (more compression).</p>