您的位置:首页 > 运维架构 > Apache

Apache配置静态缓存

2017-06-14 17:20 489 查看
Apache配置静态缓存
这里的静态文件指的是图片js、css等文件,用户访问一个站点,其实大多数元素都是图片js、css等,这些静态文件其实是会被客户端的浏览器缓存到本地电脑上的,目的就是为了下次请求时不在去服务器上下载,这样就加快了速度,提高了用户体验。但这些静态的文件总不能一直缓存,他有一些时效性,那么这就是过期时间。
配置如下:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/x-javascript "now plus 2 hours"
ExpiresByType application/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
或者使用mod_headrs.c模块实现
<IfModule mod_headrs.c>
#htn.html.txt类的文件缓存一个小时
<filesmatch "\.(html|htm|txt)$">
header set cache-control "max-age=3600"
</filesmatch>
#css,js,swf类的文件缓存一个星期
<filesmatch "\.(css|js|swf)$">
header set cache-control "max-age=604800"
</filesmatch>
#jpg,gif,jpeg,png,ico,flv,pdf等文件缓存一年
<filesmatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
header set cache-control "max-age=29030400"
</filesmatch>
</IfModule>
说明:这里的时间单位可以是days、hours甚至是min,两种不同的方法,上面使用的是 mod_expires,而下面用的是mod_headers,要想使用这些模块,必须要事先已经支持。
如何查看是否支持,使用命令:
# /usr/local/apache2/bin/apachectl -M
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  配置 Apache 静态缓存