Wieder was gelernt

Apache /server-status vs WSGI

Tags: apache wsgi python monitoring
2020-09-04

When writing web applications, it is often necessary (or at least more aesthetic) to make the home page part of the application. Using mod_wsgi, this looks like this:

WSGIScriptAlias / /var/www/mysite/app/myapp/myapp/wsgi.py

Static files can be exempted via aliases:

Alias /robots.txt /var/www/mysite/htdocs/robots.txt
Alias /favicon.ico /var/www/mysite/htdocs/favicon.ico
Alias /static/ /var/www/mysite/htdocs/static/

If you want to enable mod_status the normal approach doesn’t work:

<Location "/server-status">
    SetHandler server-status·
    Require ip 192.0.2.0/24
</Location>

/server-status will be passed to WSGI handler instead of the server-status handler, no matter how you arrange the directives.

However, there is a way to make it work:

The WSGIScriptAlias documentation mentions that this directive is “essentially equivalent” to:

Alias / /var/www/mysite/app/myapp/myapp/wsgi.py
<Location "/">
    SetHandler wsgi-script
    Options +ExecCGI
</Location>

So, by rewriting it in this way we can move the Alias and the Location directives around to adjust priorities:

<Location "/">
    SetHandler wsgi-script
    Options +ExecCGI
</Location>

<Location "/server-status">
    SetHandler server-status·
    Require ip 192.0.2.0/24
</Location>

<Location "/static">
    SetHandler default-handler
</Location>

Alias /static/ /var/www/mysite/htdocs/static/
Alias /server-status /var/www/mysite/htdocs/fake.server-status
Alias / /var/www/mysite/app/myapp/myapp/wsgi.py

Note that with Location directives, the last one wins while with Alias directives the first one wins. So <Location "/server-status"> has to be after <Location "/">, but Alias /server-status ... has to be before Alias / ....

We also explicitely reset the handler for /static. This can also be done for other directories which should be served statically (e.g. /.well-known)

If static files in the root directory are needed (e.g. robots.txt or favicon.ico), use RewriteRule to send a redirect.