linux利用apache模块mod_expires和mod_headers实现文件缓存
2017-04-03
服务器
通过配置.htaccess文件, 可以轻易地按文件类别设置缓存时间。对提高网站速度有一定帮助。
1. 利用mod_expires
在.htaccess中添加如下语句:
expiresactive on
#默认所有文件缓存时间设置为300秒
expiresdefault a300
#html,plain-text缓存300秒
expiresbytype text/html a300
expiresbytype text/plain a300
#css, javascript缓存一个小时
expiresbytype text/css a3600
expiresbytype application/x-javascript a3600
#图标文件缓存30天
expiresbytype image/x-icon a2592000
#image类缓存一个星期
expiresbytype image/jpeg a604800
expiresbytype image/gif a604800
expiresbytype image/png a604800
#其它文件缓存一个星期
expiresbytype application/x-shockwave-flash a604800
expiresbytype video/x-flv a604800
expiresbytype application/pdf a604800
2. 使用mod_headers
同样在.htaccess文件中添加如下内容可以实现缓存:
# htm,html,txt类的文件缓存一个小时
header set cache-control "max-age=3600"
# css, js, swf类的文件缓存一个星期
header set cache-control "max-age=604800"
# jpg,gif,jpeg,png,ico,flv,pdf等文件缓存一年
header set cache-control "max-age=29030400"
1. 利用mod_expires
在.htaccess中添加如下语句:
expiresactive on
#默认所有文件缓存时间设置为300秒
expiresdefault a300
#html,plain-text缓存300秒
expiresbytype text/html a300
expiresbytype text/plain a300
#css, javascript缓存一个小时
expiresbytype text/css a3600
expiresbytype application/x-javascript a3600
#图标文件缓存30天
expiresbytype image/x-icon a2592000
#image类缓存一个星期
expiresbytype image/jpeg a604800
expiresbytype image/gif a604800
expiresbytype image/png a604800
#其它文件缓存一个星期
expiresbytype application/x-shockwave-flash a604800
expiresbytype video/x-flv a604800
expiresbytype application/pdf a604800
2. 使用mod_headers
同样在.htaccess文件中添加如下内容可以实现缓存:
# htm,html,txt类的文件缓存一个小时
header set cache-control "max-age=3600"
# css, js, swf类的文件缓存一个星期
header set cache-control "max-age=604800"
# jpg,gif,jpeg,png,ico,flv,pdf等文件缓存一年
header set cache-control "max-age=29030400"