nginx add_header Set-Cookie 过期不起作用

标签 nginx cookies session-cookies nginx-config setcookie

我正在尝试设置一个 nginx 服务器,当点击某个位置时,它会在 cookie 中设置某些参数。

我有以下配置将参数放入 cookie 但过期不起作用。这是我的配置:

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;
    }
}

根据我的理解,设置此 cookie 60 秒后,cookie 应该会过期并被删除。然而,它仍然存在。

这个配置有什么问题吗?

任何帮助,将不胜感激,
谢谢!

最佳答案

上述配置将导致在响应发送到客户端时设置两个 Set-Cookie header 。

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

“过期”是一个 cookie 属性,它应该出现在您设置 cookie 名称和值的标题中。还有一点需要注意的是,cookie 的 Expires 属性只需要一个固定的时间戳(例如:Expires=Wed, 21 Oct 2015 07:28:00 GMT)而不是持续时间。如果您需要根据持续时间指定 cookie 的有效性,那么您需要设置 Max-Age 属性而不是 Expires。

所以你需要改变你的配置,让它只有一个 add_header 指令,如下所示
add_header Set-Cookie "lcid=1043; Max-Age=60";

这将确保客户端在响应中只接收一个 Set-Cookie header ,并按照 cookie 的 Max-Age 属性设置适当的到期值。

关于nginx add_header Set-Cookie 过期不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55698986/

相关文章:

javascript - 如何在 javascript 中创建全局 cookie?

c# - Azure 上的用户授权

php - 是否可以在 Flash 中设置与浏览器中使用的相同的 cookie

cookies - 在 Web.Config 中设置 ServiceStack Cookie 域会导致 session ID 在每次请求时发生变化

NGINX $request_uri 与 $uri

asp.net - 根据浏览器有条件地设置 ASP.NET session 和身份验证 cookie samesite 值

amazon-web-services - 弹性 beantalk 上的 Golang 应用程序似乎正在接收双重编码请求

node.js - 什么时候应该将 cookie-parser 与 express-session 一起使用?

mysql - Docker + fig/compose + nginx + node.js + mysql + redis

ssl - 如何配置 Let's encrypt 证书用于 docker 镜像中的 nginx?