Apache2运用mod_deflate提升网络页面浏览速度

来源:互联网 发布:2016淘宝66大促 编辑:程序博客网 时间:2024/06/16 03:02

apache2已经用mod_deflate代替了mod_gzip apache2已经包含了mod_deflate 打开模组 /usr/local/apache2/bin/apxs -i -c /yourpath/httpd-2.2.3/modules/filters/mod_deflate.c httpd.conf添加 LoadModule deflate_module modules/mod_deflate.so 如果想只压缩html,text,xml 如果下面配置: AddOutputFilterByType DEFLATE text/html text/plain text/xml 下面给出我的配置档: LoadModule deflate_module modules/mod_deflate.so SetOutputFilter DEFLATE 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 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 在apache的使用手册上还有这么一句,但是不知道为什么不能添加上去. # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary 这个可以压缩 SVN 出来的文件。 ——————————————— 不能添加的原因是下面的模组没有打开: LoadModule headers_module modules/mod_headers.so ================= Apache deflate模块配置说明前几天一直在找apache2.0以dso方式编译后加载deflate模块的办法试了apxs -ica mod_deflate.c N次,始终是报 Cannot load /opt/apache/modules/mod_deflate.so into server: /opt/apache/modules/mod_deflate.so: undefined symbol: deflate 异常的痛苦,什么ldd mod_deflate.so后再export LIB_LIBRARY_PATH呀,都试了N次,google也go了N天。终于在google上go出来一篇文章,终于解决,方法如下: vi /usr/local/apache2/bin/apr-config 修改LDFLAGS=" " 为 LDFLAGS="-lz" 然后再apxs -ica mod_deflate.c 就OK了另外在配置deflate规则时 apache2.0推荐加上这句 Header append Vary User-Agent env=!dont-vary 以便确保不会输出在压缩过程中出现内容方面的error 但一般情况下,在测试apache的语法过程中会现出 Invalid command ‘Header’, perhaps mis-spelled or defined by a module not included in the server configuration 解决办法就是加载一个headers模块就OK了 不过最好还是在编译apache模块时直接加上–enable-deflate –enable-headers就省事多了。 一、 需求压缩apache的输出内容,降低网站带宽二、 加载Apache的deflate模块 1、 修改apr-config vi /usr/local/apache2/bin/apr-config 修改LDFLAGS=” ” 为 LDFLAGS=”-lz” 2、 到apache源安装目录下,例如 cd /root/httpd-2.0.55/modules/filters 3、加载mod_deflate模块 /usr/local/apache2/bin/apxs -i -a -c mod_deflate.c 如果没有安装headers模块,加载headers模块 cd /root/httpd-2.0.55/modules/metadata 加载mod_headers模块 /usr/local/apache2/bin/apxs -i -a –c mod_headers.c 三、配置Apache主配置文件 1. 在httpd.conf主配置文件里添加如下行 #声明输入流的byte数量 DeflateFilterNote Input instream #声明输出流的byte数量 DeflateFilterNote Output outstream #声明压缩的百分比 DeflateFilterNote Ratio ratio #声明日志类型 LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate CustomLog logs/deflate_log deflate #指定压缩参数 # Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4/.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine # BrowserMatch /bMSIE !no-gzip !gzip-only-text/html # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48 # the above regex won't work. You can use the following # workaround to get the desired effect: BrowserMatch /bMSI[E] !no-gzip !gzip-only-text/html # Don't compress images SetEnvIfNoCase Request_URI /.(?:gif|jpg|cab|jpe?g|exe|bmp|mp3|rar|zip|swf|png)$ no-gzip dont-vary # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary 二、日志输出格式如下 [root@trffic2 logs]# tail -f access_log "GET /apache_pb.gif HTTP/1.1" -/- (-%) "GET /manual/ HTTP/1.1" 2163/7434 (29%) "GET /manual/style/css/manual.css HTTP/1.1" 3973/18335 (21%) "GET /manual/style/css/manual-loose-100pc.css HTTP/1.1" 1006/2882 (34%) "GET /manual/style/css/manual-print.css HTTP/1.1" 2994/13017 (23%) _________________ apache1.3.x可以用mod_gzip进行优化网页浏览的速度,可以明显的感觉到速度的提升。在apache2中也尝试用mod_gzip,但是配置后确发现网页不能正确显示(空白页),所以改换mod_deflate。在Linux命令行下运行以下命令安装mod_deflate模块(斜体是apache2的目录) /usr/local/apache2/bin/apxs -i -c /root/httpd-2.0.48/modules/filters/mod_deflate.c 编辑httpd.conf,加入以下内容: LoadModule deflate_module modules/mod_deflate.so DeflateFilterNote ratio LogFormat '"%v %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" (%{ratio}n)' deflate # Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine BrowserMatch /bMSIE !no-gzip !gzip-only-text/html # Don't compress images SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary # Make sure proxies don't deliver the wrong content # Header命令不对?注释这一行 # Header append Vary User-Agent env=!dont-vary #查找Customlog 注释原来的的一行,改成 CustomLog logs/deflate_log deflate #查看logs目录下deflate_log文件,你可以看到许多这样的信息: "www.21php.com 220.163.107.88 - - [27/Mar/2004:01:03:55 -0800] "GET /ad/usrlogo/21PHPLOGO_88X31.gif HTTP/1.1" 200 3656 "http://soft.ttee.com/vbb/" "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)"" (-) "www.21php.com 211.144.88.138 - - [27/Mar/2004:01:03:57 -0800] "GET /tutorial/tutorial.php?tid=68 HTTP/1.1" 200 5173 "http://www.21php.com/tutorial/tutorial.php?catalogid=12" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MyIE2; .NET CLR 1.0.3705; Alexa Toolbar)"" (29) "www.21php.com 211.144.88.138 - - [27/Mar/2004:01:03:58 -0800] "GET /style.cssHTTP/1.1" 304 - "http://www.21php.com/tutorial/tutorial.php?tid=68" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MyIE2; .NET CLR 1.0.3705; Alexa Toolbar)"" (-) "www.21php.com 211.144.88.138 - - [27/Mar/2004:01:03:58 -0800] "GET /images/bar-4.gif HTTP/1.1" 304 - "http://www.21php.com/tutorial/tutorial.php?tid=68" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MyIE2; .NET CLR 1.0.3705; Alexa Toolbar)"" (-) "www.21php.com 211.144.88.138 - - [27/Mar/2004:01:03:58 -0800] "GET /images/logo.gif HTTP/1.1" 304 - "http://www.21php.com/tutorial/tutorial.php?tid=68" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MyIE2; .NET CLR 1.0.3705; Alexa Toolbar)"" (-) "www.21php.com 211.144.88.138 - - [27/Mar/2004:01:03:58 -0800] "GET /images/icon1.gif HTTP/1.1" 304 - "http://www.21php.com/tutorial/tutorial.php?tid=68" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; MyIE2; .NET CLR 1.0.3705; Alexa Toolbar)"" (-) Boban 写于2004年3月27日凌晨 附注:关于mod_deflate的详细内容,可以参考http://httpd.apache.org/docs-2.0/mod/mod_deflate.html __________________ 如果apache是rpm安装的,那么默认就已经有mod_deflate这个模块了。更详细的可以看这里:http://sysoev.ru/mod_deflate/readme.html 因为mod_deflate相比较mod_gzip来说,有了更多的更新,所以我用上了它!注意:mod_gzip在apache1.x上用起来比较好,但是在apache2.x上用起来就不是很爽,老出错,也许是我的水平问题,没办法,所以改用:mod_deflate 我的apache:2.0.55 配置如下:(修改:httpd.conf) DeflateCompressionLevel 9 AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php AddOutputFilter DEFLATE js css BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4/.0[678] no-gzip BrowserMatch /bMSI[E] !no-gzip !gzip-only-text/html Header append Vary User-Agent env=!dont-vary DeflateFilterNote deflate_ratioLogFormat “%v %h %l %u %t /”%r/” %>s %b mod_deflate: %{deflate_ratio}n pct.” vhost_with_deflate_infoCustomLog logs/order.7180.com-access_log vhost_with_deflate_info 将以上的配置文字加入到:之间即可:) 以上的做法是压缩一般的网页html、xml、php、css、js等格式文件的输出,虽然会占用一点服务器的资源,不过这个可以减少带宽被占用的情况:) 这里:DeflateCompressionLevel 9是指压缩程度的等级,从1到9,9是最高级别的。这样做可以压缩到20%的大小,最少也有50%。至于jpg等已经压缩过的文件,就不需要再压缩了。 转:http://blog.penner.cn/2007/04/04/apache2-mod_deflate.html

原创粉丝点击