squid 否定缓存 negative_ttl 实测和总结

作者:sealinger 发布时间:December 3, 2011 分类:都学杂了

squid-2.7.STABLE6

negative_ttl 5 minutes 总结

#  TAG: negative_ttl    time-units
#       Time-to-Live (TTL) for failed requests.  Certain types of
#       failures (such as "connection refused" and "404 Not Found") are
#       negatively-cached for a configurable amount of time.  The
#       default is 5 minutes.  Note that this is different from
#       negative caching of DNS lookups.
#
#Default:
# negative_ttl 5 minutes

1)这是针对错误status代码的“否定缓存”机制,如403、404;
2)当不想缓存此类页面,可以设置为 negative_ttl 0 seconds
3)但是需要注意,当错误代码页面声明了可缓存标记(Expries:、Cache-Control:)时,不管 negative_ttl 设置如何,这些页面仍然会被缓存,而且缓存的时间是按缓存标记声明的时间来的,这是符合缓存协议的正常行为;
4)所以,总结来说,negative_ttl 5 minutes 仅仅是针对不带缓存声明的错误页面的 缓存机制。

我已经实际测试了:

用php模拟一个404的错误页面,带上 Expires:和 Cache-Control:缓存标记,另外同时随便访问一个不存在的页面(webserver返回404):

1)404.php ,即使squid配置了 negative_ttl 5 minutes ,对该页面的缓存时间 也是按 404.php 标记的 max-age 时间来缓存;(这是符合缓存协议的正常行为)
2)xxx.html(不存在)不带缓存标记,按 negative_ttl 5 minutes 设置的时间来缓存;
3)404.php ,即使squid配置了 negative_ttl 0 seconds ,对该页面的缓存时间 也是按 404.php 标记的 max-age 时间来缓存;(这是符合缓存协议的正常行为)
4)xxx.html(不存在)不带缓存标记,按 negative_ttl 0 seconds 设置,即不缓存。

想完全屏蔽对错误页面的缓存,需要使用squid-3.x,通过配置对HTTP STATUS缓存规则来实现:

squid 3.x 才支持如下配置:

acl badurl http_status 403-404 500-
http_access deny badurl