Nginx Not Correctly Redirecting Users To Custom Page

12 Views Asked by At

I’m having an issue with nginx where I make an authentication request to a backend server. That works fine however it’s the part of catching any errors from the backend server where the issue is. For example, if the server returns a 401 status code, I want it to redirect the user to the login page (/login). However it’s not doing that and instead just returning the default nginx error page with whatever status code the auth server provided.

Here is my configuration:

server {
listen 80;
server_name testing.my.lifplatforms.com;
root /var/www/testing.my.lifplatforms.com;

index index.html index.htm index.nginx-debian.html;

location / {
    auth_request /verify_cookies;
    auth_request_set $auth_status $upstream_status;

    # Redirect to /login for unauthorized users
    error_page 401 403 =302 /login;

    # Serve requested files or fall back to index.html
    try_files $uri /index.html;
}

location = /verify_cookies {
    internal;
    proxy_pass http://localhost:8002/auth/verify_token;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
}

As you can see I’ve configured an error page for catching authentication errors but it’s doesn’t seem to work.

0

There are 0 best solutions below