Direct Answer
Yes, you can prevent double compression by excluding binary MIME types from mod_deflate. The most reliable method is to use SetEnvIfNoCase to set the no-gzip environment variable for known binary types, or to whitelist only text types with AddOutputFilterByType. The latter is simpler and avoids maintaining an exclusion list, but it requires you to list every text MIME type you serve.
Why Double Compression Happens
mod_deflate compresses any response whose MIME type matches its filter rules, regardless of whether the content is already compressed (e.g., JPEG, PNG, ZIP). If a client sends Accept-Encoding: gzip, Apache will gzip the binary again, wasting CPU and potentially increasing file size or causing corruption in rare cases.
Recommended Configuration (Apache 2.4.x)
Whitelist text types explicitly, which implicitly excludes all binaries:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json application/xml
If you prefer an exclusion list, use SetEnvIfNoCase to mark binary types:
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|pdf|zip|gz|mp4|mp3|avi)$ no-gzip dont-vary
This matches file extensions, but be aware that extension mapping depends on your AddType directives. For content-type–based matching, use:
SetEnvIfNoCase Content-Type "^(image/|video/|audio/|application/(pdf|zip|gzip))" no-gzip dont-vary
Verification Steps
- Reload Apache gracefully:
sudo apachectl graceful or sudo systemctl reload apache2.
- Check a binary asset:
curl -I -H "Accept-Encoding: gzip" https://example.com/image.png — the response should not include Content-Encoding: gzip.
- Check a text resource:
curl -I -H "Accept-Encoding: gzip" https://example.com/style.css — this should include Content-Encoding: gzip.
CPU and Memory Impact
Excluding binaries reduces CPU usage because compression is skipped for large files. Memory overhead is negligible; mod_deflate uses a streaming buffer, so the savings come almost entirely from CPU cycles. For typical web applications, the performance gain is modest but measurable under high traffic.
One Missing Detail
To give you a precise exclusion list, I need to know which binary MIME types your application actually serves. If you provide a sample of the Content-Type headers from your static assets, I can tailor the configuration accordingly.