【问题标题】:Apache throws 401 UnauthorizedApache 抛出 401 Unauthorized
【发布时间】:2014-12-17 22:19:07
【问题描述】:

这是我的VirtualHost 配置:

<VirtualHost *:80>

     ServerName example.com
     DocumentRoot /var/www/example.com/public

     <Directory /var/www/example.com/public>
             Options +FollowSymLinks
             AllowOverride All
             Require user user1 user2
     </Directory>

     <Location /error/401>
             Require all granted
     </Location>

     ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
     LogLevel warn
     CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined

     ErrorDocument 500 /error/500
     ErrorDocument 404 /error/404
     ErrorDocument 401 /error/401

</VirtualHost>

但是当我故意验证失败时(或者当我直接打开 /error/401 时),我还是得到了这个:

Unauthorized

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

Additionally, a 401 Unauthorized error was encountered while trying to use an ErrorDocument to handle the request.

我做错了什么?根据下面链接的文档,Location 应该在Directory 之后处理,所以这应该可以工作。

http://httpd.apache.org/docs/current/sections.html#merging

编辑:

很明显,这是有问题的部分:

Additionally, a 401 Unauthorized error was encountered while trying to use an ErrorDocument to handle the request.

【问题讨论】:

  • 也许我不明白问题出在哪里,但 Apache 似乎工作正常。 “当我故意验证失败时”应该带你到 401 页面,当你尝试浏览到“/error/401”时,它也应该带你到 401 页面。那么,当您输入有效的用户名和密码时,它是否有效?
  • 这是有问题的部分:“此外,在尝试使用 ErrorDocument 处理请求时遇到了 401 Unauthorized 错误。”
  • 您使用的是什么版本的 Apache? Apache 2.2 和 2.4 在访问控制方面有一些区别:httpd.apache.org/docs/2.4/upgrading.html
  • 我使用的是 2.4。你可以在我的配置中看到我正在使用Require all granted。我认为这在 2.2 中甚至不是有效的语法,所以我认为我根本无法使用它来启动 2.2。

标签: apache location virtualhost unauthorized errordocument


【解决方案1】:

我遇到了一个非常相似的问题。关键的实际问题是,在站点的 DocumentRoot 级别验证失败后,无法访问位于 DocumentRoot 下方的自定义错误页面(即 /error/401),因此会发出默认的 Apache 错误消息。

我采用的解决方案是将 html 合并到 ErrorDocument 401 指令中。例如

ErrorDocument 401 "INSERT MARKUP HERE"

(请注意标记周围的引号是必不可少的)。

当然,这是一种非常有效的解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多