Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx add_header Set-Cookie expires not working

I am trying to set up an nginx server that sets certain parameters in a cookie when hit on a certain location.

I have the following configuration that puts the parameters in the cookie but the expiration does not work. This is my configuration:

server {
    listen       8800 default_server;
    include /etc/nginx/default.d/*.conf;

    server_name test.net;

    location /initial {
            add_header Set-Cookie lcid=1043;
            add_header Set-Cookie expires=60;
    }
}

Based on my understanding, after 60 seconds of setting this cookie up the cookie should expire and get removed. However, it persists.

Is there anything wrong with this configuration?

Any help would be appreciated, Thanks!

like image 402
penguin Avatar asked Apr 16 '19 00:04

penguin


1 Answers

The above configuration will result in two Set-Cookie headers set in response sent to the client.

Set-Cookie: lcid=1043
Set-Cookie: expires=60

"Expires" is a cookie attribute which should be present in the header where you are setting the cookie name and value. Also one more point to note here is that the Expires attribute of the cookie only takes a fixed time stamp (Ex : Expires=Wed, 21 Oct 2015 07:28:00 GMT) and not the duration. If you need to specify the validity of the cookie in terms of duration then you need to set the Max-Age attribute instead of Expires.

So you need to change your configuration to have only one add_header directive which looks like below

add_header Set-Cookie "lcid=1043; Max-Age=60";

This will make sure that client receives only one Set-Cookie header in the response with the appropriate expiry value set as specified in the Max-Age attribute of the cookie.

like image 143
gingerNinja Avatar answered Oct 01 '22 22:10

gingerNinja