简体   繁体   English

Varnish-devicedetect基于cookie的用户代理覆盖是否需要不同:cookie才能正确浏览器缓存?

[英]Does Varnish-devicedetect cookie based User-agent override require vary: cookie for correct browser caching?

varnish-devicedetect lets me return different responses based on User-agent: varnish-devicedetect让我根据用户代理返回不同的响应:

...
elsif (req.http.User-Agent ~ "(?i)ipad")        { set req.http.X-UA-Device = "tablet-ipad"; }
elsif (req.http.User-Agent ~ "(?i)ip(hone|od)") { set req.http.X-UA-Device = "mobile-iphone"; }
...

and lets users opt into a different device experience by setting a cookie: 并允许用户通过设置Cookie选择不同的设备体验:

if (req.http.Cookie ~ "(?i)X-UA-Device-force") {
  ...
}

Do I need to add Vary: Cookie to get correct client caching behavior? 我是否需要添加Vary: Cookie才能获得正确的客户端缓存行为?

For example: 例如:

  1. I set only Vary: User-agent 我只设置了Vary: User-agent
  2. A user browses to example.com/beep 用户浏览到example.com/beep
  3. They opt into the desktop experience, setting X-UA-Device-force: pc . 他们选择设置X-UA-Device-force: pc进入桌面体验。
  4. They reload example.com/beep. 他们重新加载example.com/beep。
    Will their browser use the cached mobile version, since only Cookies, not User-agent changed? 他们的浏览器是否会使用缓存的移动版本,因为仅更改了Cookie,而不更改了用户代理?

Yes, you need to set a Vary: Cookie header to get the correct behaviour since different cookies would retrive different output. 是的,您需要设置Vary: Cookie标头以获取正确的行为,因为不同的Cookie会检索不同的输出。

You'll need also to take care of the cookie on vcl_recv setting the appropriate X-UA-Device in order to hash items correctly in varnish. 您还需要注意在vcl_recv设置适当的X-UA-Device的cookie,以便正确地对清漆中的项目进行哈希处理。

If you vary cookie, then you might as well through caching out the window since most users will have different cookies (especially so if you have any analytics on your site) so you will have a low hit rate and multiple copies of the same data in your cache. 如果您使用不同的Cookie,那么您最好还是缓存整个窗口,因为大多数用户将使用不同的Cookie(尤其是如果您的网站上有任何分析功能的话),因此您的点击率较低,并且相同数据的多个副本您的缓存。

Rather than vary cookie, do a hash on the specific cookie value like so: 而不是更改cookie,而是对特定cookie值进行哈希处理,如下所示:

if (req.http.cookie ~ "(?i)X-UA-Device-force" ) {
  hash_data("deviceforce");
} else {
  hash_data("nodeviceforce");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM