Remap与Lua配置
Remap与Lua
在新版本里,lua已可嵌入到remap.config里,支持更灵活的基于http header编程
ts-lua 接口文档https://github.com/portl4t/ts-lua
配置说明
Remap 可支持配置Lua Hook点的五个阶段
do_remap
1. remap前阶段,该阶段可以修改存储key,拒绝服务等等操作
send_request
1. 回源发送request 阶段,该阶段可以读取、修改回源request header
read_response
1. 读取回源response 阶段,该阶段可以读取、修改回源接收的response header
send_response
1. 发送给client response 阶段,该阶段可以读取、修改发送给用户response header
cache_lookup_complete
1. Cache 读取阶段,该阶段可以判断读出的Cache状态,读取Cache中的response header等
配置示例
注: 一个remap 里可以使用多个状态,多个状态配合成一个脚本使用
do_remap
1. http www.taobao.com {2. map / http://www.taobao.com.inner.taobao.com {3. script do_remap {4. -- 判断Useragent 做跳转5. ts.ctx['is_forbidden'] = 06. local uagent = ts.client_request.header['User-Agent']7. if string.find(uagent, 'haoyu') then8. ts.ctx['is_forbidden'] = 19. ts.http.set_resp(302, "302 Moved")10. return TS_LUA_REMAP_DID_REMAP_STOP11. end12. }14. script send_response {15. if ts.ctx['is_forbidden'] == 1 then16. ts.client_response.header['Location'] = 'http://err.haoyu.com/'17. return 018. end19. }20. }21. }
send_request
1. http www.taobao.com {2. map / http://www.taobao.com.inner.taobao.com {3. script send_request {4. -- 把回源request Host 改成www.tmall.com5. ts.client_request.header['Host'] = 'www.tmall.com'6. }7. }8. }
read_response
1. http www.taobao.com {2. map / http://www.taobao.com.inner.taobao.com {3. script read_response {4. -- 按照Accept-Test 做多副本缓存5. ts.server_response.header['Vary'] = "Accept-Test"6. }7. }8. }
send_response
1. http www.taobao.com {2. map / http://www.taobao.com.inner.taobao.com {3. script send_response {4. -- 添加Response Header5. ts.client_response.header['Test'] = 'yes'6. }7. }8. }
cache_lookup_complete
1. http www.taobao.com {2. map / http://www.taobao.com.inner.taobao.com {3. script cache_lookup_complete {4. -- 获得Cache 状态5. local cache_status = ts.http.get_cache_lookup_status()6. if cached_status == TS_LUA_CACHE_LOOKUP_HIT_FRESH then7. ts.ctx['cstatus'] = 'hit'8. else9. ts.ctx['cstatus'] = 'miss'10. end11. }13. script send_response {14. -- 添加Cache状态头15. ts.client_response.header['Cache-Status'] = ts.ctx['cstatus']16. }17. }18. }
