A 137 KB stylesheet compressed to 27.5 KB with gzip at level 1, to 23.3 KB at level 6, and to 18.8 KB with Brotli at its highest setting, which took 170 milliseconds of CPU to get there, more than 300 times as long as the fastest gzip run. Those numbers, from four real web files compressed in Python with zlib 1.3 and Brotli 1.2.1, explain most of the choices in an nginx compression config. They also explain why nginx’s own default compression level is a reasonable place to start but not a good place to stop, and why compressing fonts and images is wasted effort.
What compression on a web server does
Negotiated per request
A browser lists the compression formats it accepts in an Accept-Encoding request header. The server picks one, compresses the response, and labels it. Mozilla’s Content-Encoding reference describes the response header as listing “the encodings and the order in which they have been applied to a resource,” and notes that “Whether a server uses compression methods requested by the client depends on server configuration and capabilities.” The br value means Brotli, “A format using the Brotli algorithm structure (defined in RFC 7932).”
What nginx ships with
The nginx gzip module documentation describes the module as a filter that “often helps to reduce the size of transmitted data by half or even more.” Its defaults are conservative: gzip off;, gzip_comp_level 1;, and gzip_types text/html;, with the note that “Responses with the ‘text/html’ type are always compressed.” Brotli is not part of nginx itself. Google’s ngx_brotli project provides it as “a set of two nginx modules,” one that compresses on the fly and one that serves pre-compressed files, added at build time or loaded as dynamic modules.

The test files and method
Four kinds of content
The test used four files a typical site serves: a 137,260-byte CSS theme file and a 77,511-byte unminified JavaScript file, both from the Sphinx documentation tools’ Python packages, a 13,203-byte HTML article body, and a 195,704-byte WOFF2 web font.
Each file was compressed with Python’s gzip module at levels 1, 6 and 9, and with the brotli module at qualities 4, 6 and 11. Each setting ran repeatedly and the average time was recorded. The gzip module uses zlib, the same compression library behind nginx’s gzip filter, so the sizes are close to what nginx would produce, though not byte-identical because of differences in headers and buffering.
Results: size
Compressed size as a share of the original
| File | gzip 1 | gzip 6 | gzip 9 | Brotli 4 | Brotli 6 | Brotli 11 |
|---|---|---|---|---|---|---|
| theme.css (137 KB) | 20.0% | 16.9% | 16.9% | 17.0% | 15.3% | 13.7% |
| stemmer.js (78 KB) | 9.9% | 6.9% | 6.2% | 7.4% | 5.9% | 4.9% |
| article.html (13 KB) | 35.7% | 33.5% | 33.7% | 33.4% | 31.4% | 27.2% |
| font.woff2 (196 KB) | 98.5% | 98.4% | 98.3% | 98.9% | 98.7% | 98.1% |
Every setting cut text files dramatically. Even gzip at level 1 shrank the CSS to a fifth of its size. Going from level 1 to level 6 saved another 4.2 KB on the CSS and 2.4 KB on the JavaScript. Going from 6 to 9 saved almost nothing on the CSS and actually produced a 32-byte larger file on the small HTML article.
Brotli’s edge
Brotli at quality 6 beat gzip 9 on every text file, by about 10 percent on the CSS and 4 percent on the JavaScript. At quality 11 the gap widened: the CSS came in 19 percent smaller than gzip 9, and the HTML article 19 percent smaller too.
The font
The WOFF2 font barely changed under any setting, because WOFF2 is already compressed. Brotli 4 made it slightly larger than gzip did, and no setting saved more than 2 percent. Mozilla’s reference says it directly: “Compressing already compressed media types, such as .zip or .jpeg, is usually not appropriate because it can increase the file size.”

