Gzip and content negotiation
Send fewer bytes without changing the document
Gzip is a lossless compression format. A browser can request a compressed representation of a CSS file, then decompress it to obtain the original bytes. Nginx can compress an eligible response while delivering it. This saves network transfer for repetitive text but consumes CPU. An already compressed image or archive may gain little. Content-Type still describes the original media type; Content-Encoding describes the transformation.
GET /dynamic/catalog.txt HTTP/1.1Host: assets.testAccept-Encoding: gzip
HTTP/1.1 200 OKContent-Type: text/plainContent-Encoding: gzipVary: Accept-EncodingA client that explicitly requests identity rather than gzip should receive uncompressed bytes in this lab. Do not equate Content-Encoding with Transfer-Encoding, which concerns message framing. Vary informs a shared cache that the acceptable encoding can influence the selected representation. It is not a cache lifetime and does not by itself make a response cacheable.
Configure a bounded compression policy
location /dynamic/ { gzip on; gzip_types text/plain text/css application/javascript application/json; gzip_min_length 256; gzip_comp_level 3; gzip_vary on;}This uses a moderate example level, not a universal optimum. The gzip level ranges from 1 to 9; larger values may use more CPU. Nginx includes text/html in its eligible types even without listing it. The length threshold is checked using the response Content-Length when present; it is not a reliable minimum for a streamed response of unknown size. The gzip HTTP version and Via-based proxy policy can also affect eligibility. Merely proxying an upstream does not mean the request carries Via.
curl --noproxy '*' -sS -H 'Accept-Encoding: gzip' -o encoded.bin -w '%{size_download}\n' http://127.0.0.1:18280/dynamic/catalog.txtcurl --noproxy '*' -sS -H 'Accept-Encoding: identity' -o plain.bin -w '%{size_download}\n' http://127.0.0.1:18280/dynamic/catalog.txtThese commands write files in the current lab directory. Compare bytes downloaded, not the length of a decoded browser document. Inspect headers separately; do not assume gzip was used just because it was offered. The optional live verifier decodes the first body and compares it with the original fixture.
Avoid compressing secrets with reflected input
location /account/ { gzip off; add_header Cache-Control "no-store" always; # Application handler belongs here.}Compression of secret-bearing responses together with attacker-influenced text can expose information through response lengths, even with TLS. Assess the whole delivery chain and avoid compressing such mixed content; disabling local gzip alone does not undo upstream or CDN compression. Cache-Control no-store is a separate storage policy, not a compression control. Do not use wildcard MIME types or compression everywhere just to increase a ratio.
Practice
Predict whether known-length text of 80 bytes and 4,000 bytes qualify under the example threshold. Explain why an encoded response and identity response must decode to identical content. Next: move compression to a build step.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gzip and content negotiation
Choose gzip for suitable public responses and interpret content negotiation, thresholds, Vary and compression safety.
What does Content-Encoding: gzip describe?
A known-length 80-byte text response has gzip_min_length 256. What happens?
Which response field tells a shared cache encoding negotiation matters?
Why avoid compression on secret-bearing mixed-content pages?
What should determine a compression level?