转:www.linuxyunwei.com/2013/06/apache-启用gzip与expire/
Apache有两个模块可对文档进行Gzip压缩,分别为 mod_deflate与mod_zip,两者各有好坏,mod_deflate压缩率略低与mod_zip,但也占用mod_zip资源多,之间取舍看个个需求,本人使用的是 mod_deflate
设置缓存过期时间使用的是 mod_expires 这个模块
查看模块是否已经加载:
Shell
[root@style js]# httpd -M | grep -E "expire|deflate" Syntax OK expires_module (shared) deflate_module (shared) 如果没有输出,请检查模块是否有安装,或是配置文件中是否有加载: LoadModule expires_module modules/mod_expires.so LoadModule deflate_module modules/mod_deflate.so
在apache主配置文件中添加如下内容:
Shell
<IfModule mod_deflate.c> # 设置压缩等级,默认为6(可选1-9,等级1压缩最快,9压缩率最高) DeflateCompressionLevel 6 # 设置需要进行压缩的文件类型 AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/javas cript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/atom_xml AddOutputFilterByType DEFLATE application/x-javas cript AddOutputFilterByType DEFLATE application/x-httpd-php AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilter DEFLATE js css html </IfModule> <IfModule mod_expires.c> ExpiresActive On ExpiresDefault A0 # 如果匹配以下文件时,设置缓存过期时间为一周 <FilesMatch "\.(jpg|jpeg|png|gif|swf|js|css|xml)$"> ExpiresDefault A604800 </FilesMatch> </IfModule>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<IfModule mod_deflate.c>
# 设置压缩等级,默认为6(可选1-9,等级1压缩最快,9压缩率最高)
DeflateCompressionLevel 6
# 设置需要进行压缩的文件类型
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javas
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javas
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilter DEFLATE js css html
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A0
# 如果匹配以下文件时,设置缓存过期时间为一周
<FilesMatch "\.(jpg|jpeg|png|gif|swf|js|css|xml)$">
ExpiresDefault A604800
</FilesMatch>
</IfModule>
|