在 AWS Cloudfront 源请求中返回 set-cookie header 的响应
returning response with set-cookie header in AWS Cloudfront origin request
在我的 CloudFront 源请求 lambda@edge 函数中,我想要 return 一个响应,它将在浏览器中设置一个 cookie 值并重定向到其他页面。我通过以下 return 语句来完成:
return {
status: '302',
statusDescription: 'Found',
headers: {
location: [
{ key: 'Location', value: 'my.website.com' },
],
'set-cookie': [
{ key: 'Set-Cookie', value: 'key=value; Max-Age=600' },
]
}
};
不幸的是,CloudFront 似乎 remove/ignore 这个 set-cookie
header 并且浏览器收到没有它的响应。有趣的是,完全相同的代码放在 CloudFront viewer-request 函数中时可以正常工作。有没有办法让 origin-request lambda 在响应中保留 set-cookie header?
解决方案原来是一个缓存策略,启用了 Cookies - include specified cookies
选项,并使用正确的白名单 cookie 名称。问题中的行为是由(documentation 状态)引起的:
Don’t forward cookies to your origin – CloudFront doesn’t cache your objects based on cookie sent by the viewer. In addition, CloudFront removes cookies before forwarding requests to your origin, and removes Set-Cookie headers from responses before returning responses to your viewers.
要防止通过列入白名单的 cookie 名称进行缓存,请将以下 header 添加到响应中:Cache-Control: no-cache="Set-Cookie"
.
在我的 CloudFront 源请求 lambda@edge 函数中,我想要 return 一个响应,它将在浏览器中设置一个 cookie 值并重定向到其他页面。我通过以下 return 语句来完成:
return {
status: '302',
statusDescription: 'Found',
headers: {
location: [
{ key: 'Location', value: 'my.website.com' },
],
'set-cookie': [
{ key: 'Set-Cookie', value: 'key=value; Max-Age=600' },
]
}
};
不幸的是,CloudFront 似乎 remove/ignore 这个 set-cookie
header 并且浏览器收到没有它的响应。有趣的是,完全相同的代码放在 CloudFront viewer-request 函数中时可以正常工作。有没有办法让 origin-request lambda 在响应中保留 set-cookie header?
解决方案原来是一个缓存策略,启用了 Cookies - include specified cookies
选项,并使用正确的白名单 cookie 名称。问题中的行为是由(documentation 状态)引起的:
Don’t forward cookies to your origin – CloudFront doesn’t cache your objects based on cookie sent by the viewer. In addition, CloudFront removes cookies before forwarding requests to your origin, and removes Set-Cookie headers from responses before returning responses to your viewers.
要防止通过列入白名单的 cookie 名称进行缓存,请将以下 header 添加到响应中:Cache-Control: no-cache="Set-Cookie"
.