400-1513-886

全国统一服务热线

系统运维

Nginx开启Gzip压缩大幅提高页面加载速度

发布时间:2019-01-26 22:50:13 1152 次

Vim打开Nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

找到#gzip on进行修改

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";


第1行:开启Gzip

第2行:不压缩临界值,大于1K的才压缩,一般不用改

第3行:buffer

第4行:用了反向代理的话,末端通信是HTTP/1.0

第5行:压缩级别,1-10,数字越大压缩的越好,时间也越长

第6行:进行压缩的文件类型

第7行:跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding"

第8行:IE6对Gzip不兼容,不压缩

 

4、:wq保存退出,重新加载Nginx

/usr/local/nginx/sbin/nginx -s reload

 

5、用curl测试Gzip是否成功开启

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.tongtian.tech"

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Sat, 26 Jan 2019 14:56:03 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
X-Powered-By: PHP/7.0.31
Set-Cookie: PHPSESSID=ie6kjh6bm44o2ec78k5tkaoth6; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Encoding: gzip

页面成功压缩

扫码进入小程序

公司名称:广州统天网络科技有限公司

公司地址:广州市白云区石井镇潭村水闸街6号

公司电话:400-1513-886

公司邮箱:info@tongtian.tech