brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nginx gzip_static serves your precompressed .gz files instead of compressing on every request

Nginx
~/tips/tip-nginx-gzip-static

gzip -k -9 dist/app.css dist/app.js

Sends ready-made .gz versions of static files, compressed once at maximum level.

With gzip on, nginx compresses a CSS or JS file again for every visitor, at a modest level to save CPU. For files that never change between deploys, you can compress them once at the highest level and let nginx send those.

gzip -k -9 dist/app.css dist/app.js
location /assets/ {
    root /srv;
    gzip_static on;
}

-k keeps the original next to app.css.gz. When a browser sends Accept-Encoding: gzip, nginx looks for the .gz file and serves it with Content-Encoding: gzip; clients without gzip get the plain file. Deliberately different content in the two files shows which one comes back, and nginx picks the right one each time.

It needs the http_gzip_static_module. The official Docker image and most distribution packages have it; nginx -V shows --with-http_gzip_static_module if yours does.

nginx does not compare the two files. If your build step updates app.css but leaves an old app.css.gz, visitors with gzip get the old version, which is a confusing bug. I’d make the compress step part of the build and delete stale .gz files in the same run. Keep plain gzip on as well for dynamic responses. Brotli is not included in the stock nginx build, so .br files need an extra module.

Further reading: ngx_http_gzip_static_module

Going deeper: 🛜 Guide: AdGuard Home + Tailscale (Secure Split-Horizon DNS)

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link