NGINX set cookie based on value of a header

19,801

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

Share:
19,801
2achary
Author by

2achary

Updated on June 13, 2022

Comments

  • 2achary
    2achary almost 2 years

    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;
            }
        }
    }