我使用 Varnish 3,网站在 wordpress 上。问题是当我删除所有 cookie 和 set-cookie 标头时,清漆缓存很好。但是我需要根据cookie制作清漆来缓存页面。(我的网站内容取决于地理位置,所以当用户第一次登录时,我添加了 cookie 'detected_country')首先我删除了所有 set-cookies 标题:
# Drop any cookies Wordpress tries to send back to the client.
sub vcl_fetch {
if (!(req.url ~ "wp-(login|admin)")) {
unset beresp.http.set-cookie;
}
}
然后:默认情况下,varnish 不会缓存具有 cookie 的请求。默认情况下,这是由 vcl_fetch 逻辑完成的。所以我更改了默认的 vcl_fetch 并注释掉了 cookie 禁令(请参阅 参考资料/*|| req.http.Cookie*/
):
sub vcl_recv {
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization /*|| req.http.Cookie*/) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
之后,我希望清漆会缓存带有 cookie 的页面,但事实并非如此 :-( 但它应该缓存!不是吗?
如果我删除所有 cookie,它会缓存好:
sub vcl_recv {
if (!(req.url ~ "wp-(login|admin)")) {
remove req.http.Cookie;
}
}
请帮忙!我花了很多时间找出它为什么不缓存