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