【问题标题】:Varnish - PURGE request does not purge hash_data() URLVarnish - PURGE 请求不会清除 hash_data() URL
【发布时间】:2014-10-17 16:34:58
【问题描述】:

在 vcl_hash 中,我有

backend default {
        .host = "127.0.0.1";
        .port = "8080";
}
acl purge {
        "localhost";
}
sub vcl_hash {
        if(req.http.Cookie ~ "isLogin") {
                hash_data("1");
        }
}
sub vcl_recv {
        if (req.request == "PURGE") {
                if (!client.ip ~ purge) {
                        error 405 "Not allowed.";
                }
                return (lookup);
        }
        return(lookup);
}
sub vcl_hit {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}
sub vcl_miss {
        if (req.request == "PURGE") {
                purge;
                error 404 "Not in Cache.";
        }
}

我正在使用以下命令来清除 url。 curl -X PURGE http://release.com/user/details

如果为已注销的用户缓存 url,我会得到以下输出

curl -X PURGE http://release.com/user/details
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>200 Purged.</title>
  </head>
  <body>
    <h1>Error 200 Purged.</h1>
    <p>Purged.</p>
    <h3>Guru Meditation:</h3>
    <p>XID: 1071483546</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>

如果它只为登录用户缓存,我会一直低于输出。 (即使 url 正在“命中”)

curl -X PURGE http://release.com/user/details
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>404 Not in Cache.</title>
  </head>
  <body>
    <h1>Error 404 Not in Cache.</h1>
    <p>Not in Cache.</p>
    <h3>Guru Meditation:</h3>
    <p>XID: 998719206</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>

http://release.com/user/details 在用户登录与否的基础上看起来不同。 (即它是否有 isLogin cookie)。 PURGE 不适用于在 vcl_hash 中散列的 url。 似乎是 Varnish 的错误或功能。请建议,可以做什么。

【问题讨论】:

  • 您确认登录的用户甚至可以获得缓存页面吗?通常,使用 Set-Cookie 返回的响应不会被缓存(除非您覆盖它)
  • 你发现了吗?我在使用 varnish 4.1 时遇到了同样的问题
  • @DavidStone 也许我的回答可以帮助你

标签: varnish vcl varnish-vcl


【解决方案1】:

我发现我的问题可能对你和任何使用 vcl_hash 和 purge 的人来说都是一样的。哈希用于清除,因此用于哈希的所有请求信息都将用于执行清除。在您的情况下,您检查了 cookie if(req.http.Cookie ~ "isLogin"),因此您必须发送带有清除请求的此 cookie,例如:

curl -X PURGE --cookie "isLogin=1" http://release.com/user/details

如果您想清除哈希的所有变体,您需要调用它两次,一次使用 cookie,一次不使用。

【讨论】:

    【解决方案2】:

    我们也有类似的情况。我们希望为移动和桌面用户提供不同的内容(取决于用户代理)。

    我们在不改变对象哈希的情况下这样做。我们正在使用同一对象的不同变体(Vary)。阅读cache invalidation documentation

    我们正在使用在beresp.http.Vary 中添加的自定义标头(我们也在后端使用此自定义标头)。

    在我们的场景中,必须设置发回给用户(包括 Google)的 Vary 标头,以便他们知道响应取决于 User-Agent。我们在vcl_deliver 做。

    sub vcl_recv {
      ...
    
      if (req.restarts == 0) {
        # Change this code, we are using a CloudFront header
        if (req.http.CloudFront-Is-Mobile-Viewer == "true") {
          set req.http.X-Device-Type = "mobile";
        } else {
          set req.http.X-Device-Type = "desktop";
        }
      }
    
      ...
    
      if (req.method == "PURGE") {
        if (!client.ip ~ acl_purge) {
          return(synth(400, "Bad request."));
        }
        return(purge);
      }
    
      ...
    }
    
    sub vcl_backend_response {
        ...
    
        if (beresp.http.Vary) {
          set beresp.http.Vary = beresp.http.Vary + ", " + "X-Device-Type";
        } else {
          set beresp.http.Vary = "X-Device-Type";
        }
    
        ...
    }
    
    sub vcl_deliver {
      ...
    
      set resp.http.Vary = "Accept-Encoding, User-Agent";
    
      ...
    }
    

    【讨论】:

    猜你喜欢
    • 2015-02-03
    • 2012-09-22
    • 1970-01-01
    • 2015-10-04
    • 2017-04-25
    • 2016-02-03
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多