【问题标题】:Varnish: ban URL with any GET parametersVarnish:禁止带有任何 GET 参数的 URL
【发布时间】:2020-05-14 20:25:39
【问题描述】:

我正在使用 Varnish 缓存 6.0.6。有时我需要基于忽略 GET 参数和任何标头的 URL 使缓存无效。我为此使用BAN 命令。

GET 命令来检索内容。他们收到的结果是不同的。

curl --user login:secret 'http://example.com/v1/account/123'
curl --user login2:secret2 'http://example.com/v1/account/123?origin=abc'

BAN 命令禁止两个缓存。

curl -X BAN example.com -H "X-Ban: /v1/account/123"

清漆配置

 if (req.method == "BAN") {
    if (!client.ip ~ purge) {
      return (synth(405, "Not allowed"));
    }
    ban("obj.http.x-host == " + req.http.host + " && obj.http.x-url ~ " + req.http.X-Ban);
    return (synth(200, "Ban added"));
  }

但是,ban 命令仅对第一个请求使缓存无效。第二个请求保持缓存。 如何使以^/v1/account/123 开头的所有内容无效?

【问题讨论】:

    标签: caching varnish varnish-vcl


    【解决方案1】:

    我已经在本地设置上对其进行了测试,它似乎无需任何更改即可工作。

    初始缓存请求:

    这是初始请求,你可以看到Age: 35 表示该项目已在缓存中存储了 35 秒。

    ➜  ~ curl -I localhost/v1/account/123\?origin=abc
    HTTP/1.1 200 OK
    Content-Type: text/plain
    Date: Fri, 15 May 2020 07:26:59 GMT
    Content-Length: 216
    x-host: localhost
    x-url: /v1/account/123?origin=abc
    X-Varnish: 32788 32786
    Age: 35
    Via: 1.1 varnish (Varnish/6.0)
    Accept-Ranges: bytes
    Connection: keep-alive
    

    禁止请求

    下一个请求发出禁令,使用您在 VCL 中描述的确切语法:

    ➜  ~ curl -X BAN localhost -H "X-Ban: /v1/account/123"
    <!DOCTYPE html>
    <html>
      <head>
        <title>200 Ban added</title>
      </head>
      <body>
        <h1>Error 200 Ban added</h1>
        <p>Ban added</p>
        <h3>Guru Meditation:</h3>
        <p>XID: 14</p>
        <hr>
        <p>Varnish cache server</p>
      </body>
    </html>
    

    结果

    当我再次执行初始 curl 调用时,Age 标头重置为零,这意味着页面不是从缓存中提供的。这是期望的结果。

    ➜  ~ curl -I localhost/v1/account/123\?origin=abc
    HTTP/1.1 200 OK
    Content-Type: text/plain
    Date: Fri, 15 May 2020 07:40:23 GMT
    Content-Length: 213
    x-host: localhost
    x-url: /v1/account/123?origin=abc
    X-Varnish: 16
    Age: 0
    Via: 1.1 varnish (Varnish/6.0)
    Accept-Ranges: bytes
    Connection: keep-alive
    

    VCL

    这是用于此示例的完整 VCL。请根据您的需要调整后端和 ACL 设置。

    请确保x-hostx-urlsub vcl_backend_response 中正确设置,否则您的禁令声明将无法匹配这些值。这就是我们所说的对潜伏者友好的禁令

    vcl 4.0;
    
    backend default {
        .host="localhost";
        .port="8080";
    }
    
    acl purge {
        "localhost";
    }
    
    sub vcl_recv {
        if (req.method == "BAN") {
            if (!client.ip ~ purge) {
                return (synth(405, "Not allowed"));
            }
            ban("obj.http.x-host == " + req.http.host + " && obj.http.x-url ~ " + req.http.X-Ban);
            return (synth(200, "Ban added"));
        }
    }
    
    sub vcl_backend_response {
        set beresp.http.x-host = bereq.http.host;
        set beresp.http.x-url = bereq.url;
    }
    

    【讨论】:

    • 您好 Thijs,非常感谢您的努力。我刚刚检查了版本。已经是6.0.6了。我将尝试从本地清洁清漆开始。也许还有一些其他配置会覆盖 URL。
    • @AlexeySchepin 我删除了版本部分并添加了我用于示例的完整 VCL。看看关于x-host & x-url 的评论。
    • 我找到了失败的原因。我的清漆节点在 HAProxy 后面。第一个请求缓存在一个节点上,第二个请求缓存在另一个节点上。当我发送ban 命令时,只有一个 Varnish 节点(没有参数)接收它。
    猜你喜欢
    • 2018-05-25
    • 2020-12-07
    • 1970-01-01
    • 2023-04-01
    • 2023-04-07
    • 2017-02-22
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    相关资源
    最近更新 更多