NGINX根据标头的值设置cookie

时间:2016-10-19 12:08:54

标签: http cookies nginx header

我正在尝试让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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你得到了什么样的回应?如果您的回复中出现错误,则可能需要添加always标记,否则可能无法添加标题。

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

  

语法:add_header name value [always];

     

如果指定了always参数(1.7.5),则无论响应代码是什么,都将添加标题字段