打开http.conf文件,首先开启gzip模块:
LoadModule deflate_module modules/mod_deflate.so
然后,配置何时使用gzip压缩
<IfModule mod_deflate.c>
# 压缩等级 9
DeflateCompressionLevel 9
# 压缩类型 html、xml、php、css、js
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript application/x-httpd-php
AddOutputFilter DEFLATE js css
</IfModule>
重启apache进行测试
测试代码如下:
1 <html> 2 <body> 3 </body> 4 <div id="div1">1234</div> 5 <input type="button" value="ajax请求php页面" onclick="display()" /> 6 <script src="./js/zepto.min.js" type="text/javascript" charset="utf-8"></script> 7 <script type="text/javascript" charset="utf-8"> 8 function display() 9 { 10 $.ajax( 11 { 12 type:"post", 13 url:"newfile.php", 14 data:"des=hello", 15 success:function(msg){alert(msg);} 16 } 17 ); 18 } 19 </script> 20 </body> 21 </html>
未开启:gzip情况下zepto.min.js回送的信息如下:
Request URL:http://localhost/zybook/mytest/js/zepto.min.js
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:localhost
Pragma:no-cache
Referer:http://localhost/zybook/mytest/zepto.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.60 Safari/537.1
Response Headersview source
Accept-Ranges:bytes
Connection:Keep-Alive
Content-Length:24014
Content-Type:application/javascript
Date:Mon, 06 Aug 2012 02:58:22 GMT
ETag:"1b000000010dca-5dce-4c65a8edc6347"
Keep-Alive:timeout=5, max=96
Last-Modified:Fri, 03 Aug 2012 11:09:13 GMT
Server:Apache/2.2.22 (Win64) PHP/5.3.13
开启gzip得到的回送信息
Request URL:http://localhost/zybook/mytest/js/zepto.min.js
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:localhost
Pragma:no-cache
Referer:http://localhost/zybook/mytest/zepto.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.60 Safari/537.1
Response Headersview source
Accept-Ranges:bytes
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:8622
Content-Type:application/javascript
Date:Mon, 06 Aug 2012 02:59:51 GMT
ETag:"1b000000010dca-5dce-4c65a8edc6347"
Keep-Alive:timeout=5, max=87
Last-Modified:Fri, 03 Aug 2012 11:09:13 GMT
Server:Apache/2.2.22 (Win64) PHP/5.3.13
Vary:Accept-Encoding
计算一下压缩率在64%
附上其它的一些方法:
加速blog:GZIP压缩传输你的文件http://cotine.blog.hexun.com/45757173_d.html
加快页面载入速度的方法【一】 使用GZIP压缩功能 http://hi.baidu.com/susheshop/blog/item/34380700d8c1f60a728da573.html
PHP开启gzip页面压缩实例代码:http://www.jb51.net/article/22497.htm