Precompressed assets and optional Brotli
Compress assets once during a build
A static asset may remain unchanged for weeks. Instead of spending CPU on each request, build a matching gzip file once. Deploy both original and compressed representations from the same release; a stale compressed file can deliver old application code even when the ordinary file was updated. Keep the two modification times aligned and verify that decompression produces the exact original bytes.
gzip -n -c app.css > app.css.gztouch -r app.css app.css.gzgzip -t app.css.gzThe -n flag omits the original name and timestamp from the gzip header; touch synchronizes filesystem timestamps. The original file remains available. A gzip integrity test alone does not prove that the file corresponds to the current app.css; compare the decompressed bytes too.
location /prebuilt/ { gzip off; gzip_static on; gzip_vary on;}gzip_static requires its optional module. With on, Nginx can select a .gz sibling for an accepting client; an ordinary file is used otherwise. The URL remains app.css and the content type remains CSS. Runtime gzip is off here so an absent compressed sibling does not trigger a new compression operation. A direct request for the .gz filename is a different URL; the fixture denies direct access.
Compressed-only storage is a separate design
location /compressed-only/ { gzip_static always; gunzip on; gzip_vary on;}The always mode skips the client capability check in gzip_static; gunzip can decode the representation for incompatible clients when its module is available. Do not turn on always without a verified delivery strategy. Decompression adds work, and range and validator behavior should be checked for both variants. The main lab uses the simpler two-file deployment instead.
Brotli is an optional compatibility lane
brotli_static on;brotli off;The Google ngx_brotli project and F5 NGINX Plus module documentation describe optional static and filter modules. A build may include neither. Verify the package, binary compatibility, loaded modules and supported directives before using the fragment. Brotli uses the br content coding and commonly a .br sibling. Do not assume gzip and Brotli negotiation priorities across different builds; test clients that accept either, both or neither. No Brotli dependency is installed in this lab.
Practice
Describe a release that updates app.css but forgets app.css.gz. Design a build gate that compares decompressed content and verifies both client variants. Next: optimize file transfer without changing content.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Precompressed assets and optional Brotli
Build and verify precompressed assets, distinguish gzip_static and gunzip, and recognize optional Brotli modules.
What does gzip_static on look for?
What is the safest deployment check for app.css.gz?
What is risky about gzip_static always alone?
What must be verified before enabling brotli_static?
When gzip is off and gzip_static on but no .gz sibling exists, what is the simple expected result for an existing ordinary file?