#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.59
# the above regex won’t work. You can use the following
# workaround to get the desired effect:
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html force-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don’t compress images and other
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|doc)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/x-javascript
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary
#DeflateFilterNote ratio ratio_info
#LogFormat '"%v %h %l %u %t “%r" %>s %b "%{Referer}i" "%{User-Agent}i"" (%{ratio}n)' deflate
#CustomLog logs/deflate_log deflate
但是本人没有添加header模块,在重启httpd时候提示
Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration 需要操作1:/usr/local/apache2/bin/apxs -i -c /home/httpd-2.0.63/modules/metadata/mod_headers.c或者/usr/local/apache2/bin/apxs -i -c /home/httpd-2.0.63/modules/filters/mod_headers.c2:增加LoadModule headers_module modules/mod_headers.so 下面摘抄一下网上介绍的在linux下开启gzip压缩功能的文章内容:
而如果是 Linux/Unix 操作系统,如果你没有在编译安装的过程中将所需要的两个模块 mod_deflate 和 mod_headers 编译进去的话,那就稍微有点麻烦,首先我们先看如何在编译安装 Apache 过程中也同时编译这两个模块,请在执行 configure 程序时增加两个参数分别是:
# ./configure –enable-deflate –enable-headers
这样在编译完 Apache 后就可以直接在 httpd.conf 中启用并配置 deflate 模块了,配置的方法跟 Windows 平台下是相同的。
如果说您的 Apache 已经在运行了,不想再重新编译一次,那也可以选择只编译 deflate 模块所需的文件 mod_deflate.c 和 mod_headers.c。这两个文件位于 {apache-src}/modules/filters/ 目录下(其中 {apache-src} 为 apache 源文件所在的目录)。使用如下命令来单独编译这两个源文件。
# {apache-bin}/apxs -i -a -c {apache-src}/modules/filters/mod_deflate.c
# {apache-bin}/apxs –i –a –c {apache-src}/modules/filters/mod_headers.c
When install the header module the src directory is {apache-src}/modules/metadata/mod_headers.c
So we need change the command as follows:{apache-bin}/apxs -i -a -c {apache-src}/modules/metadata/mod_headers.c
其中 {apache-bin} 为 Apache 安装目录下的 bin 目录,接下来在 httpd.conf 直接配置该模块即可。
很多时候你在单独编译 deflate 模块的时候可能会碰到编译错误,提示是:
Cannot load /opt/apache/modules/mod_deflate.so into server: /opt/apache/modules/mod_deflate.so: undefined symbol: deflate
解决的方法如下:
编辑 /usr/local/apache2/bin/apr-config 文件修改其中的 LDFLAGS 值为 “-lz”,然后再重新编译 mod_deflate 模块,apxs -ica mod_deflate.c 即可。
为了省却不必要的麻烦,请尽量在编译安装时直接加上 –enable-deflate –enable-headers 参数。
检验压缩效果:
http://gzip.zzbaike.com/
检测结果是否压缩?是压缩类型:gzip源文件大小:77.6KB压缩后大小:15.67KB压缩率: 79.8%
Header信息DateFri, 07 Jan 2011 06:05:42 GMTServerApache/2.2.17 (Unix) PHP/5.2.14Accept-RangesbytesCache-Controlmax-age=300ExpiresFri, 07 Jan 2011 06:10:42 GMTVaryAccept-Encoding,User-AgentContent-EncodinggzipContent-Length16082Content-Typetext/html