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.

ということでredirect機能がbuildinされていないので、小技で対応しないといけない?

いったん独自な(この例では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 = "http://www.example.com/iphoneversion/";
    set obj.status = 302;
    return(deliver);
  }
}

というわけで2箇所設定しないといけないし面倒なので、backendサーバ側で設定したほうが楽かな。