Apache Traffic Server(ATS) 根据User Agent实现302重定向
2017-03-10 16:57
447 查看
需求:通过User Agent判断手机用户,重定向到手机页面。
首先看github上面的例子,或者直接看官网。
其实很简单,直接来吧
修改remap.conf配置文件,加一行配置:
重新加载配置文件:
测试:
完美收工。
首先看github上面的例子,或者直接看官网。
其实很简单,直接来吧
#判断是否有手机ua关键字的,举了些例子。 function isMobile(userAgent) ua_array = {'android','iphone','ipad','mqqbrowser','windows phone','huawei','htc','meizu','oppo','vivo','xiaomi'} n = table.getn(ua_array) for i= 1, n do if(string.match(userAgent,ua_array[i])) then return true end end return false end #在返回的header里面加入Location,即302之后的地址。 function send_response() if(remap_url) then ts.client_response.header['Location'] = remap_url end return 0 end #判断访问来自手机后返回302 function do_remap() local ua = ts.client_request.header['User-Agent'] if(isMobile(string.lower(ua))) then local uri = ts.client_request.get_uri() if(string.match(uri,'^/$')) then ts.http.set_resp(302) remap_url = "http://wap.test.com/" end end ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, send_response) end
修改remap.conf配置文件,加一行配置:
map http://www.test.com/ http://www.test.com/ @plugin=/opt/ts/libexec/trafficserver/tslua.so @pparam=/opt/ts/lib/test.lua
重新加载配置文件:
/opt/ts/bin/traffic_ctl config reload
测试:
root@zhi:~# curl -v -H "User-Agent:iphone" http://www.test.com/ >/dev/null % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 192.168.1.6... * Connected to www.gameabc.com (192.168.1.6) port 80 (#0) > GET / HTTP/1.1 > Host: www.gameabc.com > Accept: */* > User-Agent:iphone > < HTTP/1.1 302 Found < Date: Fri, 10 Mar 2017 08:54:15 GMT < Connection: keep-alive < Server: CUWEBCACHE/2.3.4 < Cache-Control: no-store < Content-Type: text/html < Content-Language: en < NODE: HIT < Location: http://wap.test.com/ < Content-Length: 207 < { [207 bytes data] 100 207 100 207 0 0 15572 0 --:--:-- --:--:-- --:--:-- 15923 * Connection #0 to host www.gameabc.com left intact
完美收工。
相关文章推荐
- robotframework封装关键字用于控制ats(apache traffic server)实现ats自动化测试
- Apache Traffic Server(ats)
- ATS源码目录结构(Apache Traffic Server 学习笔记 3)
- 高性能反向代理Apache Traffic Server配置小记 [ats cdn cache]
- ATS缓存配置(Apache Traffic Server 学习笔记 2)——持续更新
- Apache Traffic Server(ATS)缓存Content-Length为0字节的问题
- ATS 出现“400 Multi-Hop Cycle Detected”的原因及解决方法(Apache Traffic Server 学习笔记 3)
- SQUID与ATS (Apache Traffic Server) 压力测试
- ATS(Apache Traffic Server 4.2)配置SSL
- ATS功能(Apache Traffic Server 学习笔记 1)
- SQL Server中根据某个字段,ID字段自动增长的实现
- ApacheHttpServer+mod_perl实现防盗链
- Apache Traffic Server 安装
- Apache Traffic Server管理工具
- Apache Traffic Server的优点
- apache traffic server官网的几张图片
- Windows下Apache配置实现HTTP服务器(Server, Proxy, Redirect)
- Apache Http Server与Tomcat实现负载均衡和集群
- Apache TrafficServer作为反向代理服务器一例
- Apache Traffic Server 判断 HTTP 对象是否新鲜(fresh)的过程