Nginx开启Gzip压缩大幅提高页面加载速度及相关测试

2019-03-19 02:33|来源: 网络

本来是来自http://www.slyar.com/blog/nginx-gzip-compressed.html的一篇博客,开启gzip对网页的加速是相当明显的。不过这里是基于nginx的gzip配置。下面是转载原文:

 

文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

刚刚给博客加了一个500px相册插件,lightbox引入了很多js文件和css文件,页面一下子看起来非常臃肿,所以还是把Gzip打开了。

环境:Debian 6

1、Vim打开Nginx配置文件

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

2、找到如下一段,进行修改

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]\.";

3、解释一下

第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了

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

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

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

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:13:09 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
X-Pingback: http://www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip

页面成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/photonic/include/css/photonic.css"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:25 GMT
Content-Type: text/css
Last-Modified: Sun, 26 Aug 2012 15:17:07 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:25 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip

css文件成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-includes/js/jquery/jquery.js"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:38 GMT
Content-Type: application/x-javascript
Last-Modified: Thu, 12 Jul 2012 17:42:45 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:38 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip

js文件成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/uploads/2012/08/2012-08-23_203542.png"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:22:45 GMT
Content-Type: image/png
Last-Modified: Thu, 23 Aug 2012 13:50:53 GMT
Connection: keep-alive
Expires: Tue, 25 Sep 2012 18:22:45 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip

图片成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:23:27 GMT
Content-Type: text/css
Content-Length: 180
Last-Modified: Sat, 02 May 2009 08:46:15 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:23:27 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

最后来个不到1K的文件,由于我的阈值是1K,所以没压缩


本文链接:http://www.656463.com/article/500

相关问答

更多
  • 如果是discuz 打开GZIP会加重服务器的CPU占用率。服务器性能不好的建议不要打开。
  • 1、apache环境下开启gzip 找到并打开apache/conf目录中的httpd.conf文件 httpd.conf中打开deflate_Module和headers_Module模块,具体做法为将 如下两句前面的#去掉: LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so 在httpd.conf文件底部加入如下代码配置需要压缩的文件: SetOutputFi ...
  • 系统默认是开启的。。
  • IIS6.0启用Gzip压缩的方法: 1、新建Web服务扩展(如下图) dll路径:“c:windowssystem32inetsrvgzip.dll”,然后启用。 2、网站服务中开启HTTP压缩支持(如下图) 临时目录需要给IIS用户读写权限。 3、修改IIS配置文件MetaBase.xml 文件路径:“c:windowssystem32inetsrv”(请先备份至他处),打开后搜索“HcDynamicCompressionLevel”,并修改(确定已备份)“Compression/deflate”和“C ...
  • 服务器本身是已开启这个功能的,不需要你再开启了。
  • Gzip开启以后会将输出到用户浏览器的数据进行压缩的处理,这样就会减小通过网络传输的数据量,提高浏览的速度。
  • gzip就是加速网站访问速度,提高用户体验的一种方式,值得注意的一点是,它是一个处理器端的行动,也就是说,它是经由进程处理器的功能来对网页进行压缩的,而它所须要的客户端支持仅仅是浏览器须要支gzip,仅此而已!此外,百度站长俱乐部也明确表态说:“百度支持GZIP压缩”。很多大型网站,都开启了gzip解压。这个与优化没有半毛钱关系,楼主大可放心。
  • 你好,对于现在的开源程序都有相关插件可以实现,想wordpress dede这些都有相关插件可以实现,你可以搜索一下这些插件, 还有网站不要安装太多的插件,注意不必要的插件不要安装,现在的空间商都支持gzip压缩的!希望我的回答对你有一定的帮助
  • 首先要根据你的服务器类型(win、linux),安装gzip环境,然后在config配置里打开该功能。
  • 更改此行: gzip_types text/plain application/x-javascript text/xml text/css; 为此: gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css; 请注意在您的gzip类型列表中添加了application/javascript和text/javascript 。 在这里发布的答案 ...