设置 cookie 时指定到期日期
Specify expiration date when setting cookies
我想在 VCL 中设置 cookie 时指定到期日期。我目前有这样的东西:
add resp.http.Set-Cookie = "language=" + req.http.X-Language + "; path=/";
我知道我必须添加如下内容:
Expires=Thu, 01 Jan 1970 00:00:00 GMT
Varnish 中是否有一个内置函数可以让我动态地将到期日期设置为将来的任何日期?我一直在看他们的文档,但到目前为止没有运气。
非常感谢您。
-天使
如果你使用 Varnish 4,你应该使用 Cookie VMOD。
来自文档:https://github.com/varnish/varnish-modules/blob/master/docs/vmod_cookie.rst
format_rfc1123
STRING format_rfc1123(TIME now, DURATION timedelta)
Description
Get a RFC1123 formatted date string suitable for inclusion in a Set-Cookie response header.
Care should be taken if the response has multiple Set-Cookie headers. In that case the header vmod should be used.
Example
sub vcl_deliver {
# Set a userid cookie on the client that lives for 5 minutes.
set resp.http.Set-Cookie = "userid=" + req.http.userid + "; Expires=" + cookie.format_rfc1123(now, 5m) + "; httpOnly";
}
更新 - 工作解决方案:
不确定此语法是否特定于 Fastly,但我使用了它:time.add(now,1d)
:
add resp.http.Set-Cookie = "language=" + req.http.X-Language + ";expires="+ time.add(now,1d) +"; path=/";
我想在 VCL 中设置 cookie 时指定到期日期。我目前有这样的东西:
add resp.http.Set-Cookie = "language=" + req.http.X-Language + "; path=/";
我知道我必须添加如下内容:
Expires=Thu, 01 Jan 1970 00:00:00 GMT
Varnish 中是否有一个内置函数可以让我动态地将到期日期设置为将来的任何日期?我一直在看他们的文档,但到目前为止没有运气。
非常感谢您。
-天使
如果你使用 Varnish 4,你应该使用 Cookie VMOD。 来自文档:https://github.com/varnish/varnish-modules/blob/master/docs/vmod_cookie.rst
format_rfc1123 STRING format_rfc1123(TIME now, DURATION timedelta) Description Get a RFC1123 formatted date string suitable for inclusion in a Set-Cookie response header. Care should be taken if the response has multiple Set-Cookie headers. In that case the header vmod should be used. Example sub vcl_deliver { # Set a userid cookie on the client that lives for 5 minutes. set resp.http.Set-Cookie = "userid=" + req.http.userid + "; Expires=" + cookie.format_rfc1123(now, 5m) + "; httpOnly"; }
更新 - 工作解决方案:
不确定此语法是否特定于 Fastly,但我使用了它:time.add(now,1d)
:
add resp.http.Set-Cookie = "language=" + req.http.X-Language + ";expires="+ time.add(now,1d) +"; path=/";