Ngx_http_gzip_module(压缩模块):基于gzip方式对响应报文作压缩;
官方文档:http://nginx.org/en/docs/http/ngx_http_gzip_module.html
官方定义:The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half or even more.
相关指令:
| gzip: | 定义是否启用“gzip”压缩功能,默认不启用; |
| gzip_buffers: | 定义设置用于压缩响应的缓冲区数量和大小,默认值:gzip_buffers 32 4k|16 8k,代表多少个缓冲区(number),每个的大小为多少(size); |
| gzip_comp_level: | 设置响应gzip压缩级别,压缩级别1~9之间;默认值:gzip_comp_level 1; |
| gzip_disable: | 定义“User-Agent”请求进行正则表达式匹配,User-Agent表示浏览器相关版本等,通过User-Agent检测避开压缩支持不好的浏览器; |
| gzip_min_length: | 设置一个响应压缩的最小长度;大于此数字进行压缩;默认:gzip_min_length 20; |
| gzip_http_version: | 定义HTTP协议版本进行压缩,默认http_version 1.1; |
| gzip_proxied: | Nginx作为代理服务器时启用,设置参数; |
| gzip_types: | 定义压缩的响应内容MIME类型;默认:gzip_types text/html; |
| gzip_vary: | 定义是否在发送客户端的响应头部插入“Vary:Accept-Encoding”响应信息,用于客户端浏览器识别内容是否已经进行压缩;默认:gzip_vary off; |
其中gzip_proxied指令:
| Syntax: | gzip_proxied |
|---|---|
| Default: | gzip_proxied off; |
| Context: | http, server, location |
off:对代理请求不进行压缩;
expired:定义响应报文首部包含“Expires”头,则启用压缩功能;(Expires定义资源过期时间);
no-cache:定义如果Cache-Control中包含“no-cache”,则启用压缩功能;no-cache,缓存遵循web服务器验证
no-store:定义如果Cache-Control中包含“no-store”,则启用压缩功能;no-store,禁止缓存
private:定义如果Cache-Control中包含“private”,则启用压缩功能;
no_last_modified:定义包含“Last-Modified”首部,启用压缩功能;
no_etag:定义包含“ETag”首部,启用压缩功能;
auth:定义包含Authorization首部,则启用压缩功能;
any:全部请求都做压缩;
相关设置:
[root@GaoServer ~]# vim /etc/nginx/nginx.conf
......
#定义相关配置段在“Context:http,server,location;”
gzip on;
gzip_buffers 32 4k; #可以不设置,默认使用默认值;
gzip_comp_level 5; #压缩比越大,过程资源消耗越大;
#gzip_disable; #官方建议“msie6”针对IE设置,也可以通过正则表达式匹配“MSIE [4-6]\.”
#gzip_proxied any;
gzip_min_length 64;
gzip_types text/css; #默认text/html;
......
#查看相关MIME类型
[root@GaoServer ~]# cat /etc/nginx/mime.types
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
......
[root@GaoServer ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@GaoServer ~]# nginx -s reload本文出自 “Gning丶” 博客,请务必保留此出处http://gning.blog.51cto.com/11847592/1980895
Nginx基于gzip压缩配置参数(Ngx_http_gzip_module)
原文:http://gning.blog.51cto.com/11847592/1980895