在使用之前,首先要确认一下”mod_expires”模组是否有启用.如果是自己安装apache来架设网页主机的话,这里我们可以透过编辑apache的”httpd.conf”设定档来处理.搜寻一下,你可能会找到这么一行:
#loadmodule expires_module modules/mod_expires.so
复制代码
将该行前面的”#”字号删除,然后将”httpd.conf”设定档储存后,重新启动apache来使这个更新生效.
当然如果我们是租用虚拟主机的话,”httpd.conf”设定档我们一般用户是接触不到的,而在网站根目录里写个”.htaccess”设定档, 我想在运用上相对是较灵活的.”mod_expires”的设定资料除了可以写在apache的”httpd.conf”设定档中,也可以写 在”.htaccess”设定档里.
我们知道在使用浏览器浏览网页时,浏览器会把网页资料快取(cache)下来储存在本机端,用以加快下回浏览相同网页时不必再重新由网站上下载,进 而有加速的效果.使用mod_expires模组来加速网页浏览,这里所谓的”加速”,其实是利用”mod_expires”的功能,来设定网页文件的过 期时间,加长网页文件被浏览器快取(cache)保存的时间.如此一来,只要网页文件的过期时间未到,浏览器就会引用快取的资料,而不用花时间再去下载网 站上的资料.另一方面带给站长的好处是,可以减少浏览者对网站的流量耗用(例如有些虚拟主机有限制网站可使用的流量).
接下来我们直接由范例中来学习吧.
范例一:
<ifmodule mod_expires.c>
expiresactive on
expiresdefault “access plus 10 days”
expiresbytype text/css “access plus 1 second”
</ifmodule>
复制代码
范例二:
<ifmodule mod_expires.c>
expiresactive on
expiresdefault a86400
expiresbytype image/x-icon a2592000
expiresbytype application/x-javascript a2592000
expiresbytype text/css a2592000
expiresbytype image/gif a604800
expiresbytype image/png a604800
expiresbytype image/jpeg a604800
expiresbytype text/plain a604800
expiresbytype application/x-shockwave-flash a604800
expiresbytype video/x-flv a604800
expiresbytype application/pdf a604800
expiresbytype text/html a900
</ifmodule>
复制代码
范例三:
<ifmodule mod_expires.c>
expiresactive on
expiresdefault a0
# 1 年
<filesmatch “\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$”>
expiresdefault a9030400
</filesmatch>
# 1 星期
<filesmatch “\.(jpg|jpeg|png|gif|swf)$”>
expiresdefault a604800
</filesmatch>
# 3 小时
<filesmatch “\.(txt|xml|js|css)$”>
expiresdefault a10800″
</filesmatch>
</ifmodule>
复制代码
使用<ifmodule></ifmodule>来包夹指令可以避免在mod_expires模组没有启用的情况下还要去 执行.如果mod_expires模组确定已经启用,那不写<ifmodule></ifmodule>也没关係.
expiresactive on是指启用mod_expires功能,相对的off就是关闭功能.
expiresdefault指令是设定预设的过期时间.
从范例一和范例二中,各位可以看到关于时间设定的方式有分两种,一个是文字叙述型,一个是代码加上秒数型.
文字叙述型:
“access plus 10 days”意指浏览时起算10天.依照apache官方说明文件,过期起算时间有三种,分别是access、now 以及modification.其中access与now意义相同,而modification指的是网页文件的”最后编辑时间”.所以如果要以档桉的最 后编辑时间起算,可以写成这样,”modification plus 10 days”.而时间的指定也很简单,就是英文单字(years、months、weeks、days、hours、minutes、seconds).例 如,可以写成这样,”access plus 1 month 15 days 2 hours”.
代码加上秒数型:
a86400意指浏览时起算1天.格式是代码加上秒数.代码有分两种,”a”等同”access”,意指浏览时起算过期时间.使用代码”a”比较适合应用 在不常变动的网页文件类型,例如图片.另外一种代码是”m”,其意义等同于”modification”,指的是网页文件的”最后编辑时间”.使用代 码”m”比较适合应用在经常变动的网页文件类型,例如html页面这类经常更新内容的资料.秒数资料我在文后附上参考资料,可以供各位快速参考.
expiresbytype指令是依照不同的网页文件型态来做过期时间设定.
例如,expiresbytype text/css a2592000,意指网站上的css风格档3天后过期; expiresbytype image/gif a604800,意指网站上的gif图档在7天后过期.
范例三里,使用了<filesmatch></filesmatch>来包夹各型态的网页文件,而不是使用”expiresbytype”指令,这也是一种用法.
利用apache模块mod_expires和mod_headers实现文件缓存,add an expires header|为文件头指定expires
利用apache模块mod_expires和mod_headers实现文件缓存,add an expires header|为文件头指定expires
大家在使用yslow的网站速度优化,常会看到add an expires header这一条分值很低,搜索很多但还不知道怎么该。下面就是答案。
add an expires header / 为文件头指定expires
给静态文件加上过期标志。让浏览器或者cdn服务器缓存起来,加速图片和其他静态文件的加载。
expires是浏览器cache机制的一部分,浏览器的缓存取决于header中的四个值: cache-control, expires, last-modified, etag。
优化这个选项,所要做的是对站内所有的文件有针对性的设置cache-control和expires.
我们要实现加上过期标志可以利用apache模块mod_expires和mod_headers。
通过配置.htaccess文件, 可以轻易地按文件类别设置缓存时间。对提高网站速度有一定帮助。
1. 利用mod_expires
在.htaccess中添加如下语句:
<ifmodule mod_expires.c>
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
</ifmodule>
但有一个问题是我们常用的apache主机经常不怎么支持mod_expires,没有关系,我们用另一个模块使用mod_headers。
同样在.htaccess文件中添加如下内容可以实现缓存:
<ifmodule mod_headers.c>
# htm,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>
以下为样本代码:
<filesmatch “\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$”>
header set cache-control “max-age=604800, public”
</filesmatch>
<filesmatch “\.(xml|txt)$”>
header set cache-control “max-age=18000, public, must-revalidate”
</filesmatch>
<filesmatch “\.(html|htm|php)$”>
header set cache-control “max-age=3600, must-revalidate”
</filesmatch>
以上就是apache启用mod_expires模块的内容。