NGINX - Redirect on auth failure

2019-07-06 00:52发布

问题:

I'm trying to find a way to redirect a user to a different location, if he provides invalid credentials, or no credentials at all.

My configuration looks like this:

 server {
    listen          9999;

    auth_basic      "Authentication Required";
    auth_basic_user_file    passwords;

This causes the user's browser to constantly prompt for credentials, until he cancels the prompt - which returns a 401 error.

I would like to redirect the user to a different page if the authentication failed (for whatever reason) - is this possible with NGINX?

回答1:

Working fine. Just make sure that your placeholder.html exists. Else it will through 404.

    server {
        listen 80;
        server_name testing.com;

        root /var/www/;

    location / {
        index index.html index.htm;
            auth_basic "Restricted";
            auth_basic_user_file /etc/nginx/.htpasswd;  
    }
    error_page 401 /placeholder.html;
    location = /placeholder.html {
                auth_basic off;
        }

}


标签: nginx