ngx_http_gzip_static_module

Serve precompressed .gz files instead of compressing on the fly

compiled inBSD-2-Clausesource docs

Overview

ngx_http_gzip_static_module sends precompressed files with the ".gz" filename extension instead of the regular files. For a request to /app.js it checks for /app.js.gz and, when the client accepts gzip, serves that file with the matching Content-Encoding and no CPU spent compressing.

gzip_http_version, gzip_proxied, gzip_disable and gzip_vary from ngx_http_gzip_module are taken into account when deciding whether to use the compressed file. The files can be produced with the gzip command or any compatible tool, and upstream recommends keeping the modification date and time of the original and compressed files the same. This site also ships Brotli and Zstandard modules (/modules/brotli/ and /modules/zstd/) that serve pre-compressed .br and .zst files in the same way.

It is an official nginx module. Upstream does not build it by default and enables it with the --with-http_gzip_static_module configure parameter. A typical use is a build pipeline that emits .gz copies of JavaScript, CSS and font assets next to the originals.

Key directives

gzip_staticEnables (on) or disables (off) checking for precompressed files, or serves them unconditionally (always); default off.
gzip_static alwaysSince 1.3.6, uses the gzipped file in all cases without checking client support; pair with gunzip or all-.gz trees.
.gz lookup ruleThe compressed variant is the requested filename with a ".gz" extension appended, compressed by gzip or a compatible tool.
gzip_* awarenessgzip_http_version, gzip_proxied, gzip_disable and gzip_vary are all taken into account.
matching mtimeUpstream recommends that original and compressed files share the same modification date and time.

Example

nginx.conf
location /assets/ { root /var/www/site; # serve app.js.gz when the client accepts gzip gzip_static on; gzip_vary on; gzip_proxied expired no-cache no-store private auth; expires 30d; }

Availability

Compiled into every n.wtf nginx binary. Install nginx-extras and the directives above work out of the box, no load_module line needed.