Custom 401 page not asking for credentials for nginx

I have a part of my site protected by auth_basic

:

    location / {
            auth_basic "Authorization Required";
            auth_basic_user_file .htpasswd;
            index  app.php;
            try_files $uri @rewriteapp;
    }

      

For unauthorized users (those who click Cancel), I would like to display a custom error page. This is how I did it:

    error_page 401 = @error401;

    location @error401 {
                    rewrite ^ /error/401.html redirect;
    }

    location /error {
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public";
            index  app.php;
            try_files $uri @rewriteapp;
    }

      

I had to create an alias @ error401 so that I could specify a Redirect, because if I were to use an internal redirect, my application would process the original request (and give access to the actual requested page!)

So this works great, the page is displayed. The problem is that now nginx doesn't even ask users for credentials.

Is there a way to fix this? Or a better way to deal with this problem?

+3


source to share


2 answers


This is how I was able to accomplish what you are trying to do:



## Path must be prefixed with a /, i.e. /401.html, NOT 401.html
error_page 401 /401.html;

location ~ (401.html)$ {
    alias /usr/share/nginx/html/$1;
}

      

+3


source


You must add auth_basic off;



0


source







All Articles