NGINX 根据标头的值设置 cookie

2ac*_*ary 6 cookies header http nginx

我正在尝试让 NGINX 检查请求标头user_header_token是否存在。如果它不存在,则重定向到登录站点。如果存在,请使用标头的值设置 cookie。当前设置的 cookie 是空的,而不是$http_我试图将其设置为的变量。有没有人看到我在做什么阻止将此 cookie 设置为标头的值?

http {
    include /etc/nginx/mime.types;
    server {
        listen 80;
        location / {
            if ($http_user_header_token = "") {
                rewrite ^.*$ https://loginsite.com/;
                return 403;
            }

            add_header Set-Cookie user_header_token=$http_user_header_token;

            root /usr/src/ui/;
            index index.html;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Col*_*lin 4

你得到什么样的回应?如果您的响应中有错误,您可能需要添加标志always,否则可能无法添加标头。

http://nginx.org/en/docs/http/ngx_http_headers_module.html

语法:add_header 名称值 [always];

如果指定了always参数(1.7.5),则无论响应代码如何,都会添加标头字段