tl;dr; for Ubuntu (>=24.10) run apt install libnginx-mod-http-brotli-static

Nginx is the web server I use on my personal web server. It sits in front of static files and Python and Node backend servers.
For static files, you can make it server the files compressed with Brotli, if the equivalent file exists on disk as a .br file.
Suppose you have this:


server {
    root /path/to/my/app/dist;
    server_name myapp.example.com;

    brotli_static on; // THIS!

    location / {
        try_files $uri /index.html;
    }

    ...
}

Now, if you request https://myapp.example.com/staticassets/foo.css Nginx will see if there's a file called /path/to/my/app/dist/staticassets/foo.css.br and if the request has br in the Accept-Encoding header.

What you need to do to make this possible is to compress all your static assets. For example:


cd /path/to/my/app/dist
brotli staticassets/*.css

On Ubuntu, when you install nginx it won't enable this brotli_static directive, by default. Thankfully you don't need to compile the code any more. You can just run:


apt install libnginx-mod-http-brotli-static

I don't know when that came into existence but previously I had to compile it myself and move a certain compiled file into /etc/nginx/modules-enabled/ and hope it works.

Comments

Your email will never ever be published.

Previous:
Object.keys to return the known strings of an object in TypeScript October 25, 2024 JavaScript
Next:
An ideal pattern to combine React Router with TanStack Query November 18, 2024 React, JavaScript
Related by category:
set -ex - The most useful bash trick of the year August 31, 2014 Linux
Be very careful with your add_header in Nginx! You might make your site insecure February 11, 2018 Linux, Nginx
Linux tip: du --max-depth=1 September 27, 2007 Linux
How to use letsencrypt-acme-challenge.conf in Nginx September 5, 2021 Nginx
Related by keyword:
How slow is Node to Brotli decompress a file compared to not having to decompress? January 19, 2024 Linux, Node, macOS
How I added brotli_static to nginx 1.17 in Ubuntu (Eoan Ermine) 19.10 April 9, 2020 Linux, Nginx