vacatia / varnish-mobiletranslate
Requires (Dev)
- php: >=5.3.1
This package is not auto-updated.
Last update: 2022-04-30 04:47:49 UTC
README
### 目标:基于https://github.com/serbanghita/Mobile-Detect库的移动用户检测的varnish即插即用解决方案。
### 如何实现:一个VCL脚本,在请求中添加一个X-UA-Device
头。
设备类型 | X-UA-Device |
---|---|
手机 | phone |
平板电脑 | tablet |
其他 | 桌面 |
### 要求 Varnish 3是必需的。如果您尝试在Varnish 2上运行此包,则不支持保证。了解VCL语言和工作流程的基础也很有用。这不是VMOD模块,因此不需要编译。
### 安装
- 将mobile_detect.vcl文件下载到您的vcl配置目录中。
/etc/varnish/
wget https://raw2.github.com/willemk/varnish-mobiletranslate/master/mobile_detect.vcl
- 将
mobile_detect.vcl
包含在您的default.vcl
文件中(见以下示例) - 从varnish的vcl_recv中调用
devicedetect
(见以下示例)
### 使用 #### 示例用例:不同后端 使用与桌面不同的后端为移动端
include "mobile_detect.vcl";
backend mobile {
.host = "10.0.0.1";
.port = "80";
}
sub vcl_recv {
call devicedetect;
if (req.http.X-UA-Device ~ "^mobile") {
set req.backend = mobile;
}
}
#### 示例用例:规范化后端的用户代理字符串 后端接收由mobile_detect生成的简化用户代理。后端可以根据相应内容提供服务。然后按设备类型进行缓存。
include "mobile_detect.vcl";
sub vcl_recv {
call devicedetect;
if (req.http.X-UA-Device) {
set req.http.User-Agent = req.http.X-UA-Device;
unset req.http.X-UA-Device;
}
}
sub vcl_hash {
#Default Hash
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
#Also hash based on device type
if (req.http.X-UA-Device) {
hash_data(req.http.X-UA-Device);
}
return (hash);
}
#### 示例用例:基于设备类型的缓存 如果您的后端根据设备类型为相同URL生成不同的内容,则应根据该设备类型进行缓存。
include "mobile_detect.vcl";
sub vcl_recv {
call devicedetect;
}
sub vcl_hash {
#Default Hash
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
#Also hash based on device type
if (req.http.X-UA-Device) {
hash_data(req.http.X-UA-Device);
}
return (hash);
}
### 缺陷 & 功能
如果您有任何缺陷报告、功能请求或想提交拉取请求,请使用适当的github工具。
### 感谢
感谢以下项目提供的帮助和灵感
https://github.com/serbanghita/Mobile-Detect
https://github.com/varnish/varnish-devicedetect
### 更新mobile_detect.vcl
您可以使用serbanghita/Mobile-Detect
的最新版本更新移动检测规则,通过执行php varnishtranslate.php
。然后将mobile_detect.vcl
推送到仓库。