自定义401页面不提示nginx上的凭据

时间:2013-01-24 12:07:58

标签: nginx basic-authentication http-status-code-401

我的网站的一部分受auth_basic保护:

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

对于未经授权的用户(点击“取消”的用户),我想显示自定义错误页面。这是我如何做到的:

    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;
    }

我必须创建@ error401别名,所以我可以指定“Redirect”,因为如果我使用内部重定向,我的应用程序将处理原始请求(并提供对实际请求页面的访问权限!)

这样工作正常,页面显示。 问题是现在nginx甚至没有要求用户提供凭据。

有什么方法可以解决这个问题吗?或者更好的方法来处理这个问题?

2 个答案:

答案 0 :(得分:3)

这就是我能够完成你想要做的事情:

## 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;
}

答案 1 :(得分:0)

您应该添加auth_basic off;

相关问题