0

我正在尝试通过让 Varnish 继续提供这些页面的旧缓存版本(又名宽限模式)来解决后端服务器的问题,该服务器将不时开始提供具有 200 OK 响应的空白页面。

首先,我尝试检查 中的响应vcl_fetch,但据我所知,没有办法确定 中的内容长度vcl_fetch。然后我尝试在vcl_deliver(Content-Length 标头可用的地方)进行这项工作。这确实有效,但我不知道如何删除坏的缓存对象(带有空白页的对象),所以这似乎是不行的。

有人建议我在 obj.grace 和 obj.ttl 中设置vcl_deliver,这是我当前的代码:

sub vcl_deliver {
  # If the front page is blank, invalidate this cached object, in hope
  # that we'll get a new one.
  if (req.url == "/" && std.integer(resp.http.content-length, 0) < 1000) {
    set obj.grace = 0m;
    set obj.ttl = 0m;

    return(restart);
  }
}

但是,Varnish 不喜欢这样,并且在我尝试加载 VCL 时给了我这个错误:

Message from VCC-compiler:
'obj.grace': cannot be set in method 'vcl_deliver'.
At: ('input' Line 146 Pos 9)
    set obj.grace = 0m;
--------#########------

obj.ttl如果我删除该行,我会得到同样的错误obj.grace- 两者似乎都不可写入vcl_deliver,即使文档另有说明。这是在清漆 3.0.2 上。

4

2 回答 2

2

What i did is check the Content-Length for 0 and 20 in sub_vcl_fech and restart when this happens

if (beresp.http.Content-Length == "0" || beresp.http.Content-Length == "20"){
    return(restart);
}

content length of 20 is what my server returned when an error occurs.

in the sub vcl_recv i added a check on amount of restarts maximum of 2

if(req.restarts == 2){
   error 500 req.http.host;
}

Option 2

another option i got from the varnish documentation. https://www.varnish-cache.org/docs/3.0/tutorial/handling_misbehaving_servers.html

  1. Throw error in the vcl_fetch like: error 751 req.http.host;
  2. add magic marker in the vcl_error.
  3. add return(restart); in the vcl_error
  4. check for magic marker set in vcl_recv or vcl_fetch
于 2014-08-04T13:00:29.920 回答
0

在 vcl_deliver 中执行此操作为时已晚。这个 sub 在向客户端发送内容之前被调用,并且 obj 不再可用(只有 'resp' 不包含任何 ttl 或 Grace 参数)。

您是否尝试在 vcl_fetch 中执行此操作?您无需调用“restart”,而是直接调用“hit_for_pass”。

无论如何,(不确定)我不认为可以根据响应内容使用宽限模式,因为它应该在您无法获得任何内容更新(后端故障)时触发。也许它可以通过将后端更改为“僵尸”并重新启动请求来工作,但可以肯定的是,一旦你在 vcl_fetch 中,就会......获取响应,并且不会触发宽限模式。

于 2012-04-02T15:48:58.097 回答