默认情况下,Nginx的gzip压缩是关闭的, gzip压缩功能就是可以让你节省不少带宽,但是会增加服务器CPU的开销,Nginx默认只对text/html进行压缩 ,如果要对html之外的内容进行压缩传输,我们需要手动调整。
一.nginx资源文件压缩模块介绍
基于gzip实现资源文件压缩模块:
发送给客户端的资源结果做压缩:
ngx_http_gzip_module
整个网站内容压缩了:
ngx_http_gzip_static_module
需要编译:--with-http_gzip_static_module
此模块的作用就是在接到请求后,会到url相同的路径的文件系统去找扩展名为“.gz”的文件,如果不存在,再将文件进行gzip压缩,再发送出去,这样可以避免重复的压缩无谓的消耗资源,这个模块不受gzip_types限制,会对所有请求有效。所以建议不要在全局上使用,因为一般来说大部分都是动态请求,是不会有.gz这个文件的,建议只在局部我们确认有.gz的目录中使用。
我们常用的是对发送给客户端的资源结果做压缩,那我们就关注一下ngx_http_gzip_module模块;
二.gzip压缩模块详解
1.模块安装
gzip压缩模块是ngx_http_gzip_module,是nginx内置的。
2.模块指令
决定是否开启gzip模块
gzip
语法:
Syntax:gzip on | off;
默认关闭:
Default:gzip off;
配置段:
Context:http, server, location, if in location
设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间
gzip_buffers
语法:
Syntax:gzip_buffers number size;
默认值:
Default:gzip_buffers 32 4k|16 8k;
配置段:
Context:http, server, location
设置gzip压缩等级,等级越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大
gzip_comp_level
语法:
Syntax:gzip_comp_level level;
默认压缩比是1:
Default:gzip_comp_level 1;
配置段:
Context:http, server, location
当返回内容大于此值时才会使用gzip进行压缩,以K为单位,当值为0时,所有页面都进行压缩
gzip_min_length
语法:
Syntax:gzip_min_length length;
默认值是20byte:
Default:gzip_min_length 20;
配置段:
Context:http, server, location
用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,所以为了支持前期版本加了此选项,目前此项基本可以忽略
gzip_http_version
语法:
Syntax:gzip_http_version 1.0 | 1.1;
默认版本是1.1:
Default:gzip_http_version 1.1;
配置段:
Context:http, server, location
Nginx做为反向代理的时候启用
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 #启用压缩,如果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 #无条件压缩所有结果数据
设置需要压缩的MIME类型,非设置值不进行压缩
gzip_types
语法:
Syntax:gzip_types mime-type ...;
默认压缩html文件:
Default:gzip_types text/html;
配置段:
Context:http, server, location
3.nginx官网实例
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;
解释:
开启gzip压缩
最小压缩文件大小是1000字节
如果header中包含”Cache-Control:no-cache”头信息,”Cache-Control:no-store”头信息,Cache-Control:private”头信息,“Authorization”头信息就启用压缩
压缩的类型为text和xml文件
三.http首部报文模块
1.模块介绍
ngx_http_headers_module模块提供了两个重要的指令add_header和expires,来添加 “Expires” 和 “Cache-Control” 头字段,对响应头添加任何域字段。add_header可以用来标示请求访问到哪台服务器上。expires指令用来对浏览器本地缓存的控制。
2.模块指令
add_header指令
语法:
Syntax:add_header name value [always];
默认值:
Default:—
配置段:
Context:http, server, location, if in location
对响应代码为200,201,204,206,301,302,303,304,或307的响应报文头字段添加任意域。如:
add_header From stu31.com
expires指令
语法:
Syntax:expires [modified] time;
expires epoch | max | off;
默认关闭:
Default:expires off;
应用配置段:
Context:http, server, location, if in location
在对响应代码为200,201,204,206,301,302,303,304,或307头部中是否开启对“Expires”和“Cache-Control”的增加和修改操作。
可以指定一个正或负的时间值,Expires头中的时间根据目前时间和指令中指定的时间的和来获得。
epoch表示自1970年一月一日00:00:01 GMT的绝对时间,max指定Expires的值为2037年12月31日23:59:59,Cache-Control的值为10 years。
Cache-Control头的内容随预设的时间标识指定:
·设置为负数的时间值:Cache-Control: no-cache。
·设置为正数或0的时间值:Cache-Control: max-age = #,这里#的单位为秒,在指令中指定。
参数off禁止修改应答头中的”Expires”和”Cache-Control”。
实例:对图片,flash文件在浏览器本地缓存30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
四.对虚拟主机启用gzip压缩和自定义http首部报文
1.修改虚拟主机配置文件,加入gzip压缩和http首部报文
[root@www ~]# vim /etc/nginx/extra/nginx-vhost.conf
server {
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_comp_level 7;
gzip_buffers 16 8k;
gzip_types text/plain text/html application/xml application/json;
gzip_min_length 1024;
listen *:80 default_server;
server_name www.stu31.com;
index index.html index.htm ;
root /www/vhosts/www1;
add_header X-header TestHeader2014;
access_log /var/log/nginx/www.stu31.com.log main ;
location /status {
stub_status on;
auth_basic "Nginx-status";
auth_basic_user_file /etc/nginx/.htpasswd;
allow 172.16.0.0/16;
deny all;
}
}
2.语法检查:
[root@www ~]# service nginx configtest
nginx: [warn] duplicate MIME type "text/html" in /etc/nginx/extra/nginx-vhost.conf:8
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
原因:text/html根本就不需要写的,gzip默认就会压缩它的,只不过以前的nginx版本不提示这个警告而已,新版本的会出这个警告。
3.重启nginx服务:
[root@www ~]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
4.连接测试:
可以看见响应报文头部内有自定义的:
[root@www ~]# curl -I http://172.16.31.40
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Sat, 27 Dec 2014 05:56:57 GMT
Content-Type: text/html
Content-Length: 27
Last-Modified: Sat, 27 Dec 2014 02:09:31 GMT
Connection: keep-alive
ETag: "549e14db-1b"
X-header: TestHeader2014
Accept-Ranges: bytes
压缩也是成功的:
至此,nginx的资源压缩模块和http头部报文模块介绍完毕。
转载于:https://blog.51cto.com/sohudrgon/1596696