2achary 2achary - 11 months ago 216
HTTP Question

NGINX set cookie based on value of a header

I'm trying to get NGINX to check if a request header

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

Answer Source

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.


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