GZIP vs Brotli: How Website Compression Works and How to Configure It
How encoding is negotiated, which file types to compress, and which to leave alone because compressing them again costs CPU for nothing.
In short
Text compresses extremely well. Your HTML, CSS, JavaScript, SVG and JSON can typically be sent at a quarter to a fifth of their size, which costs almost nothing and is usually a one-line server change.
Brotli is the better algorithm and is supported everywhere that matters. GZIP is the universal fallback. Serve Brotli, fall back to GZIP, and you are done.
The part people get wrong is what not to compress. JPEG, PNG, WebP, AVIF, WOFF2, MP4 and ZIP are already compressed. Compressing them again burns processor time to produce a file that is the same size or slightly larger.
How it works
Compression is negotiated per request. The browser says what it can decode; the server picks one and says what it used.
# The browser's request
Accept-Encoding: gzip, deflate, br, zstd
# The server's response
Content-Encoding: br
Vary: Accept-Encoding
Vary: Accept-Encoding matters more than it looks. It tells caches that the response differs by encoding, so a compressed copy is not served to a client that cannot decode it. Without it, a CDN can cache a Brotli response and hand it to a client that asked for GZIP.
The browser decompresses before anything else happens. Compression saves transfer time, not parsing or execution time — a 90 KB JavaScript bundle compressed to 25 KB still has 90 KB of code to parse and run.
Brotli versus GZIP
Brotli generally produces smaller output on text than GZIP — commonly in the region of 15–25% smaller, varying considerably by content. It is worth having, and it is not a dramatic difference: the large win is going from nothing to something, not from GZIP to Brotli.
Two practical considerations:
Compression level is a trade-off. Brotli offers levels up to 11. High levels are slow — fine for files compressed once at build time, bad for responses compressed per request. A common configuration is level 11 for pre-compressed static files and level 4 or 5 for dynamic responses.
Static pre-compression is better where possible. Compress your build output once, store the .br and .gz files next to the originals, and have the server serve them directly. You get maximum compression with no per-request cost.
# Serve pre-compressed files if they exist (requires ngx_brotli_static)
brotli_static on;
gzip_static on;
What to compress, and what to leave alone
| Compress | Do not compress |
|---|---|
| HTML | JPEG, PNG, GIF |
| CSS | WebP, AVIF |
| JavaScript | WOFF2 (already Brotli-compressed) |
| JSON | MP4, WebM |
| SVG (it is text) | ZIP, gzip archives |
| XML, including sitemaps and RSS | PDF (usually already compressed internally) |
| Plain text |
Two entries catch people out. SVG is text and compresses very well, so it should be in your list — many configurations omit it. And WOFF2 is already Brotli-compressed internally, so compressing it again is pure waste.
Compressing already-compressed data wastes CPU and can produce a slightly larger file, because the compressor adds its own framing to data with no remaining redundancy.
Configuration
Nginx
# Brotli (requires the ngx_brotli module)
brotli on;
brotli_comp_level 5;
brotli_types text/plain text/css text/xml application/javascript
application/json application/xml image/svg+xml
application/rss+xml font/ttf;
# GZIP fallback
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_vary on;
gzip_proxied any;
gzip_types text/plain text/css text/xml application/javascript
application/json application/xml image/svg+xml
application/rss+xml;
gzip_min_length 256 avoids compressing tiny responses, where the overhead is not worth it.
Apache
<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css \
text/xml application/javascript application/json image/svg+xml
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css \
text/xml application/javascript application/json image/svg+xml
Header append Vary Accept-Encoding
</IfModule>
Behind a CDN
Most CDNs compress on your behalf, and some will compress even if your origin does not. That is convenient and has a catch: the CDN compresses what it caches, so a cache miss may be served uncompressed. Configure compression at the origin as well.
Hosted platforms
On Shopify, Wix, Squarespace, Webflow and most hosted platforms, compression is already applied and not configurable. If a report says "enable text compression" and you are on one of these, the recommendation is not actionable — see the platform permission matrix.
Verifying it
Per file, from the command line
# Does the server compress, and with what?
curl -sI -H "Accept-Encoding: br, gzip" https://example.com/assets/app.js \
| grep -iE "content-encoding|content-length|vary"
# Compare compressed and uncompressed sizes
curl -s -o /dev/null -w "brotli: %{size_download} bytes\n" \
-H "Accept-Encoding: br" https://example.com/assets/app.js
curl -s -o /dev/null -w "none: %{size_download} bytes\n" \
-H "Accept-Encoding: identity" https://example.com/assets/app.js
What you should see: content-encoding: br, a vary: accept-encoding header, and a compressed size substantially smaller than the uncompressed one.
In DevTools
In the Network panel, compare the Size column (transferred) with the value below it (actual). Equal means uncompressed. Enable the Content-Encoding column to see which algorithm was used per file.
Check the whole list, not one file. Partial configuration is the usual problem.
Common mistakes
Compressing images, video or fonts. Wasted CPU for no gain.
An incomplete content-type list, typically missing JavaScript, JSON or SVG.
Omitting Vary: Accept-Encoding, which can let a cache serve the wrong encoding.
Using maximum compression level for dynamic responses, adding server latency to save a few bytes.
Compressing at the CDN only, so cache misses are uncompressed.
Expecting it to help execution time. It reduces transfer, nothing else.
Testing with curl without an Accept-Encoding header and concluding compression is off — some clients do not advertise support by default.
Chasing Brotli when nothing is compressed. Get GZIP working first; the jump from nothing is the large one.
When it still does not help
- No
content-encodingheader → not enabled, or the content type is missing from your list. Check the exact type the server sends for that file. - Some files compressed, others not → an incomplete type list, or a
min_lengththreshold excluding small files. - Compressed at the edge but not from the origin → configure the origin too, so cache misses are also compressed.
- The file got slightly larger → you are compressing something already compressed. Remove that type.
- Server CPU rose noticeably → your dynamic compression level is too high, or you should pre-compress static files at build time.
- No change to your metrics → compression helps transfer. If your bottleneck is server response time or script execution, this was never going to be the fix.
Keeping it configured
Compression is set once and then quietly lost — a server migration, a new reverse proxy, a container image built from a different base.
Two habits: check content-encoding on a real public URL after any infrastructure change, and include a compression check in whatever monitoring you already run, because this is a silent regression that costs every visitor on every page.
Frequently asked questions
Is Brotli better than GZIP?
Generally yes on text — commonly around 15 to 25% smaller, varying by content. But the large win is going from no compression to some compression, not from GZIP to Brotli. Serve Brotli with GZIP as the fallback.
What files should I not compress?
Anything already compressed: JPEG, PNG, WebP, AVIF, WOFF2, MP4 and ZIP. Compressing them again burns CPU to produce a file the same size or slightly larger. WOFF2 catches people out — it is already Brotli-compressed internally.
Does compression make my JavaScript run faster?
No. It reduces transfer time only. A 90 KB bundle compressed to 25 KB still has 90 KB of code to parse and execute, which is usually the larger cost on a phone.
Why is only some of my site compressed?
Almost always an incomplete content-type list, typically missing JavaScript, JSON or SVG because the configuration was copied from an old example. SVG is text and compresses very well, and is frequently omitted.
Do I need the Vary: Accept-Encoding header?
Yes. It tells caches the response differs by encoding, so a Brotli response is not served to a client that cannot decode it. Without it a CDN can hand the wrong encoding to a visitor.
Should I use the highest compression level?
For files compressed once at build time, yes. For responses compressed per request, no — high levels add server latency to save a few bytes. A common setup is maximum level for pre-compressed static files and a middle level for dynamic responses.
References
- Compression in HTTP — MDN
- Content-Encoding — MDN
- RFC 7932: Brotli — IETF