Nginx: different configuration based on cookie

nginx

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?

Best Answer

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/