在 Varnish 4 中重试期间更改后端 [英] Change backend during retry in Varnish 4

查看:29
本文介绍了在 Varnish 4 中重试期间更改后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在 Varnish 4 中重试时更改后端.我们已经在使用 Varnish 3 的不同(较旧)应用程序上进行了这项工作,但我一直无法弄清楚v4,也没找到太多文档.我们想要的设置是有 2 组控制器 - 一组用于初始请求尝试在同一数据中心作为 varnish 的本地服务器,因为这样更快,然后只有当失败时,从不同的控制器随机选择其他服务器数据中心.

I'd like to be able to change the backend on a retry in Varnish 4. We've got this working on a different (older) application using Varnish 3, but I haven't been able to figure it out for v4, nor find much documentation. The setup we want is to have 2 sets of directors - one for the initial request trying a local server in the same datacenter as varnish because this is way faster, and then only if that fails, pick randomly from a different director for servers in other datacenters.

在 v3 中,这很容易:

In v3, this was easy:

sub vcl_recv {
    if (req.restarts == 0) {
        set req.backend = defaultdirector;
    } else {
        set req.backend = backupdirector;
    }
}

#Then in vcl_fetch and/or vcl_error something like:
if (beresp.status >= 500 && req.restarts < some_max) {
    return(restart);
}

但是现在在 v4 中,restart 应该被替换为 retry,整个文档是:

But now in v4, restart has supposedly been replaced with retry, with the entire documentation being:

在 3.0 中,可以在注意到后端响应错误后返回(重新启动)以更改为不同的后端.

In 3.0 it was possible to do return(restart) after noticing that the backend response was wrong, to change to a different backend.

现在称为 return(retry),并跳回到 vcl_backend_fetch.

This is now called return(retry), and jumps back up to vcl_backend_fetch.

这只影响后端获取线程,不影响客户端处理.

This only influences the backend fetch thread, client-side handling is not affected.

但是我仍然看到一些人的示例代码包含 return(restart) 而不是 return(retry),并且没有一个使用 retry 命令的示例.

Yet I still see a few people's example code containing return(restart) rather than return(retry), and not a single example of it working with the retry command.

我明白varnish不应该再次做vcl_recv中的所有工作(比如剥离cookies),因为只是与后端的通信失败了,所以反弹回后端获取是有意义的而不是重做所有前端处理,但是如果我尝试更改 vcl_backend_fetch 中的后端,我会收到编译错误.我该怎么做?

I understand that varnish should not have to do all of the work in vcl_recv again (such as stripping cookies), since it was only the communication with the backend that failed, so it does make sense to bounce back to the backend fetch rather than redo all the frontend processing, but I get a compile error if I try to change the backend in vcl_backend_fetch. How do I make this work?

推荐答案

official documentation 有点误导.实际上,restart 仍然存在:您可以在 vcl_deliver 中捕获错误并使用 req.backend_hint<在 vcl_recv 中相应地设置后端/强>:

the official documentation is kind of misleading. In fact, restart still exists: you can catch the error in vcl_deliver and set the backend accordingly in vcl_recv using req.backend_hint:

sub vcl_recv {
    if (req.restarts == 0) {
        set req.backend_hint = defaultdirector.backend();
    } else {
        set req.backend_hint = backupdirector.backend();
    }
}

sub vcl_deliver {
    if (resp.status >= 500 && req.restarts < some_max) {
        return(restart);
    }
}

或者,如果更合适,您可以在 vcl_backend_responsevcl_backend_fetch 之间使用 retry :

Or, if it is more adequate, you can use retry between vcl_backend_response and vcl_backend_fetch:

sub vcl_backend_fetch {
    if (bereq.retries > 0) {
        set bereq.backend = backupdirector.backend();
    }
}

sub vcl_backend_response {
    if (beresp.status >= 500 && bereq.retries < some_max) {
        return(retry);
    }
}

这篇关于在 Varnish 4 中重试期间更改后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