【问题标题】:cookies changes when url encoded or not encoded in nginx当 url 在 nginx 中编码或未编码时,cookie 会发生变化
【发布时间】:2020-10-23 01:03:53
【问题描述】:

我在 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;"

然后,我grep下面的必要信息,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???

【问题讨论】:

    标签: nginx openresty


    【解决方案1】:

    为了解决这个问题,我改变了nginx.conf,如下所示。

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

    虽然这样可行,但我不知道为什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-20
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2020-09-26
      • 1970-01-01
      相关资源
      最近更新 更多