【问题标题】:Why isn't my custom Apache error page showing?为什么我的自定义 Apache 错误页面没有显示?
【发布时间】:2014-08-08 16:32:13
【问题描述】:

我有一个在 Apache2 下运行的 mod_wsgi 应用程序,每当出现未捕获的异常时,都会显示默认的 Apache HTTP 500 错误页面,尽管我的 Apache 配置中有 ErrorDocument 500 /500.html

是否有一些 mod_wsgi 指令可以用来显示我的自定义错误页面?我不是在寻找可以放在我的 mod_wsgi 脚本中的东西,因为如果脚本中有错误,那将无济于事。

编辑2:当使用下面的配置浏览到/cgi-bin/test.wsgi会成功显示自定义错误页面,但是浏览到/test会显示默认的Apache错误页面。

编辑:这是一个小示例配置,它给了我同样的问题:

/etc/apache2/sites-enabled/test

<VirtualHost *:80>
    ServerName foo.example.com

    DocumentRoot /path/to/docroot/

    <Directory /path/to/docroot/>
        Order allow,deny
        Allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /path/to/docroot/cgi-bin/

    <Directory /path/to/docroot/cgi-bin/>
        Options +ExecCGI
        AddHandler wsgi-script .wsgi
        Order allow,deny
        Allow from all
    </Directory>

    ErrorDocument 500 /500.html

    WSGIDaemonProcess test user=www-data group=www-data processes=4 threads=1 display-name=%{GROUP}
    WSGIProcessGroup test
</VirtualHost>

/path/to/docroot/.htaccess

RewriteEngine on
RewriteBase /

RewriteRule ^test/?$    /cgi-bin/test.wsgi/    [L,QSA]

/path/to/docroot/500.html

<html><body>This be 500.</body></html>

/path/to/docroot/cgi-bin/test.wsgi

hi

【问题讨论】:

    标签: apache mod-wsgi


    【解决方案1】:

    URL /500.html 与所使用的 Apache 配置映射到什么?

    如果它没有映射到有效的 URL,因此尝试针对它发出子请求会生成 404,然后 Apache 将返回一个通用的 500 响应。

    查看 /500.html 访问的访问日志,看看它是否指示 404。

    【讨论】:

    • 不,任何日志中都没有 404,500.html 位于DocumentRoot 中,ErrorDocument 之前指定。我尝试将 ErrorDocument 更改为绝对 URL,但这也不起作用。
    • 但是如果你有 WSGIScriptAlias for / 那么它会覆盖并隐藏 DocumentRoot。您需要使用别名 /500.html /some/path/foo.html
    • 我没有使用WSGIScriptAlias,直接在文件中添加Alias也不起作用。
    • 那么您是如何配置 mod_wsgi 来托管您的 WSGI 应用程序的呢?
    • WSGIDaemonProcessAddHandler wsgi-script.htaccess
    【解决方案2】:

    默认情况下,WSGI 应用程序完全控制请求响应周期,因此任何 Apache 错误处理程序都将被绕过。

    在 Apache 设置生效之前,您可能需要一个 WSGIErrorOverride On 指令。我自己还没有尝试过,但是在为一个操作团队追踪类似目标的建议时(当 Django 应用程序给出 500 时,让 Apache 返回它自己的静态错误页面)我通过How to return an apache error page from a wsgi app? 遇到了这个问题,并且 -相反的目标——How to configure Apache with mod_wsgi so that error messages come from the application?

    此功能被添加到 mod_wsgi 时的 original description 是:

    在 mod_proxy 的情况下,存在 ProxyErrorOverride 指令, 这允许您说您想要后端生成的错误页面 应用程序在适当时被忽略,并允许 Apache 生成 通过其正常的 ErrorDocument 指令获取内容。

    应该实现一个等效的 WSGIErrorOverride 指令以允许 WSGI 应用程序更好地安装到 WSGI 所在的大型站点中 应用程序不是唯一的内容来源,而只是一个贡献者。

    所以听起来确实像我们所追求的。请注意,这是在 mod_wsgi 3.0 及更高版本中添加的,希望这些版本已经发布了足够长的时间以供普遍使用。

    【讨论】:

    • 请记住,WSGIErrorOverride 仅在使用 mod_wsgi 守护程序模式时才有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2014-02-28
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多