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

Her*_*man 12 vcl varnish

我无法弄清楚,对于我的生活,如何让varnish忽略500内部服务器错误的缓存.基本上,如果某人遇到清漆并返回500内部服务器错误,我希望varnish不缓存该页面(设置0s ttl /宽限期?).我正在使用清漆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);
}
Run Code Online (Sandbox Code Playgroud)

所以,用英语:如果返回500内部服务器...... X-CACHE应该显示MISS.当我刷新页面时,如果它仍然是500内部服务器,那么它应该再次显示一个MISS.如果页面成功传递,则应显示HIT.

NIT*_*MAN 16

默认情况下,Varnish只会缓存以下状态代码[1]:

  • 200:好的
  • 203:非权威信息
  • 300:多种选择
  • 301:永久移动
  • 302:暂时移动
  • 307:临时重定向
  • 410:走了
  • 404:未找到

请注意,第一次成功发送页面时,您仍会获得MISS

[1] http://book.varnish-software.com/3.0/VCL_Basics.html#the-initial-value-of-beresp-ttl

  • @Leonid,404通常由反向代理缓存,因为它不表示上游服务器出现故障:而是正确接收和处理请求,但请求的资源不存在. (3认同)