Re: proxy_cache_bypass and proxy_no_cache

                 set $dont_cache 0;

it.
You have to set it at the server level to make it work.

set $dont_cache 1;

if ($cookie_route) {
set $dont_cache 0;
}

location /test {
proxy_pass http://a/;
proxy_cache_bypass $dont_cache;
proxy_no_cache $dont_cache;
}

If creates an implicit location.

If is evil: If is Evil… when used in location context | NGINX

As Igor said above use map and get clear of the if directive quirks.

–appa