我正在使用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
}
我的问题是此标头不构成缓存哈希的一部分(如果这是正确的术语)。因此,如果首先在移动设备上查看它,那么无论设备如何,都可以缓存所有未来的请求。如果第一个请求来自台式设备,反之亦然。
如何使用mobile
和pc
来缓存两个版本的网站,以便我可以将此标头作为哈希的可靠部分,以便我可以从PHP中获取它?
答案 0 :(得分:2)
以下是您需要执行此操作的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