I use nginx as a reverse proxy to server a website, every request will be transferred to backdoor application by nginx, and once a response is generated, it will be replied to client by nginx. What I want to do is add a custom cookie when nginx get the response from backdoor application.
Suppose My domain is xxx.com, from the user's view, what I want it when he visits any page of my site, his browser will get a cookie username looks like the following:
First try
My nginx conf is:
location / {
add_header Set-Cookie "username=$http_id; Domain=.xxx.com; Path=/" always;
}
When visits every page of my site, nginx will add the username combine with a new path(path is not /, it is $uri variable in nginx), then there are so many duplicate cookie username under my site(they are only different at path). Looks like:
Second try
I change the conf to:
location = / {
add_header Set-Cookie "username=$http_id; Domain=.xxx.com; Path=/" always;
}
This time, there are no duplicate cookie item, but one must visit xxx.com/. If one just visit some pages but not the /,cookie will not be set.
Question
What conf can let nginx to write cookie just once when user visit any page of my site?

