【问题标题】:Varnish: ignore some cache cookies for hash computationVarnish:忽略一些用于哈希计算的缓存 cookie
【发布时间】:2017-04-09 18:58:55
【问题描述】:

情况::

  1. 即使请求中存在 cookie,Varnish 也需要缓存。
  2. 请求可能包含 N 个任意 cookie,其中某些已知 cookie 不得构成缓存键的一部分。任意 cookie 不包含任何用户敏感数据,例如。他们就像 is_authenticated=1 一样是化妆品的帮手。
  3. 实际的后端必须接收原始的 cookie 集,以防缓存未命中。
  4. 我不想检查 VCL 中的 URL 模式,因为这假定了对后端的了解太多。

这很难解决。到目前为止,我发现的所有解决方案都假定 (2) 有一个白名单,而我需要一个黑名单。而且大多数解决方案都会删除应该通过后端的 cookie。

【问题讨论】:

  • 只是为了确认一下,您想对某些 UNKNOWN cookie 集(不包括那些已知的 cookie)进行哈希处理?
  • 是的,通常的散列组件(主机、url)必须保留为散列的一部分。

标签: varnish


【解决方案1】:

那么(未经测试)怎么样:

# We use builtin.vcl logic and 'return' from our vcl_recv in order 
# to prevent default Varnish behaviour of not caching with cookies present
sub vcl_recv {
    # your vcl_recv starts here
    # ...
    # your vcl_recv ends here

    if (req.method == "PRI") {
    /* We do not support SPDY or HTTP/2.0 */
        return (synth(405));
    }
    if (req.method != "GET" &&
      req.method != "HEAD" &&
      req.method != "PUT" &&
      req.method != "POST" &&
      req.method != "TRACE" &&
      req.method != "OPTIONS" &&
      req.method != "DELETE") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }

    if (req.method != "GET" && req.method != "HEAD") {
        /* We only deal with GET and HEAD by default */
        return (pass);
    }
    if (req.http.Authorization) {
        /* Not cacheable by default */
        return (pass);
    }
    return (hash);
}

sub vcl_hash {
    set req.http.X-Cookie-Hash = regsub(req.http.cookie, "KNOWN1=[^;]+;", "");
    set req.http.X-Cookie-Hash = regsub(req.http.X-Cookie-Hash, "KNOWN2=[^;]+;", "");
    hash_data(req.http.X-Cookie-Hash);
}

这会删除每个已知的 cookie 和剩余部分的哈希 :) 不理想,因为它不能保证 cookie 在标头中的顺序,但其他应该可以工作。

【讨论】:

  • 这看起来确实是正确的,但仍然存在一个问题 - Varnish 仍然看到 cookie 的存在,因此拒绝缓存。有没有办法改变这种行为?
  • 哇,这令人印象深刻。到目前为止,我的测试表明它运行良好。我会将您的答案标记为解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2023-03-08
  • 2014-05-21
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
相关资源
最近更新 更多