我有一个
nginx
作为反向代理,将我的请求代理到不同的目的地。客户端发送不同的内容到nginx
。我想删除我所在位置之一的特定 cookie。例如,如果客户端发送 cookie A 和 B,我想发送 /api
的 A 表单。
我该怎么做?
假设您使用
proxy_pass
指令并且您的 cookie 名称为 my_cookie
,您可以通过这种方式从 Cookie
HTTP 标头中剪切此 cookie 及其值:
location /api {
# save original "Cookie" header value
set $altered_cookie $http_cookie;
# check if the "my_cookie" cookie is present
if ($http_cookie ~ '(.*)(^|;\s)my_cookie=("[^"]*"|[^\s]*[^;]?)(\2|$|;$)(?:;\s)?(.*)') {
# cut "my_cookie" cookie from the string
set $altered_cookie $1$4$5;
}
# hide original "Cookie" header
proxy_hide_header Cookie;
# set "Cookie" header to the new value
proxy_set_header Cookie $altered_cookie;
... # other proxy settings here
proxy_pass <upstream>; # change to your upstream server
}
这个复杂的正则表达式允许检查
my_cookie
cookie 是否存在,无论它是在 Cookie
标头值的开头、中间还是末尾。以下是几个示例,展示了此正则表达式如何在不同字符串上工作:
Whole "Cookie" string $1 $2 $3 $4 $5 $1$4$5
----------------------------------------------------------- -------------------- ---- ---------- ---- --------------------- -----------------------------------------
"some_cookie=value1; my_cookie=value2; other_cookie=value3" "some_cookie=value1" "; " "value2" "; " "other_cookie=value3" "some_cookie=value1; other_cookie=value3"
"some_cookie=value1; my_cookie=value2" "some_cookie=value1" "; " "value2" "" "" "some_cookie=value1"
"my_cookie=value2; other_cookie=value3" "" "" "value2; " "" "other_cookie=value3" "other_cookie=value3"
"my_cookie=value2" "" "" "value2" "" "" ""
对于那些正在寻找相同食谱但使用
fastcgi_pass
而不是 proxy_pass
的人 - 使用 fastcgi_param HTTP_COOKIE $altered_cookie if_not_empty;
而不是 proxy_hide_header
和 proxy_set_header
指令。
谢谢亚历克斯,这确实节省了我很多时间。但是你能向我解释一下为什么非捕获组
(?:;\s)?
是必要的吗?在我看来,你似乎也可以忽略这一点。
另外,我建议删除 if
(if
是邪恶的)并使用带有 map
语法的新变量:
map $http_cookie $altered_cookie {
"~(.*)(^|;\s)my_cookie=(\"[^\"]*\"|[^\s]*[^;]?)(\2|$|;$)(.*)" $1$4$5;
default $http_cookie;
}