php – Varnish:如何根据特定cookie的值单独缓存页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Varnish:如何根据特定cookie的值单独缓存页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2451字,纯文字阅读大概需要4分钟。
内容图文
我管理的网站只有一个cookie,我们必须使用它,但总是9个值之一(包括没有值).我想在我们的应用程序服务器前面使用清漆,varnish根据cookie值分别缓存每个页面的版本.
因此,如果我们有page / page1,Varnish应该单独管理/ page1与cookie值a,b,c,d等相似的副本….
假设我们在Varnish服务器上有足够的内存来处理存储所有cookie组合的所有页面.
我们已经尝试了很多VCL设置,但无法弄清楚如何使其工作. Varnish也需要将特定的cookie发送到我们的应用服务器,因此我们的应用程序知道要发回哪些内容.
提前致谢!
解决方法:
实现起来很简单,实际上,你应该添加一个自定义的vcl_hash:
sub vcl_hash {
#...
/* Hash cookie data */
# As requests with same URL and host can produce diferent results when issued with different cookies,
# we need to store items hashed with the associated cookies. Note that cookies are already sanitized when we reach this point.
if (req.http.Cookie) {
/* Include cookie in cache hash */
hash_data(req.http.Cookie);
}
#...
}
使用此代码,清漆将为每个cookie值存储不同的cookie …但我建议您还要在vcl_recv上清理您的cookie,这是[1]的摘录,其中包含针对Drupal站点的cookie清理:
sub vcl_recv {
#...
# Remove all cookies that backend doesn't need to know about.
# See https://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies
if (req.http.Cookie) {
/* Warning: Not a pretty solution */
/* Prefix header containing cookies with ';' */
set req.http.Cookie = ";" + req.http.Cookie;
/* Remove any spaces after ';' in header containing cookies */
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
/* Prefix cookies we want to preserve with one space */
/* 'S{1,2}ESS[a-z0-9]+' is the regular expression matching a Drupal session cookie ({1,2} added for HTTPS support) */
/* 'NO_CACHE' is usually set after a POST request to make sure issuing user see the results of his post */
/* Keep in mind we should add here any cookie that should reach the backend such as splahs avoiding cookies */
set req.http.Cookie = regsuball(req.http.Cookie, ";(S{1,2}ESS[a-z0-9]+|NO_CACHE|OATMEAL|CHOCOLATECHIP)=", "; \1=");
/* Remove from the header any single Cookie not prefixed with a space until next ';' separator */
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
/* Remove any '; ' at the start or the end of the header */
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
if (req.http.Cookie == "") {
/* If there are no remaining cookies, remove the cookie header. */
unset req.http.Cookie;
}
}
#...
}
[1] https://github.com/NITEMAN/varnish-bites/blob/master/varnish3/drupal-base.vcl
内容总结
以上是互联网集市为您收集整理的php – Varnish:如何根据特定cookie的值单独缓存页面全部内容,希望文章能够帮你解决php – Varnish:如何根据特定cookie的值单独缓存页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。