Skip to content

Compression in Nginx — gzip versus Brotli and what to actually compress

How to enable gzip and Brotli compression in Nginx, choose which content types are worth compressing, and avoid the BREACH-style pitfalls on TLS-served compressible content.

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

  1. gzip — already linked into the package; enable in nginx.conf under http {}:
    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 5 is the sweet spot — levels 6+ cost noticeably more CPU for marginal additional compression. gzip_min_length 1024 skips tiny responses where the header overhead exceeds the savings.
  2. Brotli — install the module and load it:
    sudo apt install -y libnginx-mod-brotli
    Brotli loads via load_module lines in the top of nginx.conf (the package usually drops these into /etc/nginx/modules-enabled/). Then under http {}:
    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;
  3. Decide on static pre-compression. If you generate Brotli/gzip artefacts at build time (.br and .gz next to the original), let Nginx serve those directly:
    gzip_static   on;
    brotli_static on;
    This skips the runtime compression entirely — cheaper and more compressible (you can use brotli_comp_level 11 offline without hurting request latency).
  4. Test:
    sudo nginx -t && sudo systemctl reload nginx
    curl -sH 'Accept-Encoding: br' -I https://example.com/file.css | grep -iE 'content-encoding|vary'
    The Vary: Accept-Encoding header is what tells caches (browser, CDN, reverse proxy) that this response varies by encoding.
  5. Confirm gzip_proxied matches your topology. The default off means do not compress responses to proxied requests, which is wrong if your CDN/reverse-proxy is upstream of you. gzip_proxied any compresses regardless of proxy headers.
  6. 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 .bin downloads 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-Encoding from the browser does not advertise br (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.