Controlling the Expiration of Content in Caches¶
Module: mod_expire
Description:
mod_expire controls the Expire and Cache-Control: max-age headers in the Response Header of HTTP/1.0 and HTTP/1.1
messages. It is useful to set it for static files which should be cached
aggressively like images, stylesheets or similar.
Options:
expire.url
assigns a expiration to all files below the specified path. The
specification of the time is made up of:
<access|modification> <number> <years|months|days|hours|minutes|seconds>
where access means time of user access and modification means time of file modification. Follows the syntax used by mod_expire in Apache 1.3.x and later. (See: http://httpd.apache.org/docs/1.3/mod/mod_expires.html) Example:
expire.url = ( "/images/" => "access 1 hours" )
Example to include all sub-directories:
$HTTP["url"] =~ "^/images/" {
expire.url = ( "" => "access 1 hours" )
}
Troubleshoot:
It is known that mod_expire may not work due to an incorrect order of loading of modules. One instance is that mod_expire is loaded after mod_fastcgi. The solution is simple, it is to move mod_expire within the modules array in front of mod_fastcgi. Note: The order of the modules is loaded from top to bottom.
Symptoms of the above scenario is the server starts up fine but fails to serve content.
In 1.4.13 (and probably others) you must load mod_expire BEFORE mod_compress. Otherwise, expires headers will NOT be output when serving a compressed document.
Also, expire.url = ( "/images/" => "access 1 hours" ) did not work for me. I had to use the second form.
I tested in 1.4.18 and above two problem(one is order mod_expire and mod_compress and the other is expire time specification setting) is not appeared.
文章来源:http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModExpire
我的测试:
过期时间(expire)是HTTP响应(response)的头部控制信息,浏览器会在指定过期时间内使用本地缓存,不用重新加载,对应的缓存对象主要是静态文件,如css,image, java; 可以在服务器端进行全局设置,也可以应用端进行设置,这里我会说下服务器端的设置
打开:"mod_expire",去掉前面的注释:"#"
#========================================================================================#
#------------------配置 expire 模块 设置过期时间----------------------------------------------#
#-----语法:<access|modification> <number> <years|months|days|hours|minutes|seconds>
#========================================================================================#
#data2及其子目录的规则:访问过期2小时
$HTTP["url"] =~ "^/data2/" {
#expire.url = ( "" => "modification 2 hours" ) #这个在浏览器访问会一次200(读取服务器文件) 一次304(读取本地缓存)轮流替换
expire.url = ( "" => "access 6 hours" ) #这个第一次200 第二次304 以后就一直本地
}
有关Etag和Expires的文档,参见这里
所以mod_expire要配合Etag使用
etag 默认的配置 是根据系统的inode 可以选择为:modified-time
如果服务器有多台的话,就得配置:
etag.use-inode = "disable"
etag.use-mtime = "enable" #使用修改时间,每台服务器的修改时间得一致,才能达到目的
我们现在配置的是:
#启用etag
static-file.etags = "enable"
#使用文件修改时间
etag.use-mtime = "enable"
#禁用系统inode
etag.use-inode = "disable"
另外官方解释:
Description
defines if the ETag generation algorithm will use the inode value of the file.
If you have multiple lighttpd servers serving static content then you should use
etag.use-inode = "disable"
So that the ETag value is consistent across the lighttpd clusters. Otherwise you will generate lesser number of 304 responses
Do make sure that the static content on all your clusters have the same mtime (modified-time)
Default: enabled