Varnish 2.x 的“禁止”替代方案

问题描述

我们在 Varnish 4.x 中使用以下代码

 if (req.http.X-Pool) {
    ban("obj.http.X-Pool ~ " + req.http.X-Pool);
 }

现在我们正在转向使用Varnish 2.x的Fastly,所以我们没有得到什么可以替代Varnish 2.x

解决方法

要获得 Fastly Varnish/VCL 的帮助,我建议通读这两本书:

我通常还建议联系 support@fastly.com(他们是一群很好的人)。

关于您的问题,我不熟悉标准 Varnish 中的 bans,但阅读 https://varnish-cache.org/docs/trunk/users-guide/purging.html#bans 表明禁止是一种防止缓存内容的方法从被送达。

因此,解决方案取决于您在发生这种情况时要实现的目标。

如果您只想避免缓存,您可以从 passvcl_recv 等各种子例程返回 vcl_hit(尽管来自 vcl_hit 会导致 hit-for-pass 等这将导致请求直接发送到后端。

您还可以向 vcl_recv 甚至 vcl_hit 添加自定义逻辑(如果您想确保请求的内容实际上已缓存),然后您可以从那里触发 error这会将您发送到 vcl_error,您可以在其中构建综合响应:

sub vcl_hit {
  #FASTLY hit

  if (<some_condition>) {
    error 700
  }
}
sub vcl_error {
  #FASTLY error

  if (obj.status == 700) {
    set obj.status = 404;

    synthetic {"
      <!doctype html>
      <html>
        <head>
          <meta charset="utf-8">
          <title>Error</title>
        </head>
        <body>
          <h1>404 Not Found (varnish)</h1>
        </body>
      </html>
      "};

    return(deliver);
  }

或者从 vcl_recvvcl_hit 您可能想要 restart 然后检查重启并做一些不同的事情(以某种方式更改后端或请求):

sub vcl_recv {
  #FASTLY recv

  if (req.restarts > 0) {
    if (<some_condition>) {
      // do something
    }
  }
}