简体   繁体   中英

Why isn't my custom Apache error page showing?

I have a mod_wsgi application running under Apache2, and whenever there is an uncaught exception, the default Apache HTTP 500 error page is shown despite the fact that I have ErrorDocument 500 /500.html in my Apache config.

Is there some mod_wsgi directive I can use to display my custom error page? I'm not looking for something I can put in my mod_wsgi script as that won't help if there is an error in the script.

Edit 2: When using the below configuration browsing to /cgi-bin/test.wsgi displays the custom error page successfully, but browsing to /test displays the default Apache one.

Edit: Here is a small sample configuration that gives me the same problem:

/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

What does the URL /500.html map to with the Apache configuration used?

If it doesn't map to a valid URL and so an attempt to issue a sub request against it generates a 404, then Apache will thence return a generic 500 response.

Have a look in your access log for the /500.html access and see if it indicates a 404.

By default the WSGI application takes complete control over the request and response cycle, so any Apache error handlers will be bypassed.

You will likely need a WSGIErrorOverride On directive before the Apache settings work. I have not tried this myself yet, but in tracking down advice on a similar goal for an ops team (having Apache return its own static error page when a Django app gives a 500) I came across this via How to return an apache error page from a wsgi app? and — with the opposite goal — How to configure Apache with mod_wsgi so that error messages come from the application? .

The original description for this feature as it was being added to mod_wsgi was:

In the case of mod_proxy there exists the ProxyErrorOverride directive, which allows one to say that you want error pages generated by the back end application to be ignored when appropriate, and allow Apache to generate the content through its normal ErrorDocument directives.

An equivalent WSGIErrorOverride directive should be implemented to allow WSGI applications to be better fitted into a large site where the WSGI application isn't the only source of content, but just one contributor.

So it does sound like what we are after. Note that this was added to mod_wsgi version 3.0 and later, which have hopefully been out long enough to be in common use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM