Monday, September 17, 2012

A/B Testing with nginx via rewrite

Here is how you can setup 50% of your users to see a different homepage via nginx.

We split them based on the last digit of their IP address:

location / {
    if ($remote_addr ~ "[02468]$") {
        rewrite ^(.+)$ /a$1 last;
    }
    rewrite ^(.+)$ /b$1 last;
}
location /a {
    alias   /Users/dogself/site;
    index  index.html index.htm;
    ssi    on;
}
location /b {
    alias   /Users/dogself/site;
    index  index2.html index2.htm;
    ssi    on;
}

The trick here is that instead of using the root directive you should use the alias directive. Using alias means that you dont need to create an 'a' and 'b' directories on your server for this to work.

The downside is that now you have /a/ and /b/ in your path. If you know a way around that, let me know!

No comments:

Post a Comment