Apache2 中 401 的自定义错误页面

发布于 2024-10-05 22:47:57 字数 824 浏览 0 评论 0原文

以下是 .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 与 Suhosin-补丁服务器位于 www.dirbe.com 端口80

Here's the relevant part of the .htaccess file:

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

Docuement root is set to /var/www/mywebsite/web, it's on of many vhosts. I can navigate to the index.html page.

All I'm seeing is the generic Apache 401 page, any thoughts.

EDIT: This is the error message in my browser:

Authorization Required

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 Authorization
Required error was encountered while
trying to use an ErrorDocument to
handle the request. Apache/2.2.9
(Debian) PHP/5.2.6-1+lenny8 with
Suhosin-Patch Server at www.dirbe.com
Port 80

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我乃一代侩神 2024-10-12 22:47:57

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

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

Make sure that /var/www/errors is readable by the apache user and include this in your apache configuration:

<Directory /var/www/errors>
  Order allow,deny
  Allow from all
</Directory>
回忆躺在深渊里 2024-10-12 22:47:57

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

ErrorDocument 404 /error/error.html

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

ErrorDocument takes in a absolute URL path instead of a file path. So it should be:

ErrorDocument 404 /error/error.html

Assuming under your document root is a /error/error.html file.

不可一世的女人 2024-10-12 22:47:57

这个问题(以及答案和评论)对我很有帮助,非常感谢!

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

因此,例如,http://example.com 需要提供来自 http://internal-server:8080/。此外,http://example.com 需要使用带有自定义 401 错误文档的基本身份验证进行保护。

因此,我在 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/

This question (and answers and comments) helped me a bunch, thanks much!

I solved a slightly different way, and wanted to share. In this case, we needed to provide a custom 401 error document and the root path needed to be proxied to a backend app.

So, for example, http://example.com needed to serve content from http://internal-server:8080/. Also, http://example.com needed to be protected using Basic Auth with a custom 401 error document.

So, I created a directory named "error" in the DocumentRoot. Here's the relevant lines from the vhost:

    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/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文