Cutting Payload Size with Apache mod_deflate: A Practical Guide
Enable gzip compression in Apache with mod_deflate, configure the right MIME types, verify with curl, and understand the CPU trade‑offs.
29 Oct 2025, 08:25 UTC

The problem: bloated responses
HTML, CSS, JavaScript, and JSON can easily exceed a few hundred kilobytes per page. On mobile or high‑latency links that extra bandwidth translates directly into slower load times and higher data costs for visitors.
Why compress on the fly with mod_deflate
Apache’s mod_deflate streams a gzip (or deflate) version of each response as it is generated. The module works for both static files and dynamic output from PHP, Python, or any other handler, so you get compression without pre‑generating .gz files. Typical text‑based payloads shrink 60‑80 % while the CPU cost stays modest on modern servers.
Worked configuration example
Assume Apache 2.4.x on a Debian‑derived system. First ensure the module is loaded:
# Run as root or with sudo
a2enmod deflate
systemctl reload apache2
Then add the filter rules to your virtual host or the main httpd.conf. The snippet below compresses the most common text types and deliberately skips binary formats:
<IfModule mod_deflate_module>
AddOutputFilterByType DEFLATE \
text/html \
text/plain \
text/css \
application/json \
application/javascript \
text/xml \
application/xml
</IfModule>
Reload Apache again to activate the changes:
systemctl reload apache2
Verifying the setup
- From a client machine (or the server itself) run:
Replacecurl -I -H "Accept-Encoding: gzip" http://example.com/http://example.com/with your actual URL. Look for the headerContent-Encoding: gzipin the response. - Open the browser dev‑tools Network tab, reload the page, and confirm that the transferred size is smaller than the content‑length and that
content-encoding: gzipappears. - Monitor CPU usage for a few minutes after enabling compression (e.g.,
topormod_status) to ensure the extra load stays within your capacity plan.
Trade‑offs and limitations
- CPU overhead: each compressed response consumes cycles. On a heavily loaded box, watch the CPU trend; consider limiting compression to responses larger than a threshold (e.g.,
DeflateCompressionLevel 6andSetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip). - Already‑compressed assets: images, videos, PDFs, and zip files gain nothing and waste CPU. The
AddOutputFilterByTypelist above excludes them by design. - Proxy/CDN interaction: if a downstream cache strips
Accept-Encodingor re‑compresses, you may see double compression. Test through the full delivery chain.
Actionable next steps
- Enable
mod_deflateon a staging host using the commands above. - Apply the
AddOutputFilterByTypeblock to the relevant virtual host. - Run the
curlverification and confirmContent-Encoding: gzip. - Observe CPU and latency for at least one traffic peak before promoting to production.
With these steps you’ll cut the majority of text payload size while keeping the server’s resource usage predictable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.