varnish でredirectする

Varnish does not have any built in support for HTTP redirections so redirection in Varnish can only be accomplished using something of a dirty hack. You trigger an specified error and then pick this error code up in the vcl_error subroutine. See this example for details.


いったん独自な(この例では750) statusに飛ばして、vcl_error 内でstatusを判別して locationを設定する。

sub vcl_recv {
  if (req.http.user-agent ~ "iP(hone|od)") {
    error 750 "Moved Temporarily";
sub vcl_error {
  if (obj.status == 750) {
    set obj.http.Location = "";
    set obj.status = 302;