Nginx Compression
nginx(8) is able to seamlessly compress data before sending over the wire. This reduces the amount of traffic, at the cost of computation time.
Contents
Unit Measurements
In nginx(8) configuration files, size and offset are measured in...
- bytes (no suffix)
kilobytes (k or K suffix)
megabytes (m or M suffix)
gigabytes (g or G suffix)
To be clear, a size or offset without any suffix will be treated as a measurement in bytes.
Usage
Gzip
The gzip directive belongs in the http block and takes either on or off.
For many deployments, this is all the configuration needed to get 'good enough' results.
http { gzip on; }
Gzip_Buffers
Gzip_Comp_Level
Gzip_Disable
The gzip_disable directive disables compression based on the user agent. It takes a regex that should match user agents for which compression is disabled.
http { # Disable compression for Internet Explorer up to version 6 gzip_disable "MSIE [1-6]\."; # A special value that is equivalent to the regex "MSIE [4-6]\." but faster gzip_disable "msie6"; }
Gzip_Http_Version
The gzip_http_version directive disables compression based on the version of HTTP used.
The default is:
http { gzip_http_version 1.1 }
This is because HTTP 1.0 does not allow for simulatenously keepalive and compression.
Gzip_Min_Length
The gzip_min_length directive sets a threshold based on a response's "Content-Length" header. Below this threshold, data is sent uncompressed.
http { gzip_min_length 1024; }
Gzip_Proxied
The gzip_proxied directive causes data to be compressed for proxied connections.
By default this is not done because the proxy should cache that data itself. That proxy already is a solution for reducing traffic. The computation time of (1) nginx(8) compressing the data and (2) the proxy decompressing the data scales poorly.
The directive takes one or more of the following values:
Setting |
Meaning |
expired |
compress even if a response has an "Expires" header set to a value that disables caching |
no-cache |
compress even if a response has a "Cache-Control" header set to "no-cache" |
no-store |
compress even if a response has a "Cache-Control" header set to "no-store" |
private |
compress even if a response has a "Cache-Control" header set to "private" |
no_last_modified |
compress even if a response does not have a "Last-Modified" header |
no_etag |
compress even if a response does not have an "ETag" header |
auth |
compress even if a response has an "Authorizationn" header |
Gzip_Type
Gzip_Vary
The gzip_vary directive causes both compressed and uncompressed data to be cached. This is only useful if some data won't be compressed.
http { gzip_vary on; }
See also
Module ngx_http_gzip_module documentation