gpt4 book ai didi

security - 删除 nginx 反向代理中的特定 cookie

转载 作者:行者123 更新时间:2023-12-04 11:38:12 47 4
gpt4 key购买 nike

我有一个 nginx作为将我的请求代理到不同目的地的反向代理。客户端发送不同到 nginx .我想删除我的一个位置的特定 cookie。例如,如果客户端发送 cookie A 和 B,我想发送一个表单给 /api .
我怎样才能做到这一点?

最佳答案

假设您正在使用 proxy_pass指令和您的 cookie 名称是 my_cookie ,您可以从 Cookie 中剪切此 cookie 及其值这样的 HTTP header :

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 的开头、中间或结尾 header 值。这里有几个例子展示了这个正则表达式如何在不同的字符串上工作:
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_headerproxy_set_header指令。

关于security - 删除 nginx 反向代理中的特定 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67548886/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com