whitesiroi
whitesiroi

Reputation: 2833

How to redirect on specific url - Varnish?

Sorry, I'm new to Varnish.

I'm trying bunch of stuff in my /etc/varnish/mysite.vlc but can't make it work.

I want to redirect on specific url to another url. Example: If someone access http://mysite1/thepage1.xml it should go to 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

Any help would be appreciated.

Upvotes: 2

Views: 8332

Answers (1)

Jacob Rastad
Jacob Rastad

Reputation: 1171

How to redirect in 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);
  }
}

How to redirect in 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);
  }
}

Upvotes: 7

Related Questions