<-
Apache > HTTP Server > Documentation > Modules

Apache Module mod_deflate 4d1b2r

Available Languages:  ko 

Description: Compress content before it is delivered to the client
Status: Extension
Module Identifier: deflate_module
Source File: mod_deflate.c

Summary 6m2os

The mod_deflate module provides the DEFLATE output filter that allows output from your server to be compressed before being sent to the client over the network.

 Apache!

Topics 2c136y

Directives 1a4l6m

Bugfix checklist 4i2533

See also 27136x

top

ed Encodings 3y3c39

The gzip encoding is the only one ed to ensure complete compatibility with old browser implementations. The deflate encoding is not ed, please check the zlib's documentation for a complete explanation.

top

Sample Configurations 22605h

Compression and TLS b5b3u

Some web applications are vulnerable to an information disclosure attack when a TLS connection carries deflate compressed data. For more information, review the details of the "BREACH" family of attacks.

This is a simple configuration that compresses common text-based content types.

Compress only a few types 3e37i

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
top

Enabling Compression 4s1as

Compression and TLS b5b3u

Some web applications are vulnerable to an information disclosure attack when a TLS connection carries deflate compressed data. For more information, review the details of the "BREACH" family of attacks.

Output Compression 6b3846

Compression is implemented by the DEFLATE filter. The following directive will enable compression for documents in the container where it is placed:

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip

If you want to restrict the compression to particular MIME types in general, you may use the AddOutputFilterByType directive. Here is an example of enabling compression only for the html files of the Apache documentation:

<Directory "/your-server-root/manual">
    AddOutputFilterByType DEFLATE text/html
</Directory>

Note 196q6v

The DEFLATE filter is always inserted after RESOURCE filters like PHP or SSI. It never touches internal subrequests.

Note 196q6v

There is an environment variable force-gzip, set via SetEnv, which will ignore the accept-encoding setting of your browser and will send compressed output.

Output Decompression 2y5b20

The AddOutputFilter, for example:

<Location "/dav-area">
    Proxy "http://example.com/"
    SetOutputFilter INFLATE
</Location>

This Example will uncompress gzip'ed output from example.com, so other filters can do further processing with it.

Input Decompression 175s2b

The AddInputFilter, for example:

<Location "/dav-area">
    SetInputFilter DEFLATE
</Location>

Now if a request contains a Content-Encoding: gzip header, the body will be automatically decompressed. Few browsers have the ability to gzip request bodies. However, some special applications actually do request compression, for instance some WebDAV clients.

Note on Content-Length 5jn3c

If you evaluate the request body yourself, don't trust the Content-Length header! The Content-Length header reflects the length of the incoming data from the client and not the byte count of the decompressed data stream.

top

Dealing with proxy servers 4a5k2k

The mod_deflate module sends a Vary: Accept-Encoding HTTP response header to alert proxies that a cached response should be sent only to clients that send the appropriate Accept-Encoding request header. This prevents compressed content from being sent to a client that will not understand it.

If you use some special exclusions dependent on, for example, the -Agent header, you must manually configure an addition to the Vary header to alert proxies of the additional restrictions. For example, in a typical configuration where the addition of the DEFLATE filter depends on the -Agent, you should add:

Header append Vary -Agent

If your decision about compression depends on other information than request headers (e.g. HTTP version), you have to set the Vary header to the value *. This prevents compliant proxies from caching entirely.

Example 5z1g6h

Header set Vary *
top

Serving pre-compressed content 674q57

Since mod_deflate to serve them without re-compressing them. This may be accomplished using a configuration like the following:

<IfModule mod_headers.c>
    # Serve gzip compressed CSS and JS files if they exist
    # and the client accepts gzip.
    RewriteCond "%{HTTP:Accept-encoding}" "gzip"
    RewriteCond "%{REQUEST_FILENAME}\.gz" -s
    RewriteRule "^(.*)\.(css|js)"         "$1\.$2\.gz" [QSA]

    # Serve correct content types, and prevent mod_deflate double gzip.
    RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
    RewriteRule "\.js\.gz$"  "-" [T=text/javascript,E=no-gzip:1]


    <FilesMatch "(\.js\.gz|\.css\.gz)$">
      # Serve correct encoding type.
      Header append Content-Encoding gzip

      # Force proxies to cache gzipped &
      # non-gzipped css/js files separately.
      Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>
top

DeflateBufferSize Directive 72a2l

