# 开启gzip
gzip on;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 1;
# 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
# 禁用IE 6 gzip
gzip_disable "MSIE [1-6]\.";
# 设置压缩所需要的缓冲区大小
gzip_buffers 32 4k;
# 设置gzip压缩针对的HTTP协议版本,没做负载的可以不用
# gzip_http_version 1.0;
# 开启缓存
location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
access_log off;
expires 2d;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
access_log off;
expires 24h;
}
location ~* ^.+\.(html|htm)$ {
expires 1h;
}
location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
access_log off;
expires max;
}
# 格式
# expires 30s;
# expires 30m;
# expires 2h;
# expires 30d;
说明
gzip on
打开或关闭 gzip。
Syntax: gzip on | off; Default: gzip off; Context: http, server, location, if in location
gzip_buffers
设置用于处理请求压缩的缓冲区数量和大小。比如 32 4K 表示按照内存页(one memory page)大小以 4K 为单位(即一个系统中内存页为 K),申请 32 倍的内存空间。建议此项不设置,使用默认值。
Syntax: gzip_buffers number size; Default: gzip_buffers 32 4k|16 8k; Context: http, server, location
gzip_comp_level
设置 gzip 压缩级别,级别越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大。
Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http, server, location
gzip_disable
通过表达式,表明哪些 UA 头不使用 gzip 压缩。
Syntax: gzip_disable regex ...; Default: — Context: http, server, location This directive appeared in version 0.6.23.
gzip_min_length
当返回内容大于此值时才会使用 gzip 进行压缩,以 K 为单位,当值为 0 时,所有页面都进行压缩。
Syntax: gzip_min_length length; Default: gzip_min_length 20; Context: http, server, location
gzip_http_version
用于识别 http 协议的版本,早期的浏览器不支持 gzip 压缩,用户会看到乱码,所以为了支持前期版本加了此选项。默认在 http/1.0 的协议下不开启 gzip 压缩。
Syntax: gzip_http_version 1.0 | 1.1; Default: gzip_http_version 1.1; Context: http, server, location
在应用服务器前,如果还有一层 Nginx 的集群作为负载均衡,在这一层上,若果没有开启 gzip。
如果我们使用了 proxy_pass 进行反向代理,那么 nginx 和后端的 upstream server 之间默认是用HTTP/1.0协议通信的。
如果我们的 Cache Server 也是 nginx,而前端的 nginx 没有开启 gzip。
同时,我们后端的 nginx 上没有设置 gzip_http_version 为 1.0,那么 Cache 的 url 将不会进行 gzip 压缩。
gzip_proxied
Nginx 做为反向代理的时候启用:
- off – 关闭所有的代理结果数据压缩
- expired – 如果 header 中包含 "Expires" 头信息,启用压缩
- no-cache – 如果 header 中包含 "Cache-Control:no-cache" 头信息,启用压缩
- no-store – 如果 header 中包含 "Cache-Control:no-store" 头信息,启用压缩
- private – 如果 header 中包含 "Cache-Control:private" 头信息,启用压缩
- no_last_modified – 启用压缩,如果 header 中包含 "Last_Modified" 头信息,启用压缩
- no_etag – 启用压缩,如果 header 中包含 "ETag" 头信息,启用压缩
- auth – 启用压缩,如果 header 中包含 "Authorization" 头信息,启用压缩
- any – 无条件压缩所有结果数据
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
gzip_types
设置需要压缩的MIME类型,如果不在设置类型范围内的请求不进行压缩。
Syntax: gzip_types mime-type ...; Default: gzip_types text/html; Context: http, server, location
gzip_vary
增加响应头 "Vary: Accept-Encoding"
告诉接收方发送的数据经过了压缩处理,开启后的效果是在响应头部添加了 Accept-Encoding:gzip,这对于本身不支持 gzip 压缩的客户端浏览器有用。
Syntax: gzip_vary on | off;
Default:
gzip_vary off;
Context: http, server, location