我正在使用带有以下内容的 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
?