日期:2014-05-17  浏览次数:20807 次

使用Apache模块mod_deflate加快页面打开速度

mod_deflate 模块提供了DEFLATE 输出过滤器,允许服务器在将输出内容发送到客户端以前进行压缩,以节约带宽。

修改httpd.conf

1.使用此模块
LoadModule deflate_module modules/mod_deflate.so

2.在conf文件底下加上

#set deflate modules
#<ifmodule mod_deflate.c>
#DeflateCompressionLevel 9??? #设置压缩率
#AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
#AddOutputFilter DEFLATE css js
#</ifmodule>?????????????

?

#以上设置,经过测试后感觉不太好,没有对css和js文件进行压缩。

?

<location />
SetOutputFilter DEFLATE

AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilter DEFLATE css?????? #压缩css文件
AddOutputFilter DEFLATE js????????? #压缩js文件

#BrowserMatch ^Mozilla/4 gzip-only-text/html
#BrowserMatch ^Mozilla/4\.0[678] no-gzip
#BrowserMatch \bMSIE !no-gzip !gzip-only-text/html?????#针对不同浏览器的配置,这里用处不大

# Don't compress images?????????????????????????????????????????? #对照片文件不进行压缩处理
SetEnvIfNoCase Request_URI (?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI (?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary
#SetEnvIfNoCase Request_URI .(css|js)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</location>

DeflateFilterNote Input input_info???????????????????????????????? #设置日志输出!?????????
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog logs/deflate_log.log deflate

?

?以下是日志输出 deflate_log.log

"GET /23413 HTTP/1.1" 206/268 (76%)
"GET /123 HTTP/1.1" 205/266 (77%)
"GET /favicon.ico HTTP/1.1" 209/274 (76%)
"GET / HTTP/1.1" 75/73 (102%)
"GET /new/index.jsp HTTP/1.1" 6155/27871 (22%)
"GET /new/css/index1.css HTTP/1.1" 2028/7958 (25%)
"GET /new/css/fonts.css HTTP/1.1" 212/279 (75%)
"GET /js/include.js HTTP/1.1" 2106/5425 (38%)
"GET /new/js/inc.js HTTP/1.1" 1264/3983 (31%)
"GET /search/js/search.js HTTP/1.1" 356/730 (48%)
"GET /search/js/selectarea.js HTTP/1.1" 27757/168335 (16%)
"GET /new/images/new/new_02.gif HTTP/1.1" -/- (-%)
"GET /new/images/new/new_16.gif HTTP/1.1" -/- (-%)
"GET /new/images/new/new_11.gif HTTP/1.1" -/- (-%)
"GET /new_iflogin?tt=1195799219343 HTTP/1.1" 4/2 (200%)
"GET /new/images/bian1.gif HTTP/1.1" -/- (-%)
"GET /new/js/5adpics.js HTTP/1.1" 1068/3845 (27%)
"GET /new/images/1.jpg HTTP/1.1" -/- (-%)
"GET /new/images/userimg_03.gif HTTP/1.1" -/- (-%)
"GET /new/images/4.jpg HTTP/1.1" -/- (-%)

可以看到除了图片jpg格式文件外,其他的jsp,css,js 等都进行了压缩处理!