TransWikia.com

nginx: different configuration based on cookie

Server Fault Asked by Jay Paroline on November 10, 2021

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

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

Answered by user322818 on November 10, 2021

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/

Answered by Phil Taprogge on November 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP