Squid 2.7和客户端头部max-age = 0

时间:2012-10-11 08:39:13

标签: reverse-proxy squid

我正在尝试在同一台机器上的php应用程序前面使用squid作为反向缓存代理,并且永远不会让用户到达后端,总是在后台加载过时的缓存。


我在端口80中使用自编译的squid 2.7-Stable9和Centos 6.3(x86_64),而apache + php与端口8000在同一台机器上。

apache + php响应头将此发送给squid:

header('Cache-Control: max-age=10, stale-while-revalidate=15, stale-if-error=20');
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

当我要求提供squid页面时,第一次重新验证并且下次使用时总是陈旧,但这适用于Firefox WebDevelopersTools扩展的 DISABLE CACHE 缓存选项。

不同之处在于,当未禁用缓存时,客户端发送缓存控制:max-age = 0 If-Modified-Since 并且squid连接到后端总是和重新验证。

在Squid 3.2中,它使用 ignore-cc 按预期工作,但在2.7中没有这样的选项。

这是我的squid.conf:

# http_port public_ip:port accel defaultsite= default hostname, if not provided
http_port 80 accel defaultsite=mydomain.com

# IP and port of your main application server (or multiple)
cache_peer 127.0.0.1 parent 8000 0 no-query originserver name=main
cache_peer_domain main mydomain.com

# Do not tell the world that which squid version we're running
httpd_suppress_version_string on

# Remove the Caching Control header for upstream servers
header_access Cache-Control deny all

# log all incoming traffic in Apache format
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /usr/local/squid/var/logs/squid.log combined all

cache_effective_user squid

refresh_pattern nocache=true 0 0% 0

icp_port 0

我想在php应用程序中选择缓存时间,并且永远不要让用户看到重新验证的延迟,这就是为什么我使用的是版本为2.7的stale-while-revalidate。


启用缓存的请求示例:

GET /some-page HTTP/1.1
Host: mydomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1 FirePHP/0.7.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
x-insight: activate
If-Modified-Since: Thu, 11 Oct 2012 10:14:52 GMT
Cache-Control: max-age=0

HTTP/1.0 200 OK
Date: Thu, 11 Oct 2012 10:29:31 GMT
Server: Apache/2.2.22 (PowerStack)
Last-Modified: Thu, 11 Oct 2012 10:29:32 GMT
Content-Type: text/html; charset=UTF-8
X-Cache: MISS from localhost
X-Cache-Lookup: HIT from localhost:80
Via: 1.1 localhost:80 (squid)
Connection: close

禁用缓存的示例(我想要发生的事情):

GET /some-page HTTP/1.1
Host: mydomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1 FirePHP/0.7.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
x-insight: activate

HTTP/1.0 200 OK
Date: Thu, 11 Oct 2012 10:36:53 GMT
Server: Apache/2.2.22 (PowerStack)
Last-Modified: Thu, 11 Oct 2012 10:36:53 GMT
Content-Type: text/html; charset=UTF-8
Age: 2
Content-Length: 16725
X-Cache: HIT from localhost
X-Cache-Lookup: HIT from localhost:80
Via: 1.1 localhost:80 (squid)
Connection: keep-alive

1 个答案:

答案 0 :(得分:0)

此处描述的解决方案:How to use in squid 2.7 the 3.1 feature "ignore-cc"


好吧,在尝试了所有替代方案后,我修改了源代码以实现该功能。

我更改了 Squid-2.7-stable9 文件 src / refresh.c 282

282:    if (request) {

282:    if (request && FALSE) {

他们在3.X中执行类似的操作来实现ignoreCc:

269:    if (request && !request->flags.ignore_cc) {

按预期工作。