Camille Hodoul

Use Brotli compression with Nginx on Debian | Camille Hodoul

Use Brotli compression with Nginx on Debian

June 21, 2024

Brotli is a general-purpose compression algorithm, arguably better than gzip for the web.
It is supported on all major evergreen browsers.

To have Nginx compress your web pages and assets with Brotli, you need to install the module and enable it in the configuration.

Despite what can be read online, if you’re running Debian and you’re using the official Debian nginx package, you don’t have to compile the module yourself.

Install the package

apt install libnginx-mod-http-brotli-filter

This package is available from Debian 12 “Bookworm” (at the time of writing, the “stable” release).

Include the module

If /etc/nginx/modules-enabled is already included in your nginx configuration, then you can skip to the next part.
Otherwise, add this line to your configuration (for example /etc/nginx/nginx.conf):

include /etc/nginx/modules-enabled/50-mod-http-brotli-filter.conf;

Brotli configuration directives

Edit the http { ... } block in your nginx configuration with the new brotli directives. For example:

# ...
http {
    # ...

    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

    # ...
}

Configuration reference

Check response headers

Restart nginx ( sudo systemctl restart nginx) then check the response headers of your website. You should see:

Content-Encoding: br

Camille Hodoul

I'm a JavaScript and PHP developer living in Grenoble, France.
Links