I have an upstream server handling the login of our website. On a successful login I want to redirect the user to the secure part of the site. On a failure to login I want to redirect the user to the login form.
The upstream server returns a 200 OK
on a successful login and a 401 Unauthorized
on a failed login.
This is the relevant part of my configuration:
{
error_page 401 = @error401
location @error401 {
return 302 /login.html # this page holds the login form
}
location = /login { # this is the POST target of the login form
proxy_pass http://localhost:8080;
proxy_intercept_errors on;
return 302 /secure/; # without this line, failures work. With it failed logins (401 upstream response) still get 302 redirected
}
}
This setup works when succeeding to login. The client is redirect with a 302. This does not work when failing to login. The upstream server returns 401 and I expected that the error_page
would then kick in. But I still get the 302. If I remove the return 302 /secure/
line the redirect to the login page works. So it seems I can have either one but not both.
Bonus question; I doubt the way I handle the error_page
with that named location is The Way. Am I correct in doing it like this?
edit: Turns out having a return
in the location
block makes Nginx not use the proxy_pass
at all. So it makes sense the error page is not hit. The problem on how to do this, however, remains.