如何使用Varnish添加重定向异常?

时间:2019-10-09 17:19:52

标签: varnish varnish-vcl fastly

我正在尝试重定向路径,例如www.something.com/apple/pie至www.something.com/tickets/pie-details,但也有一些例外,例如 www.something.com/apple/helloworld不会重定向到www.something.com/tickets/helloworld-details

这是我尝试过的但不起作用:

if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
    set req.url = "^/tickets/.*-details";
    error 701 req.url;
}

2 个答案:

答案 0 :(得分:1)

https://info.varnish-software.com/blog/rewriting-urls-with-varnish-redirection

作为示例(直接从帖子中):

sub vcl_recv {
    if (req.http.host != "www.varnish-software.com") {
        set req.http.location = "https://www.varnish-software.com/";
        return(synth(301));
    }
}
sub vcl_synth {
    if (resp.status == 301 || resp.status == 302) {
        set resp.http.location = req.http.location;
        return (deliver);
    }
}

您还需要正确编写req.http.location。据我了解,您想要这样的东西:

sub vcl_recv {
    if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
        set req.http.location = "/tickets + req.url + "-details";
        return(synth(301));
    }
}

答案 1 :(得分:0)

我认为进行正则表达式替代会更好。

if (req.url ~ "^/apple/.*" && req.url != "^/apple/helloworld") {
  set req.url = regsub(req.url, "^/apple/", "/tickets/");
  error 701 req.url;
}