Compression / mod_deflate
mod_deflate compresses responses (HTML, CSS, JavaScript, JSON) before sending them over the network, so the browser downloads a smaller file and decompresses it locally -- usually a large win for text-based content.In this page:
How Compression Saves Bandwidth
Text-based formats -- HTML, CSS, JavaScript, JSON, plain text -- compress very well, often shrinking to a fraction of their original size, because they're full of repeated characters and patterns. The browser sends Accept-Encoding: gzip to say it can handle compressed responses, and Apache replies with a Content-Encoding: gzip header alongside the compressed body.
Enabling mod_deflate
On Ubuntu/Debian: sudo a2enmod deflate then reload. Once enabled, AddOutputFilterByType DEFLATE text/html text/css application/javascript tells Apache which content types to compress -- it's applied by MIME type, not by file extension.
Don't Compress Already-Compressed Files
JPEG, PNG, MP4, and ZIP files are already compressed internally, so running them through mod_deflate again wastes CPU for little or no size reduction -- sometimes the "compressed" output is even slightly larger than the original. Compression should be scoped to text-based MIME types only.
mod_deflate vs. Brotli
Gzip (via mod_deflate) is universally supported and simple to configure. Brotli, supported via the separate mod_brotli module, generally compresses a little better than gzip at the same speed, and is supported by all modern browsers -- worth considering for a further, incremental improvement once basic gzip compression is already in place.
Example: Enabling gzip compression for text content
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css
AddOutputFilterByType DEFLATE application/javascript application/json
</IfModule>
{# Flagged by hand after confirming a runner can't handle this example
(a shell command / go.mod file stored as a TopicExample, a language
feature the configured runner version doesn't support, or output
that blows a runner's sandbox limit) -- see TopicExample.norun.
Never render the run button for these, regardless of language,
since it would just fail at execute_code (or worse, hang the
Judge0 queue on a submission that can never finish cleanly). #}
- Enabling DEFLATE for every content type including images and archives, wasting CPU without meaningfully reducing size.
- Forgetting to enable mod_deflate itself before adding AddOutputFilterByType rules that depend on it.
- Not testing that compression is actually active (
curl -H "Accept-Encoding: gzip" -Iand checking forContent-Encoding: gzip), assuming the config works just because it was added.
- KeepAlive and the choice of MPM (prefork/worker/event) both control how efficiently Apache handles many simultaneous connections;
eventis the modern default unless a non-thread-safe module forces prefork. - Caching (Cache-Control/Expires, ETag validators) and compression (mod_deflate for text content) both reduce how much data has to move over the network, and both need to be scoped carefully -- caching only what's safe to reuse, compressing only text-based formats.
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: