Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX set cookie based on value of a header

I'm trying to get NGINX to check if a request header user_header_token is present. If it is not present, redirect to the login site. If it is present, set a cookie with the header's value. The cookie is empty when it is set currently instead of the $http_ variable I'm trying to set it to. Does anyone see what I'm doing that's preventing this this cookie from being set to the header's value?

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;
        }
    }
}
like image 273
2achary Avatar asked Oct 19 '16 12:10

2achary


1 Answers

What kind of response are you getting? If there is an error in your response, you may need to add the always flag or the header may not be added.

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

Syntax: add_header name value [always];

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code

like image 83
Colin Avatar answered Oct 14 '22 13:10

Colin