如何使Varnish忽略,而不是删除cookie [英] How to make Varnish ignore, not delete cookies
问题描述
即使存在Cookie,我也希望使用Varnish来缓存某些页面。我需要处理3种可能性:
I want to use Varnish to cache certain pages even in the presence of cookies. There are 3 possibilities that I need to take care of:
- 匿名用户正在查看某个页面
- 登录用户正在查看具有轻量级自定义的页面。这些自定义项都存储在已签名的cookie中,并由Javascript动态填充。不设置vary-cookie http标头。
- 登录用户正在查看包含来自数据库的自定义数据的某个页面。设置了vary-cookie http标头。
预期的行为是:
- 缓存页面。这是Varnish要处理的最基本方案。
- 缓存页面而不删除cookie,因为有些Javascript逻辑需要它。
- 从不缓存此页面因为vary-cookie表示cookie内容会影响此页面的输出。
我已阅读一些关于清漆的文档我无法判断这是否是默认行为,或者我是否需要在VCL中进行一些设置才能实现。
I have read some docs on Varnish and I cannot tell if this is the default behavior or if there is some setup I have to do in VCL to make it happen.
推荐答案
只有会话对每个客户都是唯一的,不一定是cookie。
Only sessions are unique to every client, not necessarily cookies.
你想要的东西是有意义的,并且可以用Varnish,这只是一个精心制作自己的问题VCL。请注意default.vcl的以下部分:
What you want makes sense and is possible with Varnish, it is just a matter of carefully crafting your own vcl. Please pay attention to the following parts of the default.vcl:
sub vcl_recv {
...
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
}
sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
...
}
sub vcl_fetch {
if (!beresp.cacheable) {
return (pass);
}
if (beresp.http.Set-Cookie) {
return (pass);
}
...
}
你必须更换这些部分有你自己的逻辑;即定义您自己的vcl_函数。默认情况下,带cookie的请求(vcl_recv)和响应(vcl_fetch)不可缓存。您最了解后端应用程序,并且应该将通用缓存逻辑重写为此特定情况。也就是说,你应该定义清漆在哪种情况下进行查找,传递或传递。
You have to replace these parts with your own logic; i.e. define your own vcl_ functions. By default, requests (vcl_recv) and responses (vcl_fetch) with cookies are not cacheable. You know your back-end application best and you should rewrite the generic caching logic to this specific case. That is, you should define in which case varnish does a lookup, pass or deliver.
在你的情况下,你将有页面(案例1和2)没有变化-by cookie,将被所有人缓存和共享(带/不带cookie的请求);只是不介意vcl_recv中的req.http.Cookie。我不会用变化的cookie来缓存页面(案例3) - 或者至少不会长时间 - 因为它们根本无法共享;在vcl_fetch中执行'pass'。
In your case, you will have pages (case 1 and 2) without a vary-by cookie, which will be cached and shared by everyone (requests with/without cookies); just don't mind req.http.Cookie in vcl_recv. I wouldn't cache pages (case 3) with a vary-by cookie -or at least not for a long time-, as they can not be shared at all; do a 'pass' in vcl_fetch.
这篇关于如何使Varnish忽略,而不是删除cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!