1

我正在使用带有以下内容的 Varnish 缓存来检查移动/平板电脑设备:

sub device_detection
{
    set req.http.X-Device = "pc";

    if(req.http.User-Agent ~ "iP(hone|od)" || req.http.User-Agent ~ "Android" || req.http.User-Agent ~ "Symbian" || req.http.User-Agent ~ "^BlackBerr$
    {
            set req.http.X-Device = "mobile";
    }

    if(req.http.User-Agent ~ "^PalmSource")
    {
            set req.http.X-Device = "mobile";
    }

    if(req.http.User-Agent ~ "Build/FROYO" || req.http.User-Agent ~ "XOOM" )
    {
            set req.http.X-Device = "pc";
    }

    if((req.http.Cookie ~ "(force_desktop)"))
    {
            set req.http.X-Device = "pc";
    }

    if((req.http.Cookie ~ "(force_mobile)"))
    {
            set req.http.X-Device = "mobile";
    }
}

这成功设置了一个新标头,然后我可以使用以下方法在 PHP 中检查:

if(isset($headers['X-Device']) && $headers['X-Device'] == "mobile")
{
     // do mobile stuff here 
}

我的问题是此标头不构成缓存哈希的一部分(如果这是正确的术语)。因此,如果首先在移动设备上查看它,那么无论设备如何,都会缓存所有未来的请求。如果第一个请求来自桌面设备,反之亦然。

如何使此标头成为哈希的一部分,以便我可以可靠地从 PHP 获取它,同时仍然使用mobile和缓存网站的两个版本pc

4

1 回答 1

3

这是您需要执行此操作的 VCL 片段:

sub vcl_hash {
    if (req.http.X-Device) {
        hash_data(req.http.X-Device);
    }
}

这在官方 Varnish 文档中有介绍:

https://www.varnish-cache.org/docs/3.0/tutorial/devicedetection.html

于 2013-05-01T06:50:07.247 回答