Module ngx_http_gzip_module
Example Configuration
Directives
[gzip]
[gzip_buffers]
[gzip_comp_level]
[gzip_disable]
[gzip_min_length]
[gzip_http_version]
[gzip_proxied]
[gzip_types]
[gzip_vary]
[gzip_no_buffer] [Embedded Variables]
$gzip_ratio
Example Configuration
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;
Directives
* gzip Syntax: gzip on | off; Default: gzip off; Context: http, server, location, if in location #是否启用压缩 * gzip_buffers Syntax: gzip_buffers number size; Default: gzip_buffers 32 4k|16 8k; Context: http, server, location #设置压缩缓存的数量大小与 platform memory page 相关 #获取系统缓存也大小: $getconf PAGE_SIZE * gzip_comp_level Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http, server, location #压缩级别:1~9 1:压缩最快,压缩比小 9:最慢,压缩比大,消耗CPU资源 * gzip_disable Syntax: gzip_disable regex ...; Default: — Context: http, server, location # regex匹配$User-Agent时不压缩 * gzip_min_length Syntax: gzip_min_length length; Default: gzip_min_length 20; Context: http, server, location #最小压缩长度 比较“$Conteng-Length” * gzip_http_version Syntax: gzip_http_version 1.0 | 1.1; Default: gzip_http_version 1.1; Context: http, server, location #此处强调wget http/1.0协议,curl http/1.1协议, 如果走默认version,wget 无法测试 需要指定gzip_http_version 1.0; 得到Content-Encoding:gzip 就没有"Content_Length"响应头 使用1.0版本,Keepalive 将无效; 使用1.1版本,chunked 传递处理; * gzip_proxied Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...; Default: gzip_proxied off; Context: http, server, location 根据请求 & 响应判断时候支持响应压缩,匹配$via 进行判断 > off #拒绝压缩所有代理请求 > expired #response head include expired && 且值为不缓存 则压缩 > no-cache #cache-control:no-cache 则压缩 > no-store > private > no_last_modified #不含Last-Modified > no_etag #不含etag > auth #request include Authorizaton > any #任何代理请求都压缩 * gzip_types Syntax: gzip_types mime-type ...; Default: gzip_types text/html; Context: http, server, location #已经包含text/html,空格分隔。 *:添加所有MIME类型 * gzip_vary Syntax: gzip_vary on | off; Default: gzip_vary off; Context: http, server, location #是否添加"Vary:Accept-Encoding" response_head (如果开启导致IE4~6不缓存) # gzip_no_buffer Syntax: gzip_no_buffer on|off Default: off Context: http,server,location #默认ngx返回client数据会进行等待,直到gzip_buffers填满,开启会禁用缓存
Embedded Variables
$gzip_ratio: 实现压缩比,计算为原始和压缩响应大小的比值。