Can I Vary on a custom header?

I'm bucketing User-Agents by device using something like varnish-devicedetect and storing the result in X-UA-Device on the request and the response.

I've seen several recommendations to vary on User-Agent. Any reason not to vary instead on X-UA-Device ? Seems like it'd be nicer to downstream caches.


由于X-UA-Device在客户端请求或任何下游代理服务器(它在Varnish中生成)上不可用,因此必须在原始Vary标头上进行更改。


Although varying on X-UA-Device is incorrect for downstream caches, Varnish itself can still benefit from that optimization if you rewrite the Vary header in vcl_deliver :

sub vcl_deliver {
    if (resp.http.Vary) {
        set resp.http.Vary = regsub(resp.http.Vary,
                                    "(?i)X-UA-Device",
                                    "User-Agent");
    }
}

This way, Varnish varies its cache on X-UA-Device and downstream caches vary on User-Agent .

In your question, you mentioned you were adding X-UA-Device to the response header as well as the request header. In that case, the above suggestion will not work and you will instead need to send Vary: User-Agent unconditionally:

sub vcl_fetch {
    set beresp.http.X-UA-Device = req.http.X-UA-Device;
    if (!beresp.http.Vary) {
        set beresp.http.Vary = "User-Agent";
    } elsif (beresp.http.Vary !~ "(?i)User-Agent") {
        set beresp.http.Vary = beresp.http.Vary + ", User-Agent";
    }
}

(I was not sure whether you were setting the X-UA-Device response header for the benefit of client-side scripts, or in the hope that it would be recognized by downstream caches.)

链接地址: http://www.djcxy.com/p/18274.html

上一篇: Android ActionBar / ActionBarSherlock多选择微调

下一篇: 我可以自定义标题吗?