52 Nginx的Gzip模块配置指令(三)
52 Nginx的Gzip模块配置指令(三)
52.1 gzip_disable
gzip_disable:针对不同种类客户端发起的请求,可以选择性的开启和关闭Gzip功能
| 语法 | gzip_disable regex ...; |
| 默认值 | - |
| 位置 | http 、 server 、location |
regex:根据客户端的浏览器标志(user-agent)来设置支持使用正则表达式,指定的浏览器标志不使用Gzip,该指令一般用来排除一些明显不支持Gzip的浏览器
浏览器访问:http://10.0.0.100/jquery.js ,jquery.js 压缩为 7.9KB,浏览器开头为:Mozilla/5.0

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; gzip_types application/javascript; gzip_comp_level 6; gzip_vary on; gzip_disable 'Mozilla/5.0.*'; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/jquery.js ,jquery.js 还原为不压缩的 249KB

52.2 gzip_http_version
gzip_http_version:针对不同的HTTP协议版本,可以选择性地开启和关闭Gzip功能
| 语法 | gzip_http_version 1.0 | 1.1; |
| 默认值 | gzip_http_version 1.1; |
| 位置 | http 、server 、location |
注意:gzip_http_version 指定使用Gzip的HTTP最低版本,该指令一般采用默认值即可
52.3 gzip_min_length
gzip_min_length:针对传输数据的大小,可以选择性地开启和关闭Gzip功能
| 语法 | gzip_min_length length; |
| 默认值 | gzip_min_length 20; |
| 位置 | http 、 server 、location |
nginx计量单位:bytes[字节] 、kb[千字节] 、 M[兆] 例如: 1024 、 10 k|K 、 10 m|M
浏览器访问:http://10.0.0.100/jquery.js ,还原配置,jquery.js 压缩为 7.9KB,查看浏览字节长度
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; gzip_types application/javascript; gzip_comp_level 6; gzip_vary on; # 防止实验干扰 #gzip_disable 'Mozilla/5.0.*'; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; gzip_types application/javascript; gzip_comp_level 6; gzip_vary on; # 防止实验干扰 #gzip_disable 'Mozilla/5.0.*'; gzip_min_length 248941; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
浏览器访问:http://10.0.0.100/jquery.js ,原文件没有超过 248941,所以未进行压缩

注意:Gzip压缩功能对大数据的压缩效果明显,但如果要压缩的数据比较小,可能出现越压缩数据量越大的情况,要根据响应内容的大小来决定是否使用Gzip功能,响应页面的大小可以通过头信息中的 content-length 获取,如果使用了 Chunk编码动态压缩,该指令将被忽略,建议设置为 1K 或以上
52.4 gzip_proxied
gzip_proxied:设置是否对服务端返回的结果进行Gzip压缩
| 语法 | gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any |
| 默认值 | gzip_proxied off; |
| 位置 | http 、server 、location |
off:关闭nginx服务器对后台服务器返回结果的Gzip压缩
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:无条件启用压缩
———————————————————————————————————————————————————————————————————————————
无敌小马爱学习
浙公网安备 33010602011771号