← Back to blog

Gzip vs Brotli: Which Compression to Use and How to Enable It

November 21, 2026
Gzip vs Brotli: Which Compression to Use and How to Enable It

Use Brotli for static assets you can compress once at a high level, keep gzip as the fallback for every client that does not ask for br, and use moderate levels of either for responses generated on the fly. Brotli usually wins on size, gzip wins on encoding speed and universal support, and the right setup uses both. After you change anything, confirm what the server actually sends. Our free HTTP response header inspection and other web tools are built for that kind of quick check, with no installation or account for core functionality.

Pingfloat
Verify Your Server’s Compression
Use Pingfloat’s web tools to inspect response headers and check what your server sends after changing compression settings.
Visit

TL;DR

  • Brotli typically produces files 15 to 25% smaller than gzip for HTML, CSS and JavaScript at its highest settings, according to HTTP Archive-based analysis.
  • Brotli's top levels are very slow to compress. Use them for precompressed static files, and moderate levels for dynamic responses.
  • Decompression in the browser is not the bottleneck: higher compression levels do not make files more expensive to decode.
  • Serve br to clients that advertise it and gzip to the rest, and send Vary: Accept-Encoding so caches keep both versions apart.
  • Verify with curl and the Accept-Encoding header rather than trusting a config file.

Table of Contents

What gzip and Brotli actually are

Both are lossless algorithms for text: HTML, CSS, JavaScript, JSON, SVG and fonts. The browser says what it can decode in the Accept-Encoding request header, the server picks one and names it in Content-Encoding. MDN's guide to HTTP compression says only two encodings matter today: gzip, the most common, and br, the newer challenger. It also says the server must add Vary: Accept-Encoding so a cache does not hand a Brotli body to a client that wanted gzip.

Gzip is built on DEFLATE, with a fixed 32 KB window, meaning it can only look back 32 KB for repeated patterns. Brotli's window can be far larger, up to 512 times bigger according to Cloudflare's 2015 experiment. Brotli also ships with a built-in dictionary of common web strings, which is one reason it helps most on small text files.

Compression ratio: how much smaller is Brotli?

The published numbers vary with the files tested, but they point the same direction.

SourceTestResult
Cloudflare, 2015Maximal quality, Brotli vs zlibBrotli 1.19x smaller on average, 1.38x for files under 1 KB
Paul Calvano, 2024HTTP Archive data, January 2024Highest Brotli level 15 to 25% smaller than gzip
Elmar Klausmeier, 2023544 HTML files, about 19 MBgzip -6: 8,555,589 bytes. Brotli -q6: 7,998,547 bytes. Brotli -q11: 7,427,506 bytes

In the HTML benchmark, Brotli at quality 11 saved roughly 13% over gzip at level 6. Your own gain depends on the content. Minified JavaScript, which has less redundancy, compresses less dramatically than verbose HTML, so measure with your own bundle before promising anyone a percentage.

One practical point: the bytes you save are only on text. Images, ZIP files, PDFs and similar already-compressed formats gain nothing, and Deploynix's Nginx guide advises leaving them out of compression lists.

Compression ratio: how much smaller is Brotli?

Speed and CPU cost: where the trade-off lives

The size win has a price on the server side, and it depends on the level.

In the same HTML benchmark, Brotli at quality 6 finished in 0.27 seconds against 0.30 seconds for gzip level 6, while producing a smaller result. Brotli at quality 11 took 14.16 seconds, against 0.34 seconds for gzip level 9. So low and middle Brotli levels are competitive with gzip, and the very top level is a different class of workload.

Compression benchmark times for Brotli and gzip

The reverse is true in the browser. The 2021 Web Almanac notes that higher compression levels take longer to compress, but decompression CPU use does not rise, and more heavily compressed files even decode slightly faster. The R brotli package's benchmark notes reach the same conclusion: decompression is at least as fast as gzip while the ratio improves, but compression is much slower, which makes Brotli best for static content such as fonts and HTML pages.

Small dynamic responses are a special case. Cloudflare found that for files under 64 KB, Brotli quality 4 was about 1.48 times slower than zlib level 8. If the time spent compressing exceeds the time saved on the wire, the page gets slower, not faster.

