Varnish - PURGE请求不会清除hash_data()URL

时间:2014-08-24 09:50:54

标签: varnish vcl varnish-vcl

在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.";
        }
}

我使用下面的命令来清除网址。 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>

如果仅为登录用户缓存,我会继续低于输出。 (即使网址正在制作&#34; Hits&#34;)

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中进行哈希处理的网址。 似乎是Varnish的错误或功能。请建议,可以做些什么。

2 个答案:

答案 0 :(得分:0)

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

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

如果你想清除散列的所有变体,你需要调用它两次,一次使用cookie,一次不使用。

答案 1 :(得分:0)

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

我们在不更改对象哈希的情况下这样做。我们使用相同对象的不同变体(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";

  ...
}
相关问题