Mod Deflate Rules
Luke Hebert
- 2 minutes read - 324 wordsCompressing text-based responses before they hit the wire is one of the quickest wins you can make on an Apache server. A single HTML page or CSS file that is 200 KB uncompressed often drops to 30-40 KB after DEFLATE, which means faster load times and less bandwidth for your users.
Enabling Compression
The mod_deflate module handles the actual compression, but you still need to tell Apache which MIME types to compress. The AddOutputFilterByType directive does exactly that. Add the following lines to your virtual host or server configuration:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascriptVerifying It Works
After reloading Apache, you can confirm compression is active by checking the response headers:
curl -I -H "Accept-Encoding: deflate" http://your-site.com/style.cssLook for Content-Encoding: deflate in the response. If it is missing, double-check that mod_deflate is loaded (run apachectl -M | grep deflate) and that no SetOutputFilter or FilterChain directive is overriding your rules.
Browsers and modern HTTP clients negotiate compression via the Accept-Encoding header. Apache will only apply DEFLATE when the client explicitly requests it, so you do not need to worry about sending compressed data to clients that cannot decode it.
A Few Practical Tips
- Do not compress already-compressed formats such as JPEG, PNG, or ZIP files. They will not shrink further and the CPU cost is wasted.
- Keep the list specific. Using a blanket
AddOutputFilterByType DEFLATE */*can cause unexpected behavior with binary payloads. - Test after changes. A misconfigured filter chain can silently disable compression for all types, so verify with
curlor your browser dev tools after every edit.
That is the core of it: load the module, list the types you want compressed, and confirm the headers. From there you can layer on mod_pagespeed or a CDN for additional gains, but a clean DEFLATE setup is the foundation.