squid中的refresh_pattern详解

refresh_pattern 大概是 squid 最有意思但最不好懂的配置参数了,经过看书,大概明白如何使用,写出来贡献。

记住refresh_pattern 只对后端没设置Expires过期时间的页面起作用,比如论坛页面;而对类似apache mod_expires 设置过的页面不起作用。

说明之前,先将个概念LM,LM就是页面Header里时间(Date)和Last-Modified时间的差。Date一般是Squid从后面取页面的时间,Last-Modified 一般是页面生成时间。

refresh_pattern 的语法是refresh_pattern [-i] regexp min percent max [options]regexp 就不讲了,大家都明白的;)

min, max的单位是分钟,percent就是百分比。

refresh_pattern 的算法如下:(当前时间定义为CURRENT_DATE)
1) If ((CURRENT_DATE-DATE(就是LM里定义的时间)) < min),cache是新鲜的
2) else if ((CURRENT_DATE-DATE) < (min + (max-min)*percent),cache是新鲜的
3) else cache是过期的
cache过期就需要从后面server取新鲜内容。

如果希望页面一进入cache就不删除,直到被主动purge掉为止,可以加上ignore-reload选项
一般情况可以使用 reload-into-ims。
举例:
refresh_pattern -i \.gif$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.jpg$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.png$ 1440 50% 2880 ignore-reload

refresh_pattern -i \.mp3$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.wmv$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.rm$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.swf$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.mpeg$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.wma$ 1440 50% 2880 ignore-reload

refresh_pattern -i \.css$ 10 50% 60 reload-into-ims
refresh_pattern -i \.js$ 10 50% 60 reload-into-ims
refresh_pattern -i \.xml$ 10 50% 30 reload-into-ims


英文版:
Remember refresh_pattern rules apply only to responses without an explicit expiration time. Origin servers can specify an expiration time with either the Expires header, or the Cache-Control: max-age directive.



refresh_pattern syntax is as follows:

refresh_pattern [-i] regexp min percent max [options]

definitions:

resource age LM = abs(Last-Modified Time - Date) in headers.
LM factor = (Current_Date-Date)/LM
refresh_pattern algorithm:

The response is stale if the response age is greater than the refresh_pattern max value.
The response is fresh if the LM-factor is less than the refresh_pattern percent value.
The response is fresh if the response age is less than the refresh_pattern min value.
Otherwise, the response is stale.
if percent = 50%, see below image for better understanding:


If you want to cache object until it got purged by explicitly command, use ignore-reload option.

you can always cache something for some time using reload-into-ims option.

Examples:
refresh_pattern -i \.gif$ 10 100% 20 ignore-reload
refresh_pattern -i \.jpg$ 10 100% 20 ignore-reload

refresh_pattern -i \.css$ 10 50% 60 reload-into-ims
refresh_pattern -i \.js$ 10 50% 60 reload

评论

Jeff_Cheng说…
我个人的理解是这样的:
DATA就是从服务器取回来的时间
如果我这样定义:
1400 100% 140000 那么在第一天这些数据是最新鲜的,如果过了一天,那么就要检查一下这个数据是否是新的.
比如:
到了第二天,current_date-date=1400
1400+(140000-1400)*100%=140000
说明这些数据还是新鲜的.一直到第100天
Jeff_Cheng说…
refresh_pattern 指令也有少数选项导致 squid 违背 HTTP协议规范。它们如下:
override-expire
该选项导致 squid 在检查 Expires 头部之前,先检查 min 值。这样,一个非零的 min 时
间让 squid 返回一个未确认的 cache 命中,即使该响应准备过期。

override-lastmod
改选项导致 squid 在检查LM-factor百分比之前先检查 min 值。

reload-into-ims
该选项让 squid 在确认请求里,以 no-cache 指令传送一个请求。换句话说,squid 在转
发请求之前,对该请求增加一个If-Modified-Since头部。注意这点仅仅在目标有Last-Modified
时间戳时才能工作。外面进来的请求保留 no-cache 指令,以便它到达原始服务器。

ignore-reload
该选项导致 squid 忽略请求里的任何 no-cache 指令。

此博客中的热门博文

在南京见到的农民工午饭情景

购买了iacces for iPad

在 Mac M1 CPU 上编译ffmpeg,并支持 opencl和videotoolbox