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'] = 0
  6. local uagent = ts.client_request.header['User-Agent']
  7. if string.find(uagent, 'haoyu') then
  8. ts.ctx['is_forbidden'] = 1
  9. ts.http.set_resp(302, "302 Moved")
  10. return TS_LUA_REMAP_DID_REMAP_STOP
  11. end
  12. }
  13. script send_response {
  14. if ts.ctx['is_forbidden'] == 1 then
  15. ts.client_response.header['Location'] = 'http://err.haoyu.com/'
  16. return 0
  17. end
  18. }
  19. }
  20. }
  • 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.com
  5. 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 Header
  5. 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 then
  7. ts.ctx['cstatus'] = 'hit'
  8. else
  9. ts.ctx['cstatus'] = 'miss'
  10. end
  11. }
  12. script send_response {
  13. -- 添加Cache状态头
  14. ts.client_response.header['Cache-Status'] = ts.ctx['cstatus']
  15. }
  16. }
  17. }