跳至主要內容

站点优化之开启Gzip压缩

牧歌...大约 2 分钟

站点优化之开启Gzip压缩

配置Nginx

放在service里

image.png|650
image.png|650
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 6;
gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";

解释一下

第1行:开启Gzip

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

第3行:buffer,就是,嗯,算了不解释了,不用改

第4行:用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1

第5行:压缩级别,1-10,数字越大压缩的越好,时间也越长,看心情随便改吧

第6行:进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了

第7行:跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding",我不需要这玩意,自己对照情况看着办吧

第8行:IE6对Gzip不怎么友好,不给它Gzip了

验证、保存、退出

使用nginx的测试命令来验证下nginx.conf的配置是否正确

/usr/local/nginx/sbin/nginx -t

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

重新加载

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

或者使用下面命令,重新加载

systemctl reload nginx

或者重启

service nginx restart
image.png|950
image.png|950

访问一下

第一种方式是直接查看网络请求,打开浏览器的调试工具,查看 Network 请求,如果请求响应头的 Content-Encoding 字段为 gzip,就表示成功开启了 Gzip: image.png|950

第二种方式是通过站长工具验证,打开网页GZIP压缩检测open in new window,输入网站,进行查询:

image.png|950
image.png|950

#nginx配置


完~

上次编辑于:
贡献者: wenleiwang
评论
  • 按正序
  • 按倒序
  • 按热度