原文出处:https://www.varnish-cache.org/docs/3.0/tutorial/devicedetection.html
设备检测室根据用户请求的User-Agent来判断返回正确的内容。
这样的例子包括对于手机用户这样小屏幕并且高延迟网络发送压缩的文件,或者提供客户端能辨认的流媒体编码格式。
有几种策略来处理这样的用户请求:1)redirect到其他的URL. 2) 对于特殊的用户使用不同的后台;3)改变后台的请求以保证后台发送处理过的内容。
To make the examples easier to understand, it is assumed in this text that all the req.http.X-UA-Device header is present and unique per client class that content is to be served to.
Setting this header can be as simple as:
sub vcl_recv {
if (req.http.User-Agent ~ "(?i)iphone" {
set req.http.X-UA-Device = "mobile-iphone";
}
}
There are different commercial and free offerings in doing grouping and identifiying clients in further detail than this. For a basic and community based regular expression set, see https://github.com/varnish/varnish-devicedetect/ .
Serve the different content on the same URL
The tricks involved are: 1. Detect the client (pretty simple, just include devicedetect.vcl and call it) 2. Figure out how to signal the backend what client class this is. This includes for example setting a header, changing a header or even changing the backend request URL. 3. Modify any response from the backend to add missing Vary headers, so Varnish' internal handling of this kicks in. 4. Modify output sent to the client so any caches outside our control don't serve the wrong content.
All this while still making sure that we only get 1 cached object per URL per device class.
Example 1: Send HTTP header to backend
The basic case is that Varnish adds the X-UA-Device HTTP header on the backend requests, and the backend mentions in the response Vary header that the content is dependant on this header.
Everything works out of the box from Varnish' perspective.
VCL:
sub vcl_recv {
# call some detection engine that set req.http.X-UA-Device
}
# req.http.X-UA-Device is copied by Varnish into bereq.http.X-UA-Device
# so, this is a bit conterintuitive. The backend creates content based on
# the normalized User-Agent, but we use Vary on X-UA-Device so Varnish will
# use the same cached object for all U-As that map to the same X-UA-Device.
#
# If the backend does not mention in Vary that it has crafted special
# content based on the User-Agent (==X-UA-Device), add it.
# If your backend does set Vary: User-Agent, you may have to remove that here.
sub vcl_fetch {
if (req.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";
}
}
# comment this out if you don't want the client to know your
# classification
set beresp.http.X-UA-Device = req.http.X-UA-Device;
}
# to keep any caches in the wild from serving wrong content to client #2
# behind them, we need to transform the Vary on the way out.
sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device", "User-Agent");
}
}
Example 2: Normalize the User-Agent string
Another way of signaling the device type is to override or normalize the User-Agent header sent to the backend.
For example
User-Agent: Mozilla/5.0 (Linux; U; Android 2.2; nb-no; HTC Desire Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
becomes:
User-Agent: mobile-android
when seen by the backend.
This works if you don't need the original header for anything on the backend. A possible use for this is for CGI scripts where only a small set of predefined headers are (by default) available for the script.
VCL:
sub vcl_recv {
# call some detection engine that set req.http.X-UA-Device
}
# override the header before it is sent to the backend
sub vcl_miss { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
sub vcl_pass { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
# standard Vary handling code from previous examples.
sub vcl_fetch {
if (req.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";
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
}
sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device", "User-Agent");
}
}
Example 3: Add the device class as a GET query parameter
If everything else fails, you can add the device type as a GET argument.
The client itself does not see this classification, only the backend request is changed.
VCL:
sub vcl_recv {
# call some detection engine that set req.http.X-UA-Device
}
sub append_ua {
if ((req.http.X-UA-Device) && (req.request == "GET")) {
# if there are existing GET arguments;
if (req.url ~ "\?") {
set req.http.X-get-devicetype = "&devicetype=" + req.http.X-UA-Device;
} else {
set req.http.X-get-devicetype = "?devicetype=" + req.http.X-UA-Device;
}
set req.url = req.url + req.http.X-get-devicetype;
unset req.http.X-get-devicetype;
}
}
# do this after vcl_hash, so all Vary-ants can be purged in one go. (avoid ban()ing)
sub vcl_miss { call append_ua; }
sub vcl_pass { call append_ua; }
# Handle redirects, otherwise standard Vary handling code from previous
# examples.
sub vcl_fetch {
if (req.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";
}
# if the backend returns a redirect (think missing trailing slash),
# we will potentially show the extra address to the client. we
# don't want that. if the backend reorders the get parameters, you
# may need to be smarter here. (? and & ordering)
if (beresp.status == 301 || beresp.status == 302 || beresp.status == 303) {
set beresp.http.location = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
}
sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device", "User-Agent");
}
}
Different backend for mobile clients
If you have a different backend that serves pages for mobile clients, or any special needs in VCL, you can use the X-UA-Device header like this:
backend mobile {
.host = "10.0.0.1";
.port = "80";
}
sub vcl_recv {
# call some detection engine
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
set req.backend = mobile;
}
}
sub vcl_hash {
if (req.http.X-UA-Device) {
hash_data(req.http.X-UA-Device);
}
}
Redirecting mobile clients
If you want to redirect mobile clients you can use the following snippet.
VCL:
sub vcl_recv {
# call some detection engine
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
error 750 "Moved Temporarily";
}
}
sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = "http://m.example.com" + req.url;
set obj.status = 302;
return(deliver);
}
}
相关推荐
EcomDev_Varnish, Magento的高级 varnish 实现 varnish-集成Magento varnish Vache模块的实现系统要求PHP 5.3或者更高版本CE1.6. x-1.9.x/ee1.11.x-1.14.x varnish-安装可以使用 shell 脚本
varnish 安装包 varnish 安装包 varnish 安装包 varnish 安装包
linux-varnish配置
varnish+lighttpd配置
在反向代理,web加速方面,varnish已经有足够能力代替squid。挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。 2.作者:Poul-Henning Kamp是FreeBSD的内核开发者之一。
本资源包含Varnish的安装文件varnish-3.0.5-1.el6.x86_64.rpm 和varnish-libs-3.0.5-1.el6.x86_64.rpm,是搭建cdn的高性能HTTP加速器Varnish服务器必需的安装包,下载后解压即可,具体安装配置过程清移步我的博客
varnish 官方白皮书文档,包含配置信息,说明信息,方便查阅使用。
varnish测试报告
Varnish是一款全新的cache软件,据作者说采用的是最新的软件体系机构,和现在的硬件体系配合紧密。远胜过以前的Squid,性能可以提高3倍至10倍。
主要介绍了基于centos6.5使用varnish实现网站动静分离,需要的朋友可以参考下
Varnish权威指南(中文)
Varnish 2.1.2 缓存负载,做反向代理缓存优于squid
varnish-magento
laravel-varnish, 使 varnish 和 Laravel 在一起很好 使 varnish 和 Laravel 在一起很好 这个包提供了一个简单的方法,可以在 Laravel 中使用 varnish 4 ( 或者 5 ) 。 它提供了路由中间件,在应用到路由时,将确保 ...
Getting Started with Varnish Cache: Accelerate Your Web Applications by Thijs Feryn English | 13 Mar. 2017 | ASIN: B06XP3D6Z2 | 253 Pages | AZW3 | 720.91 KB How long does it take for your website to ...
教程名称: Varnish配置教程和学习资料合集【】HTTP加速器varnish安装部署【】varnish cache 配置使用ChinaUnix【】varnish 原理【】Varnish-vcl的配置【】varnish配置实例 资源太大,传百度网盘了,链接在附件中,...
squid、varnish、ngx_cache的性能测试对比报告
nginx和php和varnish配置
Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。 本文档详细讲解了varnish的安装配置过程以及配置文件详解
varnish的VCL的详细解析,网上整理