« Back to Web

Pre-compress static assets with Brotli and Gzip

Gzip

Gzip is well-known and around for some time. Almost all web clients support it and the savings are typically very high.

Compressing a file with gzip is simple, especially with the command often already installed on Linux systems.

gzip --best --force filename.html

This compresses the file as good as it can (–best) and overwrite a .gz file if it already exists (–force).

Brotli

Brotli is a fairly new kid on the block when it comes to compressing files. So besides having Gzip, it is a good idea to pre-compress files also with Brotli. The files are generally smaller than those compressed with Gzip.

Note: Typically the brotli tool is not installed by default. Use your package manager to see if it is available. Otherwise download it via the Brotli project on GitHub.

After installing Brotli, it is time to run it against a file:

brotli --best --force filename.html

This command will compress our HTML file and apply the best possible compression using –best. The –force will overwrite the compressed file if it already exists. This might be useful when the original file has been changed and needs to be compressed again.

If you want to automate things and pre-compress a whole directory of static files, consider using find. Then search for files and apply the compression to those files.

Example of a find command to apply the changes to recently changed files.

find /home/web/linux-audit.com/public/ -type f -mmin -15 -regextype posix-extended -iregex '.*\.(css|html|js|json|txt|xml)' -exec brotli --best --force {} \+

That is a line full with things, right? Let’s have a look at the individual parts of this command.

Find optionAction
-type fOnly search for files
-mmin -15Only files that are changed in the last 15 minutes
-regextype posix-extendedSelect specific type of regular expression format
‘.*.(csshtml
-exec brotli –best –force {} +Execute a task on the files that have been found

Using it in a nginx configuration

If you want to use pre-compressed files, it is important to configure nginx correctly. That starts with making sure you got the right support. For example, a modern Ubuntu system might have a pre-compiled module available that can be installed.

apt install libnginx-mod-http-brotli-static

Next step is adding the support for static files to your /etc/nginx/nginx.conf in the http directive the following:

# Added brotli/gzip support for static pre-compressed files
brotli_static on;
gzip_static on;

Test if compression is available

Using the curl tool we can easily perform a request and tell the web server that we like to receive back a compressed version of the page.

curl --head --header 'Accept-Encoding: br' https://linux-audit.com/

Check How to test if a website supports Brotli or Gzip for additional tips

Got other suggestions for pre-compressing files? Let it know!

Relevant commands in this article

Like to learn more about the commands that were used in this article? Have a look, for some there is also a cheat sheet available.

Feedback

Small picture of Michael Boelen

This article has been written by our Linux security expert Michael Boelen. With focus on creating high-quality articles and relevant examples, he wants to improve the field of Linux security. No more web full of copy-pasted blog posts.

Discovered outdated information or have a question? Share your thoughts. Thanks for your contribution!

Mastodon icon