我试图在同一台机器上的 php 应用程序前面使用 squid 作为反向缓存代理,并且永远不要让用户到达后端,总是在后台加载陈旧的缓存。
我在端口 80 中使用带有 Centos 6.3 (x86_64) 的自编译 squid 2.7-Stable9,而 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缓存选项。
不同之处在于,当缓存未禁用时,客户端发送Cache-control: 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