修改httpd.conf开启gzip压缩传输,本文在CentOS 5.5下配置
下面是具体的步骤
1. 首页查看一下mod_deflate、mod_headers两个模块是否开启,
cat /etc/httpd/conf/httpd.conf | grep mod_deflate
#输出LoadModule deflate_module modules/mod_deflate.so
cat /etc/httpd/conf/httpd.conf | grep mod_headers
#输出LoadModule headers_module modules/mod_headers.so
如果列出文字前都带#号,修改httpd.conf去除
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
2. 在httpd.conf末尾加入下面文字,并保存
SetOutputFilter DEFLATE
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|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/*
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript
注解:
SetOutputFilter DEFLATE
设置压缩等级,可设置为1到9.数字越大压缩比例越高,相应cpu也越累
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|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
以上三行分别设置对图片、压缩文件、视频文件等格式的文件不使用gzip压缩
AddOutputFilterByType DEFLATE text/*
对文字启用压缩
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript
对js启用压缩
3. 重启httpd
service httpd restart