【问题标题】:Custom error page in Apache2 for 401Apache2 中针对 401 的自定义错误页面
【发布时间】:2011-05-19 11:34:09
【问题描述】:

这是 .htaccess 文件的相关部分:

AuthUserFile  /var/www/mywebsite/.htpasswd
AuthGroupFile /dev/null
AuthName  protected
AuthType Basic
Require valid-user

ErrorDocument 400 /var/www/errors/index.html
ErrorDocument 401 /var/www/errors/index.html
ErrorDocument 403 /var/www/errors/index.html
ErrorDocument 404 /var/www/errors/index.html
ErrorDocument 500 /var/www/errors/index.html

文档根目录设置为 /var/www/mywebsite/web,它位于许多虚拟主机上。我可以导航到 index.html 页面。

我看到的只是通用的 Apache 401 页面,任何想法。

编辑:这是我浏览器中的错误消息:

需要授权

此服务器无法验证您 有权访问该文档 请求。要么你提供 错误的凭据(例如,错误的 密码),或者您的浏览器没有 了解如何提供 需要凭据。

另外,401 授权 时遇到必需的错误 尝试使用 ErrorDocument 来 处理请求。阿帕奇/2.2.9 (Debian) PHP/5.2.6-1+lenny8 与 位于 www.dirbe.com 的 Suhosin 补丁服务器 80 端口

【问题讨论】:

    标签: apache apache2 custom-error-pages


    【解决方案1】:

    确保 apache 用户可以读取 /var/www/errors 并将其包含在您的 apache 配置中:

    <Directory /var/www/errors>
      Order allow,deny
      Allow from all
    </Directory>
    

    【讨论】:

    • 不走运。并且任何错误日志中都没有任何内容。我在错误页面上看到了这一点:“此外,在尝试使用 ErrorDocument 处理请求时遇到了 401 Authorization Required 错误。”
    • 尝试将您的 require auth 行放在 部分中。
    • 感谢您的提示,但无济于事。出现错误:
    • apache 文档说 ErrorDocument 可以是相对于文档根目录的路径,也可以是将通过重定向发送的外部 URL。由于您收到 Auth required 消息,您可能需要两个相邻的目录,一个位于 /errors,一个位于 /protected,其中 /protected 中的 .htaccess 将 /errors/401.html 作为 ErrorDocument 引用。
    • Sam 的最后一条评论解决了我的问题,所以我接受了他原来的回答。
    【解决方案2】:

    这个问题(以及答案和 cmets)帮助了我很多,非常感谢!

    我解决了一个稍微不同的方法,并想分享。在这种情况下,我们需要提供一个自定义的 401 错误文档,并且需要将根路径代理到后端应用程序。

    因此,例如,http://example.com 需要提供来自 http://internal-server:8080/ 的内容。此外,http://example.com 需要使用带有自定义 401 错误文档的 Basic Auth 进行保护。

    所以,我在 DocumentRoot 中创建了一个名为“error”的目录。这是来自虚拟主机的相关行:

        ErrorDocument 401 /error/error401.html
    
        # Grant access to html files under /error
    <Location "/error">
    Options -Indexes
    Order Deny,Allow
    Allow from all
    </Location>
    
        # restrict proxy using basic auth
    <Proxy *>
    Require valid-user
    AuthType basic
    AuthName "Basic Auth"
    AuthUserFile /etc/apache2/.htpasswd
    </Proxy>
    
        # Proxy everything except for /error
    ProxyRequests Off
    ProxyPass /error !
    ProxyPass / http://internal:8080/
    ProxyPassReverse / http://internal:8080/
    

    【讨论】:

    • 试过了,但在要求登录之前我得到了错误页面。有什么想法吗?
    【解决方案3】:

    ErrorDocument 采用绝对 URL 路径而不是文件路径。所以应该是:

    ErrorDocument 404 /error/error.html
    

    假设您的文档根目录下是一个 /error/error.html 文件。

    【讨论】:

    • 这应该是选择的答案。重要的是,“URL path”。不要把它误认为是 file 路径!
    • 还要确保此位置的文件可以像“普通”文档一样访问(例如,就像您的 DocumentRoot 中的 index.html 一样)。这是因为ErrorDocument 指令执行简单的重定向,不直接读取(文件或)位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多