在nginx中进行url编码或不编码时,cookies会发生变化

我在docker中使用以下nginx.conf运行openresty,以删除特殊的cookie,以避免将此cookie代理到上游。

user nobody nogroup;
pid /data/var/run/nginx.pid;
worker_processes 36;
worker_rlimit_nofile 51000;
events {
    worker_connections 50000;
    accept_mutex off;
}

error_log /data/log/nginx/default.error.log;

daemon off;

http {
    include mime.types;
    default_type text/html;

    log_format main '$http_x_req_id [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_response_time "$remote_addr" - "$host" "$http_log_traceid"';
    access_log /data/log/nginx/default.access.log main;

    lua_package_path "/data/etc/nginx/lua/?.lua;/data/etc/nginx/lua/?/init.lua;;";
    lua_package_cpath "/data/etc/nginx/lua/clib/?.so;;";

    upstream nginx-platform {
        server host.docker.internal:9090;
    }

    server {
        listen 80 default_server reuseport;
        server_name _;
        set $stripped_cookie $http_cookie;
        if ($http_cookie ~ "(.*)\s*_u_sh0=[^;]+;?(.*)$") {
            set $stripped_cookie $1$2;
        }
        location / {
            include proxy.conf;
            include proxy-headers.conf;
            proxy_pass http://nginx-platform;
            proxy_set_header Host $host;
            proxy_set_header Cookie $stripped_cookie;
        }

    }

}

但是当请求的URL被编码时,cookie也会被编码。如果我在终端中运行以下代码

 curl 'professor.in.home.com/221%23?a=22%22&name=test' -H "cookie: ff=gg; _u_sh0=48c1be09c567538fe327348b241aebbd0642f24c1c02cfd28506dd561414112d; dd=e++d; ee=ff;"

然后,在下面的命令中必要的信息,cookie也被编码了。

E.._/#@.@.\.......A...#.+(vf..8|P.......GET /221%23?a=22%22&name=test HTTP/1.1
X-Forwarded-For: 172.17.0.1
Connection: upgrade
Host: professor.in.home.com
Cookie: ff=gg%3B%20%20dd=e%2B%2Bd%3B%20ee=ff%3B
User-Agent: curl/7.54.0
Accept: */*

如何避免出现这种编码的cookie???

点赞
用户10081420
用户10081420

为了解决这个问题,我修改了 nginx.conf 文件如下:

            if ($http_cookie ~ "(.*)\s*_u_sh0=[^;]+;?(.*)$") {
                set $stripped_cookie $1$2;
            }

            if ($request_uri !~ "(^[^%]+\?.*%.*$)|(^[^%]+$)") {
                set_unescape_uri $stripped_cookie $stripped_cookie;
            }

虽然这样可以解决问题,但我不知道为什么?

2020-07-02 12:20:23