Varnish:如何根据特定cookie的值单独缓存页面 [英] Varnish: how to separately cache pages based on value of a specific cookie

查看:404
本文介绍了Varnish:如何根据特定cookie的值单独缓存页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我管理的网站拥有一个Cookie,我们必须使用,但始终是9个值之一(包括无值)。我想在我们的应用程序服务器前面使用清漆,清漆根据cookie值单独缓存每个页面的版本。

I manage a site that has a single cookie, which we have to use, but will always be one of 9 values (including no value). I'd like to use varnish in front of our application servers, with varnish separately caching a version of each page based on the cookie value.

因此,如果我们有page / page1,Varnish应该分别管理一个什么/ page1看起来像cookie值a,b,c,d等的副本...

So if we have page /page1, Varnish should separately manage a copy of what /page1 looks like with cookie values a, b, c, d, etc....

假设我们在Varnish服务器上有足够的内存来处理存储所有cookie组合的所有页面。

Assume we have plenty of memory on the Varnish server to handle storing all pages with all cookie combinations.

我们尝试了许多VCL设置,但无法确定如何使这项工作。 Varnish需要将特定的Cookie发送到我们的应用程序服务器,因此我们的应用程序知道要发回的内容。

We have tried many VCL settings but can't figure out exactly how to make this work. Varnish needs to send that specific cookie to our application server as well, so our application knows which content to send back.

提前感谢!

推荐答案

这很简单,事实上,你应该添加一个自定义 vcl_hash

It's quite simple to achive that in fact, you should add a custom 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);
  }
  #...
}

,varnish会存储一个不同的cookie,对于每个cookie值...但我会建议你也净化你的cookie在 vcl_recv ,这是一个摘录[1]包含对Drupal网站的cookie清理:

With this code, varnish will store a different cookie, for each cookie value... but I'll recommend you to also sanitize your cookies on vcl_recv, this is an excerpt of [1] containing cookie sanitization for Drupal sites:

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

这篇关于Varnish:如何根据特定cookie的值单独缓存页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