在第一个后端出现 404 的情况下,使用带有 Varnish 1.0.3-2 的第二个后端

发布于 2024-08-24 15:22:23 字数 1022 浏览 5 评论 0原文

我们曾经使用 Varnish 的早期版本(我认为是 0.5 左右)进行缓存代理设置,当第一个后端出现 404 错误时,它使用“重新启动”操作将请求发送到第二个后端。

新版本的 Varnish 似乎不支持这一点 - 似乎不再支持“重新启动”操作,并且不再识别“req.restarts”变量。这种行为可能吗?

该文档似乎已经过时,许多在线示例也是如此。 man 7 vcl 似乎反映了当前的行为。

如果 Varnish 无法实现,您能建议其他解决方案吗?

以下是旧 Varnish 配置的相关部分:

sub vcl_recv {

    # remove cookies
    remove req.http.Cookie;

    if (req.restarts == 0) {
        set req.backend = backend1;
    } else if (req.restarts == 1) {
        set req.backend = backend2;
    }

    # remove any query strings
    set req.url = regsub(req.url, "\?.*", "");

    # force lookup even when cookies are present
    if (req.request == "GET" && req.http.cookie) {
        lookup;
    }
}

sub vcl_fetch {
    # we might set a cookie from the Rails app
    remove obj.http.Set-Cookie;

    # force minimum ttl of 1 year
    if (obj.ttl < 31536000s) {
        set obj.ttl = 31536000s;
    }

    if (obj.status != 200 && obj.status != 302) {
        restart;
    }
}

We used to have a caching proxy setup using a very early version of Varnish (0.5ish, I think) which used the 'restart' action to send requests to a second backend in the case of a 404 on the first.

The new version of Varnish doesn't seem to support this - the 'restart' action no longer seems to be supported, and the 'req.restarts' variable is no longer recognised. Is such behaviour possible?

The documentation seems to be out of date, as do many of the online examples. man 7 vcl seems to reflect current behaviour though.

If it's not possible with Varnish, can you suggest another solution?

Here are the relevant bits of our old Varnish config:

sub vcl_recv {

    # remove cookies
    remove req.http.Cookie;

    if (req.restarts == 0) {
        set req.backend = backend1;
    } else if (req.restarts == 1) {
        set req.backend = backend2;
    }

    # remove any query strings
    set req.url = regsub(req.url, "\?.*", "");

    # force lookup even when cookies are present
    if (req.request == "GET" && req.http.cookie) {
        lookup;
    }
}

sub vcl_fetch {
    # we might set a cookie from the Rails app
    remove obj.http.Set-Cookie;

    # force minimum ttl of 1 year
    if (obj.ttl < 31536000s) {
        set obj.ttl = 31536000s;
    }

    if (obj.status != 200 && obj.status != 302) {
        restart;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2024-08-31 15:22:23

似乎这种行为已在最新版本的 Varnish 中恢复。

It seems this behaviour has been reinstated in more recent versions of Varnish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文