【问题标题】:Varnish ban stays in banlist forever清漆禁令永远留在禁令名单中
【发布时间】:2014-06-02 00:35:50
【问题描述】:

我有一个在开发服务器上运行的 varnishsetup (Varnish 3),在生产服务器上运行相同的设置。 除非我对其进行测试,否则开发服务器不会获得任何流量。 生产服务器通过该 varnishinstance 获得了大约 80 个网站的大量流量。网站都是一样的,一个VCL就可以了。

目前我正在使用 URL smart-bans 和 ban_lurker 进行测试。

我期望的是,当我添加一个禁令时,即使该禁令由于某种原因不起作用,它也应该只在我的缓存中最大的 TTL + backend-response-grace-period 保留在禁令列表中。

问题

问题是,在我的生产服务器上,禁令会永远保留在禁令列表中(或者至少 24 小时,因为我已经给它消失了)。

所以我调整了我的 VCL,以便我希望任何缓存对象在缓存中停留的时间都不可能超过 20 分钟,因为我从我的 vcl_fetch 强制 20 分钟并将 beresp.grace 强制为 0s .

在我的开发 varnishserver 上,这似乎完全一样。当我点击网站并以这种方式缓存一些东西时,然后将禁令放入,它在 20 分钟内就如预期的那样消失了。 在我的生产服务器上,这似乎对禁令列表没有任何影响。它只是停留在那里。

我错过了什么吗?

VCL

有人可以看看我的 VCL 并告诉我我做错了什么吗? 请注意,我遗漏了一些东西,例如后端配置和清除/禁止配置,因为我认为现在这无关紧要。如果我错了,请纠正我:)

import std;

# Define backends
include "backends.vcl";

# Define recv basics
sub vcl_recv {
    if (req.request != "GET" &&
      req.request != "HEAD" &&
      req.request != "PUT" &&
      req.request != "POST" &&
      req.request != "TRACE" &&
      req.request != "OPTIONS" &&
      req.request != "DELETE" &&
      req.request != "PURGE" &&
      req.request != "BAN") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }
}

# Define purging and banning rules
include "purging-banning.vcl";

# Appart from PURGE and BAN requests, we only handle GET and HEAD requests
sub vcl_recv {
    if (req.request != "GET" && req.request != "HEAD") {
        /* We only deal with GET and HEAD by default */
        return (pass);
    }

    # Never cache monitor.php
    if (req.url ~ "^/monitor.php") {
         return (pass);
    }

    # in case of down backends serve content up to 30 minutes old
    if (req.backend.healthy) {
        set req.grace = 2m;
    } else {
        set req.grace = 30m;
    }
}

# Define purging and banning rules
include "recv-url-rules.vcl";

# If the URL's hasn't been caught by recv-url-rules.vcl, continue with common rules
sub vcl_recv {   
    # Don't cache AJAX requests
    if (req.http.X-Requested-With == "XMLHttpRequest") {
        return(pass);
    }
}

sub vcl_fetch {
    # Set default TTL of 20 minutes
    set beresp.ttl = 20m;

    # Keep objects in cache beyond their lifetime
    set beresp.grace = 0s; # for testing purposes

    # Don't cache if requested so by the server
    if (beresp.http.X-NoCache  == "no-cache" || beresp.http.cache-control ~ "private") {
        set beresp.ttl = 0s;
        set beresp.http.Cache-Control = "max-age = 0";
        return ( hit_for_pass );
    }

    # Pass (don't cache) big files (limit is just under 1MB)
    if ( beresp.http.Content-Length ~ "[0-9]{6,}" ) {
        set beresp.ttl = 0s;
        return ( hit_for_pass );
    }

    # Remember URL and HOST for the ban-lurker
    set beresp.http.x-url = req.url;
    set beresp.http.x-host = req.http.host;
}

sub vcl_deliver{
    # These were set for the ban-lurker, but don't have to be send to the client
    unset resp.http.x-url;
    unset resp.http.x-host;

    # Add a header to the request so that we can see wether or not the object came from cache
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

sub vcl_pipe {
    # http://www.varnish-cache.org/ticket/451
    # This forces every pipe request to be the first one.
    set bereq.http.connection = "close";
}

清漆文档

Varnish 文档声明如下:

在 Varnish 运行 vcl_fetch 之前,beresp.ttl 变量已经被设置为一个值。 >它将使用它找到的第一个值:

Cache-Control 响应标头中的 s-maxage 变量 Cache-Control 响应标头中的 max-age 变量 过期响应标头 default_ttl 参数。

那么在某些情况下,后端响应是否仍会覆盖 TTL?即使我在 vcl_fetch 中强制它?

【问题讨论】:

  • 有人有线索吗?

标签: caching varnish ttl


【解决方案1】:

禁令将保留在禁令列表中,直到缓存中比它更早的所有对象都被检查过。如果您提供具有长 TTL 的长尾内容,则禁止列表会变得非常长。

但是,由于每个对象的禁令只评估一次,因此实际上没有任何缺点(除了美学)。不用担心。

【讨论】:

  • 关于你的第二个问题;请参阅 TTL varnishlog 行。引用“RFC”的行是从响应标头中提取的 TTL,提及“VCL”的行是您的本地覆盖。本地覆盖优先。
  • 感谢您的回答(对不起,我还不能投票:()。我不确定您是否回答了我的问题。由于我将所有 TTL 设置为 20 分钟,因此禁令不可能持续超过 20 分钟,对吧?无论如何:我上周遇到了这个问题。出于某种奇怪的原因,现在突然它似乎完全按预期工作了(禁令很快就消失了)。现在我我会稍微调整一下我的 TTL,可能会改用更长的禁令列表——这就是你的“不用担心”的地方 :)
猜你喜欢
  • 2021-03-31
  • 1970-01-01
  • 2017-05-19
  • 2022-11-28
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2013-08-02
  • 1970-01-01
相关资源
最近更新 更多