Website Performance

How to Enable gzip and Brotli Compression on Nginx

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

Text files compress extremely well. HTML, CSS, JavaScript, JSON and SVG are full of repeated words and patterns, so compressing them before sending often shrinks them to a fraction of their original size. Fewer bytes mean faster downloads, especially on mobile connections, and lower bandwidth use on your server. Browsers have supported gzip for decades and Brotli for years, and they tell the server which they accept through the Accept-Encoding request header. This guide shows how to enable both on Nginx on a Linux VPS, and how to check that it works.

gzip vs Brotli

gzipBrotli
Browser supportuniversalall modern browsers, over HTTPS
Typical result on textgoodusually smaller files than gzip
CPU cost at high levelsmoderatehigh at levels 10-11
Included in Nginxyes, built inno, needs the ngx_brotli module
Header valueContent-Encoding: gzipContent-Encoding: br

The usual approach is to enable both: Brotli for browsers that support it, gzip as the fallback.

Step 1: enable gzip

gzip is part of standard Nginx builds. Create /etc/nginx/conf.d/gzip.conf (on Debian and Ubuntu, some gzip lines already exist commented out in /etc/nginx/nginx.conf; make sure you do not define the same directive twice in the http block):

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types
    text/plain text/css text/xml text/javascript
    application/javascript application/json application/xml
    application/rss+xml application/atom+xml application/manifest+json
    image/svg+xml font/ttf font/otf application/vnd.ms-fontobject;

Notes on these directives:

  • text/html is always compressed when gzip is on, so it does not need to be listed.
  • gzip_vary on adds Vary: Accept-Encoding, which keeps CDNs and proxies from serving a compressed file to a client that cannot read it.
  • gzip_proxied any compresses responses that Nginx proxies from an application such as PHP-FPM, Node.js or Python.
  • Levels go from 1 to 9. Levels around 4 to 6 are a common balance; higher levels cost more CPU for small extra savings.

Test and reload:

sudo nginx -t && sudo systemctl reload nginx

Step 2: add Brotli

Brotli requires the ngx_brotli module. On recent Debian and Ubuntu releases it is packaged:

sudo apt update
sudo apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static

If your distribution does not package it, you can build the module from source against your exact Nginx version, or use a repository that ships it. Then create /etc/nginx/conf.d/brotli.conf:

brotli on;
brotli_comp_level 5;
brotli_min_length 256;
brotli_types
    text/plain text/css text/xml text/javascript
    application/javascript application/json application/xml
    application/rss+xml application/atom+xml application/manifest+json
    image/svg+xml font/ttf font/otf application/vnd.ms-fontobject;

Brotli levels go from 0 to 11. For on-the-fly compression, levels 4 to 6 keep CPU use reasonable. Run nginx -t and reload again.

Step 3: precompress static files

For files that do not change between deployments, you can compress them once at build time with the maximum level and let Nginx serve the ready-made file:

find /var/www/site/assets -type f \( -name '*.css' -o -name '*.js' -o -name '*.svg' \) \
  -exec gzip -k -9 {} \; -exec brotli -k -q 11 {} \;
location /assets/ {
    gzip_static on;
    brotli_static on;
}

Nginx then looks for app.css.br or app.css.gz next to app.css and sends it without spending CPU per request. Remember to regenerate these files on each deployment, or a stale compressed copy will be served.

What not to compress

  • JPEG, PNG, WebP, AVIF, GIF, MP4 and most audio formats are already compressed; compressing again wastes CPU and can make them larger.
  • ZIP, gzip archives and PDF files are generally compressed internally.
  • Very small responses, where headers dominate the size.

There is also a security consideration: compressing responses that mix secret tokens with user-controlled input over HTTPS can enable attacks such as BREACH. Frameworks usually mitigate this with per-request token masking; be aware of it if you write custom pages that reflect user input next to secrets.

Step 4: verify it works

curl -sI -H 'Accept-Encoding: br' https://example.com/ | grep -iE 'content-encoding|vary'
curl -sI -H 'Accept-Encoding: gzip' https://example.com/assets/app.css | grep -i content-encoding

You should see content-encoding: br and content-encoding: gzip. Compare sizes:

curl -s -o /dev/null -w '%{size_download}\n' https://example.com/
curl -s -o /dev/null -w '%{size_download}\n' -H 'Accept-Encoding: br' https://example.com/

Browser developer tools show the same information in the Network tab. If a CDN sits in front of Nginx, the CDN may compress or recompress responses itself; check its settings as part of your CDN optimization.

Watching CPU usage

Compression trades CPU for bandwidth. On a small VPS with heavy traffic, high on-the-fly levels can become noticeable. Use moderate levels for dynamic responses, precompress static assets, and check usage with top or htop during peaks. If you need more headroom, compare plans on the pricing page.

FAQ

Should I use gzip or Brotli?

Use both. Brotli usually produces smaller files and is supported by modern browsers over HTTPS, while gzip covers older clients and tools. Nginx picks the right one based on the Accept-Encoding header the client sends.

What compression level should I choose?

For responses compressed on the fly, levels around 4 to 6 for both gzip and Brotli are a sensible balance between size and CPU. For static files compressed once at build time, use the maximum levels, gzip 9 and Brotli 11, because the cost is paid only once.

Why do I not see Content-Encoding in the response?

Common reasons: the MIME type is missing from gzip_types or brotli_types, the response is smaller than the minimum length, the request did not include Accept-Encoding, the Brotli module is not loaded, or a proxy or CDN in between removed or changed the encoding.

Related guides