Imagine this case. Running an application in the root domain, and we want to share cookie across subdomain(like The easiest way come to mind is modifying the Domain value in Set-Cookie header.





By express or any other modern web framework, this change could be done in the application layer(via some cookie/session configuration). However in my case, I can't simply change cookie domain due to some legacy package problem. (I JUST CANT find out why...)

Since the app runs behind nginx reverse proxy, I do some googling and found that there is a built-in module for solving these kind of problem in nginx.

proxy_cookie_domain module

Here are the patterns I've tried(but none of them works):

proxy_cookie_domain "";
proxy_cookie_domain *;
proxy_cookie_domain localhost;

And finally this works:

proxy_cookie_domain ~^(.+)($ "$1 $3";

The regex split original cookie and rewrite the middle part(domain part).


I think it's related to the complex of current cookie. The above example not match server cookie, so I need to specify the regex pattern explicitly.

That's it :p