删除 nginx 反向代理中的特定 cookie
remove specific cookie in nginx reverse proxy
我有一个 nginx
作为反向代理,将我的请求代理到不同的目的地。客户端发送不同 nginx
。我想删除我的一个位置的特定 cookie。例如,如果客户端发送 cookie A 和 B,我想为 /api
.
发送 A 表单
我该怎么做?
假设您正在使用 proxy_pass
指令并且您的 cookie 名称是 my_cookie
,您可以通过以下方式从 Cookie
HTTP header 中删除此 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]*[^;]?)(|$|;$)(?:;\s)?(.*)') {
# cut "my_cookie" cookie from the string
set $altered_cookie ;
}
# 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
header 值的开头、中间还是结尾。这里有几个例子展示了这个正则表达式如何在不同的字符串上工作:
Whole "Cookie" string
----------------------------------------------------------- -------------------- ---- ---------- ---- --------------------- -----------------------------------------
"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
指令。
我有一个 nginx
作为反向代理,将我的请求代理到不同的目的地。客户端发送不同 nginx
。我想删除我的一个位置的特定 cookie。例如,如果客户端发送 cookie A 和 B,我想为 /api
.
我该怎么做?
假设您正在使用 proxy_pass
指令并且您的 cookie 名称是 my_cookie
,您可以通过以下方式从 Cookie
HTTP header 中删除此 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]*[^;]?)(|$|;$)(?:;\s)?(.*)') {
# cut "my_cookie" cookie from the string
set $altered_cookie ;
}
# 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
header 值的开头、中间还是结尾。这里有几个例子展示了这个正则表达式如何在不同的字符串上工作:
Whole "Cookie" string
----------------------------------------------------------- -------------------- ---- ---------- ---- --------------------- -----------------------------------------
"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
指令。