CentOS下Apache开启Gzip网页压缩功能

时间:2022-01-11 13:17:50

版本


Centos版本:6.5

Apache版本:2.2.15

步骤


1、进入/etc/httpd/conf下打开httpd.conf文件

开启Gzip压缩功能,即去掉LoadModule deflate_module modules/mod_deflate.so这行前面的#号,默认已经去掉。

2、进行Gzip压缩设置,添加:

# mod_deflate:
<ifmodule mod_deflate.c>
DeflateCompressionLevel
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE image/svg+xml
</ifmodule>

其中DeflateCompressionLevel 6为压缩率,取值1-9之间,默认为6.

3、重启apache

service httpd restart

4、如果网站使用的是CodeIgniter框架,需要在config文件中进行设置,将

$config['compress_output'] = FALSE;
改为:
$config['compress_output'] = TRUE;

参考:CentOS下Apache开启Gzip网页压缩功能