Skip to content

Instantly share code, notes, and snippets.

@bethrezen
Created October 2, 2012 08:01
Show Gist options
  • Select an option

  • Save bethrezen/3817205 to your computer and use it in GitHub Desktop.

Select an option

Save bethrezen/3817205 to your computer and use it in GitHub Desktop.
Nginx A/B test config
set $target '';
access_by_lua '
local abtest = 0
local abtestMax = 1
local userAgent = ngx.req.get_headers()["User-Agent"]
local cookie = ngx.var.cookie_abtest
if ngx.re.match(userAgent, "(yandex|google|MSIE|bot)", "i") then
cookie = 0
end
if cookie == nil then
math.randomseed( os.time() )
abtest = math.random(0, abtestMax)
ngx.header["Set-Cookie"] = {"abtest=" .. abtest .. "; path=/", "abReason=nil"}
else
if tonumber(cookie) >= 0 and tonumber(cookie) <= abtestMax then
abtest = ngx.var.cookie_abtest
else
abtest = 0
ngx.header["Set-Cookie"] = {"abtest=" .. abtest .. "; path=/", "abReason=badRange"}
end
end
ngx.var.target = abtest
';
root /var/www/meta$target;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment