如何重定向特定的URL - Varnish?

时间:2016-03-24 07:45:25

标签: varnish varnish-vcl

对不起,我是 Varnish 的新手。

我在 /etc/varnish/mysite.vlc 中尝试了很多东西,但无法使其正常工作。

我想将特定网址重定向到另一个网址。例: 如果有人访问http://mysite1/thepage1.xml,则应转到http://mysite2/thepage2.xml

varnishd -V
varnishd (varnish-3.0.5 revision 1a89b1f)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2011 Varnish Software AS

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:5)

如何在Varnish 3中重定向

sub vcl_recv {
  if (req.url~ "^/thepage1.xml?$") {
    error 750 "http://mysite2/thepage2.xml";
  }
}

sub vcl_error {
  if (obj.status == 750) {
    set obj.http.Location = obj.response;
    set obj.status = 301;
    return(deliver);
  }
}

如何在Varnish 4中重定向

sub vcl_recv {
  if (req.url~ "^/thepage1.xml?$") {
    return (synth (750, "http://mysite2/thepage2.xml")); #This throws a synthetic page so the request won't go to the backend
  }
}

sub vcl_synth {
  if (resp.status == 750) {
    set resp.http.Location = resp.reason;
    set resp.status = 301;
    return(deliver);
  }
}