Static versus dynamic content

This is the decision that matters most in practice.

Static, cacheable files (bundles, stylesheets, fonts, SVG, prebuilt HTML): compress once at build time at the maximum level, store .br and .gz files beside the originals, and let the server pick. You pay the 14-second cost once, not per request. Paul Calvano notes that cacheable content can take the highest levels, while for dynamic, non-cacheable responses expensive levels can hurt time to first byte, so moderate levels are the better choice there.

Dynamic responses (API JSON, server-rendered pages): compress on the fly at a moderate level, roughly Brotli 4 to 6 or gzip 5 to 6. If a CMS sits in front of your origin, a page cache turns many dynamic pages into static ones. For Ghost sites, this comparison of caching plugins for Ghost shows what that layer can do for performance.

Gzip compression levels

Gzip levels run from 1 to 9. Higher means smaller output and more CPU, with sharply diminishing returns.

  • Level 1: the Nginx default. According to the Nginx gzip module documentation, gzip_comp_level accepts 1 to 9 and defaults to 1.
  • Level 5 or 6: the usual sweet spot. The h5bp Nginx configuration uses level 5 and describes it as a compromise between size and CPU, with about a 75% reduction for most ASCII files, almost identical to level 9. Paul Calvano reports that most servers and CDNs default to level 6.
  • Level 9: worth it only for files you precompress.

In Klausmeier's test, going from gzip 6 to gzip 9 saved about 12,000 bytes out of 8.5 million. That is the diminishing return in one number. If you are stuck on gzip, moving from level 1 to level 5 gives most of the available gain.

Brotli levels

Brotli uses levels 0 to 11. Defaults differ by server: Apache's mod_brotli defaults BrotliCompressionQuality to 5 and says quality 4 is roughly equivalent to gzip level 6. Google's ngx_brotli module defaults brotli_comp_level to 6. Apache describes quality 11 as the best ratio but very slow and only suitable with caching.

A workable rule: levels 4 to 6 for live responses, 9 to 11 for files compressed at build time. Calvano's tests found Brotli level 5 a good tradeoff, and level 9 or higher gave about 75% smaller payloads in his sample.

Browser support

Brotli support is no longer a blocker. Can I use lists support in current Chrome, Safari, including Safari on iOS, and the other major browsers. Older guidance, such as Chrome's Lighthouse documentation, which was written in 2022, listed Safari on iOS as an exception. Either way, the advice holds: use Brotli when the browser supports it, and gzip as the fallback. Content negotiation does this automatically, because a client that does not send br simply gets gzip.

Lighthouse also flags uncompressed text only when the response is over 1.4 KiB and the possible savings exceed 10%, so tiny files are not worth worrying about.

How to enable gzip in Nginx

Gzip ships with stock Nginx. A solid starting block, based on the h5bp reference configuration:

gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types text/css application/javascript application/json image/svg+xml;

What each line does, and where it goes wrong:

  1. gzip_min_length 256: the default is 20 bytes, which usually makes tiny files bigger after gzipping.
  2. gzip_proxied any: without it, a CDN that adds a Via header makes Nginx skip compression silently, as How HTTP Works points out.
  3. gzip_vary on: adds Vary: Accept-Encoding. It is off by default.
  4. gzip_types: the default is text/html only, so CSS, JavaScript and JSON go out uncompressed until you list them. Do not list text/html, since it is always compressed.
  5. gzip_static on: serves a prebuilt .gz file when it exists, so you can compress at level 9 in your build and spend no CPU at request time.

The Nginx documentation also warns that compressed responses over TLS may be subject to BREACH attacks. If a page reflects secrets such as tokens alongside user input, take that into account when deciding what to compress.

How to enable Brotli

Stock Nginx has no Brotli. You need Google's ngx_brotli, which provides a filter module for on-the-fly compression and a static module for precompressed files. Build both as dynamic modules, or install your distribution's packages (Debian has libnginx-mod-http-brotli-filter and libnginx-mod-http-brotli-static, per How HTTP Works). Then load them and set:

