Wednesday, April 20, 2011

Serving static files with mod_wsgi and Django

I have a django application using mod_python, fairly typical configuration except that media files are being served by a (I know, not recommended) 'media' directory in the document root. I would like to test and maybe deploy with mod_wsgi but I cannot figure out how to create something simple to serve static files. mod_python allows the use of Apache directives like:

<Location '/'>

<Location '/media'>
    SetHandler None

The django docs seem to point to the second block above as the correct way to make a similar exception for mod_wsgi, but in my tests everything below root is still being sent to the wsgi app. Is there a good way set a static media directory with mod_wsgi, or is what I am trying to do intentionally unsupported for compelling technical reasons? Answers that point to entirely different approaches are welcome.

From stackoverflow
  • I run a a dozen or so Django sites on the same server and here's how I configure the media URL's.

    Each VirtualHost has the following configuration:

    Alias /media /path/to/media/
    <Directory /path/to/media>
        Include /etc/apache2/vhosts.d/media.include

    This way I can make any changes to the media handling in one file.

    Then, my media.include file looks like this:

    Order allow,deny
    Allow from all
    SetHandler None
    FileETag none
    Options FollowSymLinks
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType image/gif "access plus 30 days"
        ExpiresByType image/jpg "access plus 30 days"
        ExpiresByType image/png "access plus 30 days"
        ExpiresByType image/jpeg "access plus 30 days"
        ExpiresByType text/css "access plus 30 days"
        ExpiresByType application/x-javascript "modification plus 2 years"
    <IfModule mod_headers.c>
        Header append Vary Accept-Encoding
    AddOutputFilterByType DEFLATE text/html text/css text/plain

    This has worked very well for me, and gets an A grade from YSlow (also see Jeff Atwood on YSlow).

    Also note, for the root dir I use the following configuration:

    WSGIScriptAlias / /path/to/app.wsgi
    <Directory /path/to>
        Options +ExecCGI
        Allow from all

    ... which should be after the Alias /media in your configuration file (because Apache looks at the aliases in order)

    bvmou : Thanks! This works like a charm, thanks for the excellent answer.
  • The mod_wsgi documentation explains how to setup static files which appear at a URL underneath that which the WSGI application is mounted at. See:

    Note that 'Options +ExecCGI' is not need when using WSGIScriptAlias directive to mount the WSGI application. The 'ExecCGI' option is only required when using AddHandler to mount applications as resources.

    bvmou : Graham Dumpleton! Thanks for mod_wsgi! Actually I think the order was the problem originally, I did not use the cgi directive. Your module has enabled me to do a ton of things, I have really developed a mod_wsgi habit since asking this question.


Post a Comment