如何在Varnish中不缓存500内部服务器错误

13

我真的想不出来如何让Varnish忽略对500内部服务器错误的缓存。基本上,如果有人访问Varnish并返回500内部服务器错误,则我希望Varnish不缓存该页面(设置0秒的ttl / grace期间?)。 我正在使用Varnish 3.0.3,并附上我的VCL。默认情况下,我希望将页面缓存30天。

sub vcl_fetch {
    # Set 30-day TTL
    set beresp.ttl = 2592000 s;
    set beresp.grace = 15d; /* The max amount of time to keep object in cache */

    if (beresp.status == 301 || beresp.status == 302) {
            return (hit_for_pass);
    }

    # Serve pages from the cache should we get a sudden error and re-check in one minute
    if (beresp.status >= 500) {
      set beresp.grace = 1s;
      set beresp.ttl = 1s;
      return (hit_for_pass);
    }

    # Unset the "etag" header (suggested)
    unset beresp.http.etag;

    return(deliver);
}

所以,简单来说:如果返回500内部服务器错误代码,那么X-CACHE应该显示MISS。当我刷新页面时,如果还是500内部服务器错误,那么它应该再次显示MISS。如果页面成功加载,则应该显示HIT。

1个回答

17

1
嗯...我不明白为什么Varnish会缓存404错误。我们曾经因此遭受过损失——资源已经恢复,但用户却看不到它。 - Leonid
4
一般来说,由于404错误并不表示上游服务器出现故障,而是表示请求已被接收和处理,但请求的资源不存在,因此它通常会被反向代理缓存。 - mickeybob
@NITEMAN 这并不是至少在Varnish 4上的真实情况。请参见:https://github.com/mattiasgeniar/varnish-4.0-configuration-templates/issues/24 - Mohsen
@Mohsen 注意,我指的是Varnish的默认行为(可覆盖),将其命名为[builtin.vcl](https://github.com/varnishcache/varnish-cache/blob/master/bin/varnishd/builtin.vcl),今天仍然如此:https://github.com/varnishcache/varnish-cache/blob/master/doc/sphinx/users-guide/vcl-built-in-subs.rst#berespttl--berespgrace--berespkeep - NITEMAN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接