Compression turns 80 KB of JSON into 10 KB on the wire — meaningful at the third decimal of LCP, and free to enable. Nginx ships gzip out of the box; Brotli requires a module but compresses 15–25% better at the same CPU cost for text. This article covers both, the content types you should actually compress, and the BREACH-style trap that means you do not compress every TLS response without thought.
How to verify
See what the server is already doing:
sudo nginx -V 2>&1 | grep -Eo 'with-http_gzip[a-z_]*|brotli'
curl -sH 'Accept-Encoding: gzip' -I https://example.com/ | grep -i content-encoding
curl -sH 'Accept-Encoding: br' -I https://example.com/ | grep -i content-encoding
sudo nginx -T | grep -E 'gzip |brotli '
If you see Content-Encoding: gzip with the first curl and Content-Encoding: br with the second, both are active. If neither shows, no compression is happening.
What’s happening
gzip is built into the core Nginx package via ngx_http_gzip_module. Brotli is not in upstream Nginx; it ships as ngx-brotli (now libnginx-mod-brotli on Ubuntu 24.04). The two coexist — Brotli is preferred by browsers that advertise br in Accept-Encoding, and gzip serves the rest.
The win is proportional to content type. JSON, HTML, CSS, JS, SVG, and XML compress well. JPEG, PNG, WebP, MP4, woff2, and PDF are already compressed; running gzip over them costs CPU and saves ~1%. The standard list of compressible MIME types is what you set in gzip_types.
The BREACH consideration: gzip + secrets in TLS responses + an attacker who can inject reflected content lets the attacker extract the secret by observing length deltas. The real-world fix is to never reflect attacker-controlled input into a response that also contains secrets — which most modern frameworks already enforce. The defensive Nginx-side fix is gzip_disable for endpoints that mix the two, but in practice we leave compression on and rely on the framework.
The procedure
- gzip — already linked into the package; enable in
nginx.confunderhttp {}:gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 5; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml application/xml+rss application/rss+xml application/atom+xml image/svg+xml font/ttf font/eot;gzip_comp_level 5is the sweet spot — levels 6+ cost noticeably more CPU for marginal additional compression.gzip_min_length 1024skips tiny responses where the header overhead exceeds the savings. - Brotli — install the module and load it:
Brotli loads viasudo apt install -y libnginx-mod-brotliload_modulelines in the top ofnginx.conf(the package usually drops these into/etc/nginx/modules-enabled/). Then underhttp {}:brotli on; brotli_comp_level 5; brotli_min_length 1024; brotli_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml; - Decide on static pre-compression. If you generate Brotli/gzip artefacts at build time (
.brand.gznext to the original), let Nginx serve those directly:
This skips the runtime compression entirely — cheaper and more compressible (you can usegzip_static on; brotli_static on;brotli_comp_level 11offline without hurting request latency). - Test:
Thesudo nginx -t && sudo systemctl reload nginx curl -sH 'Accept-Encoding: br' -I https://example.com/file.css | grep -iE 'content-encoding|vary'Vary: Accept-Encodingheader is what tells caches (browser, CDN, reverse proxy) that this response varies by encoding. - Confirm
gzip_proxiedmatches your topology. The defaultoffmeans do not compress responses to proxied requests, which is wrong if your CDN/reverse-proxy is upstream of you.gzip_proxied anycompresses regardless of proxy headers. - Measure. The headline number is “transfer size” in browser devtools. For an HTML page that is mostly JS, compression should drop the wire size to ~25–35% of the uncompressed bytes.
Common pitfalls
- Compressing
application/octet-stream— it matches.bindownloads which are usually pre-compressed media. Cost without benefit. - Forgetting
gzip_vary on;. Without it, a cache may serve a gzipped response to a client that did not ask for it. - Setting
gzip_comp_level 9. The CPU cost is double level 5 for a 1–2% smaller payload. Use 5 for runtime compression and 11 only for static pre-compression. - Brotli module installed but
Accept-Encodingfrom the browser does not advertisebr(e.g. inside a corporate proxy that strips it). The fallback to gzip works but you may not see Brotli in the wild even though it is configured. - Compressing already-compressed formats. Always exclude
image/jpeg,image/png,video/mp4,application/pdf,font/woff,font/woff2.
In the engagements we run, compression is the lowest-effort web-performance win, but easy to mis-tune — half of the configs we inherit have gzip on with the wrong types and Brotli not enabled at all. Stack Harbor templates the compression block once and keeps it consistent across hosts as part of managed operations.