Here is how I'm using Apache to compress my site content. Pretty simple but you do have to watch out for missing
Content-Length headers, and incorrect handling of
Accept-Encoding requests due to
Deflate (stylized as DEFLATE) is a lossless data compression that uses a combination of LZ77 and Huffman coding. It was designed by Phil Katz, for version 2 of his PKZIP archiving tool. Deflate was later specified in RFC 1951 (1996).
Since mod_deflate re-compresses content each time a request is made, you can pre-compress the files and then have mod_deflate (or just serve normally using mod_headers) send those instead of having to recompress. This may be accomplished using a configuration like the following:
Here is how the Google PageSpeed module uses filters:
There are a couple environment variables you can set with SetEnv or RewriteRule that control a couple aspects of compression.
- If you have the DEFLATE filter activated, this environment variable will ignore the accept-encoding setting of your browser and will send compressed output unconditionally.
- This causes any Vary fields to be removed from the response header before it is sent back to the client. Some clients don't interpret this field correctly; setting this variable can work around this problem. Setting this variable also implies force-response-1.0.
- This forces an HTTP/1.0 response to clients making an HTTP/1.0 request. It was originally implemented as a result of a problem with AOL's proxies. Some HTTP/1.0 clients may not behave correctly when given an HTTP/1.1 response, and this can be used to interoperate with them.
- When set to a value of "1", this variable disables the DEFLATE output filter provided by mod_deflate for content-types other than text/html. If you'd rather use statically compressed files, mod_negotiation evaluates the variable as well (not only for gzip, but for all encodings that differ from "identity").
- When set, the DEFLATE filter of mod_deflate will be turned off and mod_negotiation will refuse to deliver encoded resources.
- Disables brotli compression even if the client supports it.
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
User-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 User-Agent, you should add:
Header append Vary User-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.
Header set Vary *
You'll need to change the Header directives to use the
always in order to have them show up in some cases. For example if you are using mod_proxy_fcgi with php-fpm, or some other cgi.
Header always append Cache-Control "no-transform" env=!no-gzip
- Data Compression Stanford
- Understanding zlib
- Serving WebP images for PNG and JPG files
- Using filters in Apache and mod_filter (mod_filter normally only runs filters on responses with HTTP status
- ZLIB Compressed Data Format Specification version 3.3
- ZLIB Source Code
- RFC 1950: zlib compressed data format
- RFC 1951: deflate compressed data format
- RFC 1952: gzip file format