Set cookie if not already set

I figured out a simple solution so thought to share it ( to get feedback
may be to help ). In an earlier discussion I got to know about map and
one should avoid to use if an try to use map.

http {

map $cookie_abt $abt {
    default "abt=a;Path=/";
    ~.+$ "";

server {
    listen 8081;

    location / {
        add_header Set-Cookie $abt;


the map returns “” ( empty ) if cookie is already else the cookie value
abt=a;Path=/" ). The good news is add_header Set-Cookie “” does not set
cookie and we are good to go :slight_smile:


Any feedback?

Posted at Nginx Forum:,233983,233983#msg-233983

On Thursday 13 December 2012 22:29:07 amodpandey wrote:


This one:

map $cookie_abt $abt {
default “”;
“” “abt=a;Path=/”;

does the same thing, but without the resource consuming regular
Moreover, it looks plain and more readable.

wbr, Valentin V. Bartenev