Forum: NGINX set cookie if not already set

Posted by amodpandey (Guest)
on 2012-12-13 19:29
(Received via mailing list)
I figured out a simple solution so thought to share it ( to get feedback 
or
may be to help ). In an earlier discussion I got to know about map and 
why
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 
any
cookie and we are good to go :)

HTH

Any feedback?

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,233983,233983#msg-233983
Posted by Valentin V. Bartenev (Guest)
on 2012-12-14 00:36
(Received via mailing list)
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 
expression.
Moreover, it looks plain and more readable.

 wbr, Valentin V. Bartenev

--
http://nginx.com/support.html
http://nginx.org/en/donation.html
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.