S
S
Serdonda2015-10-13 12:54:32
Nginx
Serdonda, 2015-10-13 12:54:32

How to set cookies with nginx for directories?

Good afternoon. Please tell me this question:
when you click on a link with parameters like site.com/index.html?tracker=123 , you need to write the value of the
nginx config parameter in cookies like this:
location / {
root /var/site;
index index.html;
}
location ~ \.html$ {
root /var/site;
index index.html;
if ($arg_tracker) { add_header Set-Cookie "tracker=$arg_tracker;Max-Age=50"; }
}
Everything works fine for the root directory, but as soon as the link becomes
site.com/category/index.html?tracker=123 no cookie is written.
Moving adding cookies to location / does nothing either.
Tell me what's wrong?
UPD: emphasizing "move your if to the root" DOES NOT work
Solution:
location ~ \.html {
if ($arg_tracker) { add_header Set-Cookie "tracker=$arg_tracker;Path=/;Max-Age=50"; }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TyzhSysAdmin, 2015-10-13
@Serdonda

And what is the real problem?
You have links site.com/category/?tracker=123 move
your if to the root, or I didn’t understand something.

location / {
root /var/site;
index index.html;
if ($arg_tracker) { add_header Set-Cookie "tracker=$arg_tracker;Max-Age=50"; }
}

or something like that should work :)
location / {
  root /var/site;
  index index.html;
  if ($args ~ "^tracker=(\d+)") { 
    set $key_track $1;
    add_header Set-Cookie "tracker=$key_track;Max-Age=50";
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question