日期:2014-05-17  浏览次数:20735 次

apache设置文件缓存
使用google的page speed分析,有个leverage browser caching条,说是么有设置缓存有效期。The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources。

网上搜到相关设置说明。

利用mod_expires模块为文件增加Expires

加载mod_expires模块,在配置文件中增加:

LoadModule expires_module modules/mod_expires.so

可在.htaccess文件中添加如下内容

<IfModule mod_expires.c>
ExpiresActive on

#默认所有文件缓存时间设置为300秒
ExpiresDefault a300

#html,plain-text缓存300秒
ExpiresByType text/html a300
ExpiresByType text/plain a300

#javascript缓存一个小时
ExpiresByType application/x-javascript “access plus 2 hours”

#css缓存一个天
ExpiresByType text/css “access plus 2 days”

#图标文件缓存30天
ExpiresByType image/x-icon “access plus 1 month”

#image类缓存一个星期
ExpiresByType image/jpeg “access plus 1 week”
ExpiresByType image/gif “access plus 1 week”
ExpiresByType image/png “access plus 1 week”

#flash/pdf缓存一个星期
ExpiresByType application/x-shockwave-flash “access plus 1 week”
ExpiresByType video/x-flv “access plus 1 week”
ExpiresByType application/pdf “access plus 1 week”

</IfModule>