Hi,
I'm trying to redirect to a different page from homepage based on a cookie. Secondary objective is to also set that cookie. The method works with Javascript but when I try to do if with nginx, it doesn't work consistently.
I have several location rules, but the one that's matched for homepage is
location /
{
try_files $uri $uri/ /cms/assets/cache/supercache/$http_host/$cache_uri/$file /cms/index.php?$args;
gzip_static on;
expires 1d;
add_header X-Test default;
}
The $cache_uri and $file vars are set elsewhere and they work.
Now, for homepage I added this rule:
location = /index.html
{
gzip_static on;
expires 1d;
add_header X-Test root;
if ($cookie_prefer ~* "someval")
{
return 302 /somepage.html?$args;
}
}
Somehow this doesn't work reliably. Crazy as it sounds, it works... sometimes. I suspect a caching issue or something, but it's even more frustrating than if it wasn't working at all.
Am I doing something wrong? Is there a better, fool-proof way for cookie redirect?
Second question is about setting the cookie itself. I know I can add a cookie with add_header but how can I specify the expiration as 1 month from now?