【问题标题】:How do I cache an ESI fragment and the corresponding output with Varnish 4.x by IP如何通过 IP 使用 Varnish 4.x 缓存 ESI 片段和相应的输出
【发布时间】:2014-12-23 03:51:41
【问题描述】:

我正在尝试使用 Varnish 4.x 中的 ESI 从我的 (PHP) 后端获取数据。我想将结果保存(缓存)在我的 Varnish 实例中。结果对于每个 IP 地址 (client.ip) 都是唯一的,因此缓存结果的哈希必须包含 client.ip。 Varnish 没有按预期缓存。我似乎无法基于 IP 进行缓存。为了让您了解我的尝试,请参见下文。

unique.js:

var rand = '<esi:include src="/unique.php"/>';

独特的.php:

<?php echo rand(); ?>

default.vcl(提取):

sub vcl_recv {
     if (req.url ~ "^/unique.(js|php)") {
         set req.http.marker = client.ip;
     }
}

sub vcl_hash {
     if (req.http.marker) {
         hash_data(req.http.marker);
     }
}

sub vcl_backend_response {
    if (bereq.url == "/unique.js") {
       set beresp.do_esi = true;
       set beresp.ttl = 24h;
    } elseif (bereq.url == "/unique.php") {
       set beresp.ttl = 30d;
    }
}

显然我错过了一些东西。任何提示表示赞赏!

【问题讨论】:

    标签: caching varnish varnish-vcl esi


    【解决方案1】:

    我认为帖子中没有足够的信息来给你一个明确的答案,但我的猜测是缓存不能正常工作的原因与你的 ESI 代码无关。

    正如多处提到的 (here is one example),Varnish 默认不缓存 cookie。所以很可能是在 JS 或 PHP 会话代码中设置的 cookie 导致您的请求没有被缓存。我的猜测是您需要添加:

    unset req.http.cookie;
    

    在您的 if (req.url ~ "^/unique.(js|php)") { 块内。

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 2014-05-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2011-08-23
      • 2017-07-22
      相关资源
      最近更新 更多