1

We are working on a major refactor of our site. For the rollout we would like to start sending a small subset of our users to the new version. We don't want them to keep switching back and forth between old and new so we are planning to set a cookie on their first visit and then use that to decide what to do.

We know how to make all of this work except for changing the nginx config based on the cookie. The configurations are mostly the same except we need to use a different root, and try_files is different between the two, as the old version is structured like page_name/index.php and the new version routes everything through one index.php file.

For changing the root based on a cookie I have that working using a map, but I'm not sure what to do about the try_files part. I am trying to avoid using if based on if-is-evil. The old version doesn't use try_files at all, and the new version uses try_files $uri $uri/ /index.php?$args;

What's the best way to proceed without using if, or should we be safe to use if in this case?

2 Answers 2

0

I believe this is only possible with NGINX Plus using sticky route.

Have a look at https://www.nginx.com/blog/performing-a-b-testing-nginx-plus/

0

I think we can process this with openresty

here is example

nginx conf file
server {
    listen 8081;
    server_name localhost;
    location / {
        content_by_lua_block {
            ngx.say('8081 version')
        }
    }
}
server {
    listen 8082;
    server_name localhost;
    location / {
        content_by_lua_block {
            ngx.say('8082 version')
        }
    }
}
server {
    listen 80;
    server_name localhost;

    location / {
        set $versionupstream  "";
        rewrite_by_lua_block {
            local newcookie = ngx.var.cookie_Foo

            if newcookie == "bar" then
                ngx.var.versionupstream = "127.0.0.1:8081"
            else
                ngx.var.versionupstream = "127.0.0.1:8082"
            end
        }

        proxy_pass http://$versionupstream;
    }
}
test it

demo it

3
  • I think this will work but I would really like to avoid having to have nginx reverse proxy to itself, seems like it will slow things down unnecessarily (unless there really is no other option) Sep 8, 2016 at 4:43
  • Sorry to downvote. This is about NGINX and not OpenResty (even if it is based on NGINX).
    – Daniel F
    Mar 11, 2019 at 21:08
  • @doom.zhou could you please translate this to english? "那你真的是一个杠精呀. == Then you are really a bar." doesn't make sense to me.
    – Daniel F
    Mar 12, 2019 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .