您的位置:首页 > 运维架构 > 反向代理

squid缓存动态文件及cache-control参数

2010-06-13 14:14 302 查看
大家都知道squid会自动缓存静态文件,可对于这种动态网页怎么让它也缓存起来呢,所以在google上找啊找,找到上面提得那片缓存Google Earth的博客文章。
他的方法是:

acl QUERY urlpath_regex cgi-bin \? intranet
acl forcecache url_regex -i kh.google keyhole.com
no_cache allow forcecache
no_cache deny QUERY
# ----
refresh_pattern -i kh.google 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload
refresh_pattern -i keyhole.com 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload
原理就是用 no_cache allow 和 refresh_pattern 来设定一些缓存规则,将google earth的请求强行缓存起来。
此文一出,自然早有人去验证,可是没人成功,原作者也音讯全无 :( ... squid的邮件列表里也提到。 ( 看标题进来的朋友,不要急,继续往下读,不会让你空手而回的 :) )
我也没在意,估计人家功力问题 :P 。先试着用改写一下解决yupoo api的缓存问题。

acl QUERY urlpath_regex cgi-bin \?
acl forcecache url_regex -i yupoo\.com
no_cache allow forcecache
no_cache deny QUERY
refresh_pattern -i yupoo\.com 1440 50% 10080 override-expire override-lastmod reload-into-ims ignore-reload
嘿,果然nnd毫无用处,访问记录里还是 一坨坨 TCP_MISS
于是翻来覆去看文档,找资料,发现是squid的bug惹得祸,不过早已经修正(严格来说是功能扩展补丁)。
我的squid是2.6.13,翻了一下源代码,确实已经打好补丁了。
解决这个问题需要refresh_pattern的几个扩展参数(ignore-no-cache ignore-private),这几个参数在squid的文档和配置例子中均没有提到,看来squid还不够与时俱进。
下面讲一下问题所在。
先看看yupoo api返回的HTTP头部信息(cache 相关部分)

Cache-Control: no-cache, must-ridate
Pragma: no-cache
这两行是控制浏览器的缓存行为的,指示浏览器不得缓存。squid也是遵循RFC的,正常情况下自然不会去缓存这些页面。override-expire override-lastmod reload-into-ims ignore-reload 统统不能对付它。
而那个补丁正是对付这两个Cache-Control:no-cache 和 Pragma: no-cache的。
因此把 refresh_pattern那句要改写成
来源:(http://blog.sina.com.cn/s/blog_5dc960cd0100d5fh.html) - 转:squid缓存动态文件及cache-control参数_soulmate_新浪博客
refresh_pattern -i yupoo\.com 1440 50% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
这样就大功告成了, squid -k reconfigure 看看 access.log ,这回里面终于出现
TCP_HIT/200 TCP_MEM_HIT/200 了,说明缓存规则确实起作用了,那个激动啊 555~~~~
====================
补充:[/b]
后来我看了一下google earth 服务器 hk1.google.com的HTTP头部,只有

Expires: Wed, 02 Jul 2008 20:56:20 GMT
Last-Modified: Fri, 17 Dec 2004 04:58:08 GMT
,这么看来照理不需ignore-no-cache ignore-private也能工作,可能是作者这里写错了
kh.google 应该是 kh.\.google才对。
最后总结一下,缓存Google Earth/Map的正确的配置应该是[/b]

acl QUERY urlpath_regex cgi-bin \? intranet
acl forcecache url_regex -i kh.\.google mt.\.google mapgoogle\.mapabc keyhole.com
no_cache allow forcecache
no_cache deny QUERY
# ----
refresh_pattern -i kh.\.google 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
refresh_pattern -i mt.\.google 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
refresh_pattern -i mapgoogle\.mapabc 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
refresh_pattern -i keyhole.com 1440 20% 10080 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
注:
khX.google.com 是google earth的图片服务器
mtX.google.com 是google map 的图片服务器
mapgoogle.mapabc.com 是google ditu的图片服务器

原文地址:http://blog.chinaunix.net/u/8111/showart_449633.html

header中的Cache-control参数说明:

网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private、no-cache、max-age、must-ridate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:

(1) 打开新窗口
值为private、no-cache、must-ridate,那么打开新窗口访问时都会重新访问服务器。
而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:
Cache-control: max-age=5(表示当访问此网页后的5秒内再次访问不会去服务器)

Last-Modified: 告诉反向代理页面什么时间被修改
Expires: 告诉反向代理页面什么时间应该从缓冲区中删除
Cache-Control: 告诉反向代理页面是否应该被缓冲
Pragma: 用来包含实现特定的指令,最常用的是 Pragma:no-cache

(2) 在地址栏回车
值为private或must-ridate则只有第一次访问时会访问服务器,以后就不再访问。
值为no-cache,那么每次都会访问。
值为max-age,则在过期之前不会重复访问。

(3) 按后退按扭
值为private、must-ridate、max-age,则不会重访问,
值为no-cache,则每次都重复访问

(4) 按刷新按扭
  无论为何值,都会重复访问
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: