Reputation: 10950
How do I set a cookie and redirect right after an NGINX auth_basic succeeds on the same server?
It seems that some combination satisfy
, auth_basic
, or auth_request
is needed for this, but the documentation only covers the case where authentication is outsourced to some external server.
Thank you in advance.
Upvotes: 1
Views: 2093
Reputation: 6409
I feel like there should be a better way, but you can trick try_files
into doing this for you if you use an internal location.
location /cookie-drop {
auth_basic "Please authenticate";
auth_basic_user_file /your/htpasswd/file;
# Using null as a hardcoded nonexistent file
try_files null @cookie-drop;
}
location @cookie-drop {
internal;
add_header Set-Cookie "token=foo;Domain=example.com;Path=/";
return 302 https://$host/
}
If you put the return 302
inside the /cookie-drop
location, it will ignore the auth_basic
setting. The try_files
setting is the only thing I could find to redirect to an internal location like this. Maybe there's a better way though.
Upvotes: 2