Log inSign up

Compress responses with gzip and Brotli

In Nginx, set gzip on; with a gzip_types list, and add Brotli by installing the libnginx-mod-http-brotli-filter package on Ubuntu 24.04 or Debian. In Apache, use mod_deflate for gzip and mod_brotli for Brotli, which ships with Apache on all four systems. Check with curl -H 'Accept-Encoding: br' and look for a Content-Encoding header.

Topic: Web servers and proxies · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What do gzip and Brotli change?

Both shrink text responses before they leave the server, so pages arrive faster and your monthly transfer goes further. HTML, CSS, JavaScript, JSON, SVG and XML compress very well, often to a fraction of their size. Images, video, fonts in WOFF2 and archives are already compressed, so leave them out; compressing them costs CPU and saves nothing.

Browsers announce what they accept in the Accept-Encoding request header. gzip is understood everywhere. Brotli usually produces smaller files for text and every current browser supports it over HTTPS. Running both is the normal setup: the server uses Brotli when the client asks for br and falls back to gzip otherwise. Every plan includes a fixed amount of outgoing transfer (3 TB on A1-2, for example), so compression also helps you stay inside it.

How do you enable gzip in Nginx?

gzip is built into Nginx; you only need to switch it on and list types. Debian and Ubuntu already set gzip on; in /etc/nginx/nginx.conf with the other lines commented. Put your settings in one place, for example /etc/nginx/conf.d/compression.conf, and remove or comment duplicate gzip lines in the main file so nginx -t does not complain:

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

text/html is always compressed and must not be listed again. gzip_vary adds Vary: Accept-Encoding so caches keep separate copies. gzip_proxied any also compresses responses from apps behind a reverse proxy. Levels 4 to 6 give nearly all the size gain of level 9 at much lower CPU cost.

sudo nginx -t && sudo systemctl reload nginx

How do you add Brotli to Nginx?

Ubuntu 24.04 and Debian 12 and 13 package the Brotli module, so install it and it is loaded automatically:

sudo apt install -y libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static
ls /etc/nginx/modules-enabled/ | grep brotli

Add the Brotli directives next to the gzip block in the same file:

brotli on;
brotli_comp_level 5;
brotli_static on;
brotli_types text/plain text/css text/xml application/xml application/json
             application/javascript text/javascript application/rss+xml
             application/manifest+json image/svg+xml font/ttf font/otf;

On Ubuntu 22.04, run apt-cache policy libnginx-mod-http-brotli-filter to see whether the package is available for your release. Rocky Linux and AlmaLinux do not include an Nginx Brotli module in their base repositories. There you can stay with gzip, which already captures most of the benefit, build the module from source against the exact Nginx version you run, or use Apache, whose package includes Brotli.

How do you enable gzip in Apache?

Apache compresses with mod_deflate, which despite the name produces gzip output. It is enabled by default on Debian family and loaded on Rocky and Alma. Check and enable it:

sudo a2enmod deflate headers     # Ubuntu and Debian
httpd -M | grep deflate          # Rocky and Alma

Debian already ships a reasonable /etc/apache2/mods-available/deflate.conf. For full control, or on Rocky and Alma where you create the file yourself, add compression.conf under conf-available (then a2enconf compression) or under /etc/httpd/conf.d/:

<IfModule mod_deflate.c>
    DeflateCompressionLevel 5
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
    AddOutputFilterByType DEFLATE application/javascript text/javascript
    AddOutputFilterByType DEFLATE application/json application/xml image/svg+xml
</IfModule>

How do you add Brotli to Apache?

mod_brotli is part of Apache 2.4 on all four systems. Enable it on Debian family with sudo a2enmod brotli; on Rocky and Alma check that httpd -M | grep brotli lists it. Then chain both filters so Brotli is tried first and gzip covers older clients:

<IfModule mod_brotli.c>
    BrotliCompressionQuality 5
    AddOutputFilterByType BROTLI_COMPRESS;DEFLATE text/html text/plain text/css
    AddOutputFilterByType BROTLI_COMPRESS;DEFLATE application/javascript text/javascript
    AddOutputFilterByType BROTLI_COMPRESS;DEFLATE application/json application/xml image/svg+xml
</IfModule>

If the client accepts br, Brotli encodes the body and the deflate filter steps aside because the response is already encoded; otherwise gzip handles it. Test and reload with sudo apachectl configtest && sudo systemctl reload apache2 (or httpd). More Apache setup is in the Apache guide.

How do you check that compression works?

Ask for each encoding with curl and read the response headers. A GET with the body thrown away shows the real behavior:

curl -s -o /dev/null -D - -H 'Accept-Encoding: br' https://example.net/ | grep -i -E 'content-encoding|vary'
curl -s -o /dev/null -D - -H 'Accept-Encoding: gzip' https://example.net/ | grep -i content-encoding

# compare transferred bytes
curl -s -o /dev/null -w "%{size_download}\n" https://example.net/app.js
curl -s -o /dev/null -w "%{size_download}\n" -H 'Accept-Encoding: br' https://example.net/app.js

Expect content-encoding: br on the first line and gzip on the second, plus a much smaller byte count with compression. Browser developer tools show the same in the network panel. If the header is missing, check that the MIME type the server sends (visible in content-type) is in your types list. The curl guide explains these flags.

Should you serve precompressed files?

For static assets that rarely change, yes. Compress them once at the highest level during your build and let the server send the ready-made file, which costs no CPU per request. With gzip_static on; and brotli_static on; Nginx looks for app.js.gz and app.js.br next to app.js.

sudo apt install -y brotli        # or: sudo dnf install -y brotli
cd /var/www/example.net/public
find . -type f \( -name '*.js' -o -name '*.css' -o -name '*.svg' \) \
  -exec gzip -k -9 {} \; -exec brotli -k -q 11 {} \;

The -k flag keeps the original file. Rerun this step whenever the assets change, or the old compressed copy will be served. Dynamic pages still go through on-the-fly compression at a moderate level.

What are the common pitfalls?

  • Double compression: an app behind the proxy already sends gzip. Either let the app do it or the web server, not both.
  • High levels on dynamic pages: level 9 or Brotli 11 on every request burns CPU on small plans. Keep on-the-fly levels around 4 to 6.
  • Missing Vary header: caches may give gzip to a client that asked for nothing. Keep gzip_vary on.
  • Secrets mixed with user input: compressed HTTPS pages that echo user input next to tokens can leak them through size differences (the BREACH class of attacks). Keep CSRF tokens random per request or skip compression on those endpoints.
  • Brotli over plain HTTP: browsers only ask for br over HTTPS, so set up TLS first with the Certbot guide.

Watch CPU in the Usage graphs tab of the console after you turn compression on, and lower the level if it climbs.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1static sites with precompressed assets1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A2-2dynamic sites compressing on the fly2 vCPU, 2 GB RAM, 60 GB NVMe$10.80/mo$64.80 for 6 months

Questions about Compression with gzip and Brotli

Should I use gzip or Brotli?

Use both. Brotli is chosen when the browser offers it, and gzip covers everything else.

Does compression reduce my transfer usage?

Yes, for text. Outgoing transfer counts bytes sent, so smaller HTML, CSS and JavaScript responses use less of the monthly allowance.

Which compression level should I pick?

Around 5 for on-the-fly compression. Use the highest level only for files you compress once in advance.

Why is my JSON API not compressed?

Its content type is missing from gzip_types or the Apache filter list, or the response is smaller than the minimum length.

Can I compress images too?

Not usefully. JPEG, PNG, WebP and video are already compressed. Optimize them with image tools instead.