rger 发表于 2017-11-13 14:37:06

Nginx基于gzip压缩配置参数(Ngx_http_gzip_module)

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 off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth |any ...;

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:全部请求都做压缩;
相关设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# vim /etc/nginx/nginx.conf
......
#定义相关配置段在“Context:http,server,location;”
    gzip on;
    gzip_buffers 32 4k;    #可以不设置,默认使用默认值;
    gzip_comp_level 5;    #压缩比越大,过程资源消耗越大;
    #gzip_disable;    #官方建议“msie6”针对IE设置,也可以通过正则表达式匹配“MSIE \.”
    #gzip_proxied any;
    gzip_min_length 64;   
    gzip_types text/css; #默认text/html;
......
#查看相关MIME类型
# cat /etc/nginx/mime.types
types {
    text/html                        html htm shtml;
    text/css                           css;
    text/xml                           xml;
    image/gif                        gif;
......
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload






页: [1]
查看完整版本: Nginx基于gzip压缩配置参数(Ngx_http_gzip_module)