Description: Fragment size to be compressed at one time by zlib
Syntax: DeflateBufferSize value
Default: DeflateBufferSize 8096
Context: server config, virtual host
Status: Extension
Module: mod_deflate

The DeflateBufferSize directive specifies the size in bytes of the fragments that zlib should compress at one time. If the compressed response size is bigger than the one specified by this directive then httpd will switch to chunked encoding (HTTP header Transfer-Encoding set to Chunked), with the side effect of not setting any Content-Length HTTP header. This is particularly important when httpd works behind reverse caching proxies or when httpd is configured with mod_cache_disk because HTTP responses without any Content-Length header might not be cached.

top

DeflateCompressionLevel Directive 2a553y

Description: How much compression do we apply to the output
Syntax: DeflateCompressionLevel value
Default: Zlib's default
Context: server config, virtual host
Status: Extension
Module: mod_deflate

The DeflateCompressionLevel directive specifies what level of compression should be used, the higher the value, the better the compression, but the more U time is required to achieve this.

The value must between 1 (less compression) and 9 (more compression).

top

DeflateFilterNote Directive 2715k

Description: Places the compression ratio in a note for logging
Syntax: DeflateFilterNote [type] notename
Context: server config, virtual host
Status: Extension
Module: mod_deflate

The DeflateFilterNote directive specifies that a note about compression ratios should be attached to the request. The name of the note is the value specified for the directive. You can use that note for statistical purposes by adding the value to your access log.

Example 5z1g6h

DeflateFilterNote ratio

LogFormat '"%r" %b (%{ratio}n) "%{-agent}i"' deflate
CustomLog "logs/deflate_log" deflate

If you want to extract more accurate values from your logs, you can use the type argument to specify the type of data left as a note for logging. type can be one of:

Input
Store the byte count of the filter's input stream in the note.
Output
Store the byte count of the filter's output stream in the note.
Ratio
Store the compression ratio (output/input * 100) in the note. This is the default, if the type argument is omitted.

Thus you may log it this way:

Accurate Logging 6v4y48

DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio

LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog "logs/deflate_log" deflate

See also 27136x

top

DeflateInflateLimitRequestBody Directive 1l6l5s

Description: Maximum size of inflated request bodies
Syntax: DeflateInflateLimitRequestBody value
Default: None, but LimitRequestBody applies after deflation
Context: server config, virtual host, directory, .htaccess
Status: Extension
Module: mod_deflate
Compatibility: 2.4.10 and later

The DeflateInflateLimitRequestBody directive specifies the maximum size of an inflated request body. If it is unset, LimitRequestBody is applied to the inflated body.

top

DeflateInflateRatioBurst Directive 365d46

Description: Maximum number of times the inflation ratio for request bodies can be crossed
Syntax: DeflateInflateRatioBurst value
Default: DeflateInflateRatioBurst 3
Context: server config, virtual host, directory, .htaccess
Status: Extension
Module: mod_deflate
Compatibility: 2.4.10 and later

The DeflateInflateRatioBurst directive specifies the maximum number of times the DeflateInflateRatioLimit can be crossed before terminating the request.

top

DeflateInflateRatioLimit Directive 3u4g3k

Description: Maximum inflation ratio for request bodies
Syntax: DeflateInflateRatioLimit value
Default: DeflateInflateRatioLimit 200
Context: server config, virtual host, directory, .htaccess
Status: Extension
Module: mod_deflate
Compatibility: 2.4.10 and later

The DeflateInflateRatioLimit directive specifies the maximum ratio of deflated to inflated size of an inflated request body. This ratio is checked as the body is streamed in, and if crossed more than DeflateInflateRatioBurst times, the request will be terminated.

top

DeflateMemLevel Directive 4r325l

Description: How much memory should be used by zlib for compression
Syntax: DeflateMemLevel value
Default: DeflateMemLevel 9
Context: server config, virtual host
Status: Extension
Module: mod_deflate

The DeflateMemLevel directive specifies how much memory should be used by zlib for compression (a value between 1 and 9).

top

DeflateWindowSize Directive 4a3h3h

Description: Zlib compression window size
Syntax: DeflateWindowSize value
Default: DeflateWindowSize 15
Context: server config, virtual host
Status: Extension
Module: mod_deflate

The DeflateWindowSize directive specifies the zlib compression window size (a value between 1 and 15). Generally, the higher the window size, the higher can the compression ratio be expected.

Available Languages:  ko 

top

Comments 2p1l6j

Notice:
This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed by our s if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Libera.chat, or sent to our mailing lists.