删除 nginx 的 proxy_pass 中的一个 cookie

删除 nginx 的 proxy_pass 中的一个 cookie

我正在尝试将proxy_pass发送到 nginx 的整个请求发送到另一个上游服务器的某个基本 URL,并删除servercookie。所有其他 cookie、HTTP 标头、URL 本身应保持原样,发送到上游并将响应传回我的客户端。

到目前为止我已经尝试过以下本指南在 nginx 论坛上。我的位置指令如下所示:

set $new_cookie $http_cookie;

if ($http_cookie ~ "(.*)(?:^|;)\s*server=[^;]+(.*)") {
  set $new_cookie $1$2;
}

location  ~ ^/d/application(.*)$ {
  # here we serve this from another container running locally, no proxy_pass
  resolver 127.0.0.11;
  proxy_pass         http://application:8080/d/application$1;
  proxy_redirect     off;
  proxy_set_header   Host $host;
}

location  ~ ^/d/(.*)$ {
  # any other URL NOT starting with /d/application we would like to proxy_pass to another backend and remove server cookie
  resolver 127.0.0.11;
  proxy_pass         https://anotherapp.mydomain.com/d/$1;
  proxy_redirect     off;
  proxy_set_header Cookie $new_cookie;
  proxy_set_header   Host $host;
}

问题。现在只需一个请求,/d/somethingelse日志如下所示:

proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 400 106 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 2020/04/17 09:56:45 [error] 9#0: *10 upstream sent too big header while reading response header from upstream, client: 10.18.6.15, server: , request: "GET /d/somethingelse HTTP/1.1", upstream: "https://10.18.16.1:443/d/somethingelse", host: "anotherapp.mydomain.com"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
proxy_1        | 10.18.6.15 - - [17/Apr/2020:09:56:45 -0400] "GET /d/somethingelse HTTP/1.1" 502 568 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"

更新:添加

      proxy_buffer_size          128k;
      proxy_buffers              4 256k;
      proxy_busy_buffers_size    256k;

proxy_pass到可以消除错误的位置too big header,但仍然有很多请求同时发送到上游,而浏览器只有一个请求

现在,我正努力将请求转发到上游主机。如果我注释掉proxy_set_header Cookie $new_cookie;cookie 正则表达式并设置$new_cookie,我仍然会看到许多对上游的请求。

添加proxy_ssl_verify off;到第二个location-仍然看到同样的问题。

答案1

我可以让它工作,但是我仍然不清楚为什么我需要两个完全相同的 if 来重写 cookie 的值。

因此,问题的根本原因是:我有太多请求,因为 cookie 没有被删除(我忘了如果不实际删除 cookie,我就无法调试它,server因为在这种情况下,如果 cookie 存在,nginxhttps://anotherapp.mydomain.com/d/会将该请求转发回给我server- 这一直在发生。

现在我还有以下问题:为什么只用一个 if 就不行

set $new_cookie $http_cookie;

if ($new_cookie ~ "(.*)(?:^|;)\s*server=[^;]+(.*)") {
  set $new_cookie $1$2;
}

进而

proxy_set_header   Cookie $new_cookie;

location块中。但它确实适用于两个完全相同的 if,例如:

set $new_cookie $http_cookie;

if ($new_cookie ~ "(.*)(?:^|;)\s*server=[^;]+(.*)") {
  set $new_cookie $1$2;
}

if ($new_cookie ~ "(.*)(?:^|;)\s*server=[^;]+(.*)") {
  set $new_cookie $1$2;
}

proxy_set_header   Cookie $new_cookie;

location街区里?

如果出现某种情况,日志如下所示:

proxy_1        | 2020/04/20 04:11:24 [notice] 10#0: *76 "(.*)(?:^|;)\s*server=[^;]+(.*)" matches "cookie1=111; canary=0; cookie2=222; cookie3=1; server=something; server_php=something; server_asp=null; cookie4=something; cookie5=something; XDEBUG_SESSION=PHPSTORM; cookie6=something; cookie7=something; cookie8=something

下面是两个 if 的情况(当它起作用时):

proxy_1        | 2020/04/20 04:14:27 [notice] 10#0: *1 "(.*)(?:^|;)\s*server=[^;]+(.*)" matches "cookie1=111; canary=0; cookie2=222; cookie3=1; server=something; server_php=something; server_asp=null; cookie4=something; cookie5=something; XDEBUG_SESSION=PHPSTORM; cookie6=something; cookie7=something; cookie8=something
proxy_1        | 2020/04/20 04:14:27 [notice] 10#0: *1 "(.*)(?:^|;)\s*server=[^;]+(.*)" matches "cookie1=111; canary=0; cookie2=222; cookie3=1; server=something; server_php=something; server_asp=null; cookie4=something; cookie5=something; XDEBUG_SESSION=PHPSTORM; cookie6=something; cookie7=something; cookie8=something

相关内容