How To Pre-CompressYour Website With Brotli On Debian 10 And NGINX

If you have followed my guide for enabling brotli compression with NGINX then your server is already serving your content with brotli compression.

The only downside to this is that NGINX compresses the files every time it serves them. A feature of the brotli module is that it can serve files that have already been compressed with brotli. This means that they you only have to compress them once saving your CPU from doing repete work.

If you have the brotli module installed and working you first need to check that the following line is present in your NGINX configuration:

brotli_static on;

If you need to add this then remember to restart NGINX:

systemctl restart nginx

The way that the module works is that it will check if there is a brotli compressed file with the same name as the non-compressed file but ending in .br e.g.:

index.html
index.html.br

If it finds this .br file it will serve the brotli compressed file instead of compressing the non-compressed version.

This means that you now need to create brotli compressed files next to the non-compressed ones.

First, create a back up of your website.

Next, use a command like the following to create new, compressed versions of .html, .css, .xml, .js files:

find <PATH THE SITE CONTENT> -type f | grep -e ".html$" -e ".css$" -e ".xml$" -e ".js$" | xargs brotli -9

And that is all you need to do. NGINX will now serve your site using your pre-compressed files saving your the CPU cycles.