How to do http redirect on varnish
February 7th, 2009
There is no built in function to do redirecting in Varnish. After googling around, here is the result.
Using error page of varnish to return an object with location header, since obj is not available within vcl_recv.
sub vcl_recv { if (req.http.host ~ "^(www.)?mudy.info$") { error 302; } }
sub vcl_error { if (obj.status == 302 && req.http.host ~ "^(www.)?mudy.info$") { set obj.http.Location = "http://blog.mudy.info/"; } }
I am unsure whether there is a simpler way to verify the request, so I test the same condition again inside vcl_error.
If you want request url appended to redirection, you can use this instead.
set obj.http.Location = "http://blog.mudy.info" req.url;
Thanks for pointing this out. Saved me a lot of time.