nginx ab部署       

nginx ab部署

  local ck = require("cookie")
  local cookie = ck:new()
  local abtest = cookie:get("rocket")
  if abtest == nil then
    math.randomseed(tostring(os.time()):reverse():sub(1, 6))
    r = math.floor(math.random()*10)
    if r == 1 then
      if ngx.req.get_headers()["Cookie"] then
        ngx.header["Set-Cookie"]="rocket=1; path=/;"..ngx.req.get_headers()["Cookie"]
      else
        ngx.header["Set-Cookie"]="rocket=1; path=/;"
      end
      ngx.log(ngx.NOTICE, "set abtest=1 go to webv2")
      return 1
    else
      if ngx.req.get_headers()["Cookie"] then
        ngx.header["Set-Cookie"]="rocket=0; path=/;"..ngx.req.get_headers()["Cookie"]
      else
        ngx.header["Set-Cookie"]="rocket=0; path=/;"
      end
      ngx.log(ngx.NOTICE, "set abtest=0 go to webv1")
      return 0
    end
  else
    if abtest == "1" then
      ngx.log(ngx.NOTICE, "abtest=1 go to webv2")
      return 1
    else
      ngx.log(ngx.NOTICE, "abtest=0 go to webv1")
      return 0
    end
  end