码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx服务器开启GZip压缩的方法

时间:2016-08-19 11:21:03      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

常用配置片段如下:


   gzip             on;
gzip_comp_level  2;                        # 压缩比例,比例越大,压缩时间越长。默认是1
gzip_types       text/css text/javascript; # 哪些文件可以被压缩
gzip_disable     "MSIE [1-6].";           # 无视IE6这个笨蛋~

其中 gzip_types 选项默认只压缩 text/html,源码见:


   src/http/modules/ngx_http_gzip_filter_module.c 行152:     &ngx_http_html_default_types[0] src/http/ngx_http.c 行77:     ngx_str_t  ngx_http_html_default_types[] = {         ngx_string("text/html"), 

常用的静态 type 有,看自己需要压缩的情况而定:


   text/html
text/plain
text/css
application/x-javascript
text/javascript
application/xml

OK,到这里基本服务端已经配置完毕,Nginx 只需要 reload 一下即可。

下面来测试一下,用 curl 来如何测试服务端已经开启 gzip(测试条件是默认gzip_types,即只压缩 text.html ,其他 type 未压缩):


   查看是否开启gzip,需要客户端加入:"Accept-Encoding: gzip, deflate" 头信息。$ curl -I -H "Accept-Encoding: gzip, deflate" "http://localhost/tag.php" HTTP/1.1 200 OK
Server: nginx
Date: Thu, 08 Mar 2012 07:23:46 GMT
Content-Type: text/html
Connection: close
Content-Encoding: gzip$ curl -I -H "Accept-Encoding: gzip, deflate" "http://localhost/style.css" HTTP/1.1 200 OK
Server: nginx
Date: Thu, 08 Mar 2012 07:23:54 GMT
Content-Type: text/css
Connection: close
Last-Modified: Tue, 27 Dec 2011 10:00:51 GMT

 

鼎峰胡佳雄
QQ.2881064155
Skype.live:2881064155

Nginx服务器开启GZip压缩的方法

标签:

原文地址:http://www.cnblogs.com/supper-hjx/p/5786778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!