【发布时间】:2014-11-29 05:16:52
【问题描述】:
我正在 Varnish 4.0.2 中设置 varnish-devicedetect VCL:
https://github.com/varnish/varnish-devicedetect/blob/master/INSTALL.rst
我遵循方法 #1 的说明:“将 HTTP 标头发送到后端”
我已经通读了这个自述文件,并且已经在 Google 上搜索了很长时间,但仍有不少概念让我无法理解。
这是我的代码(摘录):
default.vcl
include "devicedetect.vcl";
sub vcl_recv {
call devicedetect;
# ... snip ...
}
sub vcl_backend_response {
# device detect
if (bereq.http.X-UA-Device) {
if (!beresp.http.Vary) { # no Vary at all
set beresp.http.Vary = "X-UA-Device";
} elseif (beresp.http.Vary !~ "X-UA-Device") { # add to existing Vary
set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
}
}
# ... snip ...
}
sub vcl_deliver {
# device detect
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device", "User-Agent");
}
# ... snip ...
}
这是我的问题。
- 当我在 Chrome 开发工具中检查响应时,为什么
Vary标头设置为User-Agent。方法#1的整个方法不是不使用用户代理,而是使用X-UA-Device吗? - 根据我阅读的其他指南...似乎这将触及每种类型的移动设备的起源(如果您查看设备检测,它分为... mobile-iphone、mobile-android、mobile-smartphone , ETC)。在我上面的代码中这是真的吗?对于任何给定的 URL(桌面和移动设备......我不希望所有移动设备-* 单独缓存),我绝对不想访问源服务器两次以上。
- 有人能描述一下上面 3 个代码块的实际作用吗?用一些外行的话来说。我真正理解的唯一一个是第一个代码块。
call devicedetect只是查看 User-Agent,然后将X-UA-Device标头设置为对后端请求的适当分组。不过,我有点困惑其他 2 个代码块的作用。 - 如果我不打算允许用户“使用桌面站点”,我可以删除带有
X-UA-Device-force的位吗? - 指南提到我应该在我的应用程序代码的后端设置一些东西。现在这就是我所拥有的(rails)。我不会更改标题或更改有关响应的任何内容。我只是改变了 HTML 的外观(对于网站的移动版本)。我应该改变标题还是什么?这是我目前所拥有的:
导轨:
def detect_device
if request.headers['X-UA-Device'] =~ /^mobile/
@device = 'mobile'
prepend_view_path Rails.root + 'app' + 'views_mobile'
else
@device = 'desktop'
end
end
【问题讨论】:
标签: ruby-on-rails caching mobile varnish varnish-vcl