brotli on;
brotli_comp_level 5;
brotli_min_length 256;
brotli_types text/css application/javascript application/json image/svg+xml;
brotli_static on;

Brotli has no vary directive of its own in this module, so keep gzip_vary on to get the Vary: Accept-Encoding header. With brotli_static on and gzip_static on, Nginx will serve app.js.br, app.js.gz or app.js according to what the client accepts.

On Apache 2.4.26 and later, enable mod_brotli and set BrotliCompressionQuality. Its window setting, BrotliCompressionWindow, ranges from 10 to 24 (as a power of two in bytes) and defaults to 18, a 256 KB window.

If you use Cloudflare, check the content compression docs: the visitor-facing default depends on plan (Zstandard on Free, Brotli on Pro and Business, gzip on Enterprise), and Cloudflare always asks your origin for br, gzip. Your origin must therefore answer at least one of them cleanly.

Check what your server really sends

Config files lie by omission: a proxy, CDN or missing MIME type can change the outcome. Request each encoding and compare:

curl -s -o /dev/null -H 'Accept-Encoding: gzip' -w '%{size_download}
' https://example.com/app.js
curl -s -o /dev/null -H 'Accept-Encoding: br' -w '%{size_download}
' https://example.com/app.js
curl -sI -H 'Accept-Encoding: br' https://example.com/app.js

The first two print the bytes transferred, the third shows Content-Encoding and Vary. Three negotiation rules from RFC 9110, as explained by How HTTP Works, catch people out: a missing Accept-Encoding header means any coding is acceptable, an empty one means no coding, and sending br to a client that asked only for gzip yields a garbled body, not an error.

The curl documentation adds that the option is a request, not an order, so the server may answer differently, and curl needs a build with the Brotli library to decode br itself (supported since curl 7.57.0).

If you would rather read headers in a browser, our guide to debugging HTTP headers shows what to look for, and the security headers checklist covers the other response headers worth setting while you are in the config. Nginx users editing server blocks may also want these Nginx rewrite examples.

Where Base64 fits in

Compression and encoding are different jobs. Base64 inflates data by about a third, and gzip or Brotli claw some of that back on text, but the cleaner fix is usually to serve the file as a file. See Base64 encoding overhead for the numbers.

Our perspective

We look at compression as a deployment question, not a contest. The headline ratio gets the attention, but the failures we see discussed most are configuration ones: a MIME type missing from the list, a missing Vary header, a proxy that quietly turns compression off.

Our advice is simple. Precompress static assets at the top Brotli and gzip levels during the build, serve live responses at a moderate level, and always verify the result from the outside. That last step is why we build free tools that read headers and responses without installing anything: you should be able to confirm in seconds that the encoding you configured is the encoding users get.

Pingfloat's tools are free and need no account for core functionality, so checking a header, a certificate or an endpoint after a config change is a thirty-second job.

Andreas

Check your changes with Pingfloat

After a compression change, the useful habit is to look at the live response. Pingfloat offers free web tools for developers and IT professionals, including HTTP response header inspection, an SSL checker and a website uptime monitor, so you can confirm the new setup serves correctly and stays up. Start from the Pingfloat tools page.

Pingfloat

FAQ

Is Brotli better than gzip?

For size, yes: at high levels it produces files roughly 15 to 25% smaller on HTML, CSS and JavaScript. For compression speed at its top levels, gzip is far faster, which is why Brotli suits precompressed static files best. Most sites should use both.

Should I use Brotli or gzip for dynamic pages?

Use a moderate level of either. Brotli around levels 4 to 6 compresses quickly enough for live responses and still beats gzip on size. Avoid levels 9 to 11 on requests generated per user, because the CPU time can delay the first byte.

What gzip compression level should I choose?

Level 5 or 6 is the common choice. The h5bp configuration uses 5 for its size-to-CPU balance, and many servers and CDNs default to 6. Nginx itself defaults to 1, so raising it is often a free improvement.

Do I have to build Nginx to get Brotli?

Stock Nginx does not include Brotli, so you need the ngx_brotli module. You can build it as a dynamic module and load it, or install a distribution package such as Debian's Brotli filter and static modules.

Sources