Compressing text responses is one of the cheapest performance wins on a web server — a 200 KB HTML page typically compresses to 30-40 KB, cutting transfer time by 70% over a slow link. Apache ships mod_deflate for gzip and mod_brotli for Brotli; together they cover every modern browser. This article is the enable, the right AddOutputFilterByType set, and the tuning for compression level vs CPU cost.
How to verify
Check what’s loaded and whether a typical response is actually being compressed:
apachectl -M | grep -E 'deflate|brotli|filter'
grep -rE 'DEFLATE|BROTLI|AddOutputFilter' /etc/apache2/sites-enabled/ /etc/apache2/conf-enabled/
curl -sI -H 'Accept-Encoding: gzip,br' https://example.com/ | grep -i 'content-encoding'
curl -sI -H 'Accept-Encoding: gzip,br' https://example.com/style.css | grep -i 'content-encoding'
A correctly configured response includes a Content-Encoding: gzip or Content-Encoding: br header. If the header is absent, either the module is not loaded for the matched MIME type or the response was a type excluded from compression (already-compressed images, video).
What’s happening
mod_deflate is an output filter that compresses the response body with gzip after the application produces it. It is triggered on a per-MIME-type basis with AddOutputFilterByType DEFLATE <mime>, or globally with SetOutputFilter DEFLATE inside a Location.
mod_brotli is the same idea for Brotli — typically 15-25% smaller than gzip for text content, with slightly higher compression cost. Modern browsers prefer Brotli when both are offered (Accept-Encoding: gzip, br); Apache picks the encoding based on the client’s preference and what filters are configured.
The rules for what to compress:
- Compress: HTML, CSS, JS, JSON, XML, SVG, plain text, font formats (woff is already compressed; woff2 is also already compressed but it does not hurt).
- Skip: images (JPEG, PNG, WebP — already compressed), video (MP4, WebM), audio, PDFs, fonts in WOFF/WOFF2 (re-compressing is wasted CPU), gzipped or Brotli-encoded files already on disk.
The DeflateCompressionLevel directive (1-9) trades CPU for ratio. 6 is the typical default; 5 is a good balance for high-traffic boxes; 9 is wasted CPU on dynamically-generated content.
The procedure
-
Enable the modules.
sudo a2enmod deflate filter mime sudo a2enmod brotli # may not be installed by default sudo apt install -y libapache2-mod-brotli # if a2enmod brotli fails sudo apache2ctl configtest sudo systemctl reload apache2filterandmimeare dependencies for theAddOutputFilterByTypedirective. -
Configure deflate for text MIME types. Create
/etc/apache2/conf-available/deflate.conf:<IfModule mod_deflate.c> # Text MIME types AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/json AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/atom+xml AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE font/otf # Skip already-compressed types (defensive) SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|woff2?|mp4|webm)$ no-gzip SetEnvIfNoCase Request_URI \.(?:gz|bz2|zip|tgz)$ no-gzip # Compression level DeflateCompressionLevel 6 </IfModule> -
Configure brotli with the same MIME set. Add to the same conf file:
<IfModule mod_brotli.c> AddOutputFilterByType BROTLI_COMPRESS text/html AddOutputFilterByType BROTLI_COMPRESS text/plain AddOutputFilterByType BROTLI_COMPRESS text/css AddOutputFilterByType BROTLI_COMPRESS text/javascript AddOutputFilterByType BROTLI_COMPRESS application/javascript AddOutputFilterByType BROTLI_COMPRESS application/json AddOutputFilterByType BROTLI_COMPRESS image/svg+xml BrotliCompressionQuality 5 </IfModule>Brotli quality 5 is a good balance — quality 11 is the maximum but is expensive on dynamic content.
-
Enable the conf and reload.
sudo a2enconf deflate sudo apache2ctl configtest sudo systemctl reload apache2 -
Verify with curl. Test both encodings:
curl -sI -H 'Accept-Encoding: gzip' https://example.com/ | grep -i 'content-encoding' curl -sI -H 'Accept-Encoding: br' https://example.com/ | grep -i 'content-encoding' curl -sI -H 'Accept-Encoding: gzip,br' https://example.com/ | grep -i 'content-encoding'The third call should pick
br(the higher-quality option Apache prefers when both are offered). -
Pre-compress static assets. For high-traffic boxes, pre-compress CSS/JS files at deploy time and let
mod_rewriteserve the pre-compressed version when the client accepts gzip/brotli:RewriteEngine On RewriteCond %{HTTP:Accept-Encoding} br RewriteCond %{REQUEST_FILENAME}.br -f RewriteRule (.*) $1.br [QSA,L] <FilesMatch "\.css\.br$"> ForceType text/css Header set Content-Encoding br </FilesMatch>Pre-compressing saves Apache CPU on every request — useful when one CSS file gets served thousands of times an hour.
-
Profile the CPU impact. On a high-traffic box, watch
toporhtopafter enabling compression. If CPU is now the bottleneck, lowerDeflateCompressionLevelto 4 or 5; the marginal size reduction from 6 → 9 is small while the CPU cost is significant.
Common pitfalls
- Compressing already-compressed types (JPEG, MP4) is pure CPU waste with no size benefit — the
SetEnvIfNoCase ... no-gziplines defend against an over-broad MIME type list. - The
text/javascriptMIME type is deprecated in favor ofapplication/javascript— include both, since older toolchains and CDNs still emit the legacy type. - Compressing TLS-encrypted responses can leak length information via the BREACH attack class — Apache mitigates by default, but be cautious about compressing responses that include user-controlled secrets reflected in the body.
- Compression at quality 9 (deflate) or quality 11 (brotli) on dynamic responses costs measurable CPU — start at 5-6 and only raise if profiling shows headroom.
- A reverse proxy in front of Apache (CDN, load balancer) may already be compressing — double compression is harmless but wasteful; pick one layer.
Stack Harbor enables deflate + brotli with the right MIME set on every Apache front we manage, with the compression level tuned to the box’s CPU headroom. For sites where every kilobyte counts (mobile-heavy ecommerce, regional users on slow links), the gain is measurable from day one. Pair with HTTP/2 for the full compounding effect — that combination is part of the Managed Operations onboarding.