Results: time
Milliseconds per compression
| File | gzip 1 | gzip 6 | gzip 9 | Brotli 4 | Brotli 6 | Brotli 11 |
|---|---|---|---|---|---|---|
| theme.css | 0.50 | 1.61 | 3.19 | 1.54 | 2.61 | 170.41 |
| stemmer.js | 0.16 | 0.55 | 1.02 | 0.36 | 0.74 | 145.06 |
| article.html | 0.13 | 0.22 | 0.38 | 0.38 | 0.58 | 24.64 |
| font.woff2 | 4.20 | 3.97 | 5.92 | 2.16 | 3.11 | 702.93 |
Gzip 9 took about twice as long as gzip 6 for a few hundred bytes. Brotli 6 cost roughly the same time as gzip 6 to 9 and produced smaller files. Brotli 11 was in a different league: 145 to 170 ms for the CSS and JavaScript, and 703 ms to save less than 2 percent on a font that should not have been compressed at all.
Why this matters for on-the-fly compression
Every compressed response costs server CPU at request time. On a busy small server, 170 ms of CPU per stylesheet request is a real cost, while 1 to 3 ms is not. These timings come from one machine and will vary, but the ratios between settings are the useful part.
The other end of the wire
Compression time is paid by the server; decompression time is paid by every visitor’s device. On the same machine, decompressing the CSS took 0.150 ms from gzip 6, 0.192 ms from Brotli 6 and 0.213 ms from Brotli 11. Even Brotli 11, which took 170 ms to compress, decompressed in about a fifth of a millisecond. That asymmetry is what makes maximum-quality Brotli attractive for files compressed once and downloaded thousands of times.
What the bytes are worth
At a 10 Mbit/s connection, a modest mobile link, the uncompressed CSS would take about 110 ms just to transfer. Gzip 6 cut that to 18.6 ms, Brotli 6 to 16.8 ms and Brotli 11 to 15.1 ms. Turning compression on at all saved over 90 ms; choosing the best algorithm over a sensible gzip level saved another 3.5 ms. The first decision matters far more than the second, by a factor of about twenty-five here, which is why a server sending uncompressed CSS and JavaScript is a bigger problem than one using gzip instead of Brotli.
A configuration built from the numbers
Gzip as the baseline
The results point to a moderate gzip level and a list of text types:
gzip on;
gzip_comp_level 5;
gzip_types text/css application/javascript application/json image/svg+xml text/plain;
gzip_min_length 1024;
gzip_vary on;
The level of 5 or 6 captures nearly all the savings over level 1 at a fraction of level 9’s cost. gzip_types adds CSS, JavaScript, JSON, SVG and plain text to the always-compressed HTML. The nginx documentation’s gzip_min_length default is 20 bytes; raising it skips tiny responses where compression overhead outweighs the savings. gzip_vary on adds the Vary: Accept-Encoding header that the documentation describes, so caches keep compressed and uncompressed copies apart. Fonts, images and archives stay out of the list. The font result above is the reason: WOFF2, JPEG, PNG, WebP and ZIP files are already compressed internally, so the server would spend CPU on every request to save nothing, and sometimes to send a slightly larger file.
Brotli on the fly
Where ngx_brotli is installed, its README lists brotli_comp_level with a default of 6 and a range of 0 to 11, and brotli_types defaulting to text/html. The test supports leaving the level at 6 for dynamic responses: smaller than gzip 9, at similar cost.
Quality 11, but only ahead of time
Brotli 11’s size wins are real, and its cost only matters if it is paid per request. The module’s static half solves that. The README says brotli_static “Enables or disables checking of the existence of pre-compressed files with” the .br extension. Compressing CSS and JavaScript once at build time with maximum quality, saving theme.css.br next to theme.css, and turning on brotli_static gives every visitor the smallest file with no CPU cost at request time. nginx’s own gzip_static module offers the same approach for .gz files.

Checking what a server actually sends
curl with an Accept-Encoding header
After changing the configuration, curl -s -o /dev/null -D - -H 'Accept-Encoding: br, gzip' https://example.com/style.css prints the response headers, and the content-encoding line shows which format was chosen. Removing br from the request header should switch the answer to gzip, and removing both should return the file uncompressed. A vary: accept-encoding line confirms gzip_vary is working.
Proxied responses
One default catches reverse proxy setups. The nginx documentation lists gzip_proxied off; as the default and describes off as disabling “compression for all proxied requests, ignoring other parameters.” A request that reaches nginx through another proxy or CDN may arrive uncompressed even with everything else configured, which is worth checking with the same curl test from outside the network.
Sources
- nginx documentation, “Module ngx_http_gzip_module”: https://nginx.org/en/docs/http/ngx_http_gzip_module.html
- Google, ngx_brotli (GitHub): https://github.com/google/ngx_brotli
- MDN Web Docs, “Content-Encoding”: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Encoding
