10

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 '/'>
    SetHandler MyApplication.xyz.....
</Location>

<Location '/media'>
    SetHandler None
</Location>

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.

unmounted
  • 33,530
  • 16
  • 61
  • 61

2 Answers2

18

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
</Directory>

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>

<IfModule mod_headers.c>
    Header append Vary Accept-Encoding
</IfModule>

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
</Directory>

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

Van Gale
  • 43,536
  • 9
  • 71
  • 81
  • For your root dir: It's super belt-and-braces, but from a security POV, i would put a section inside your and move your "Allow from all" into there. – Cheekysoft Feb 03 '14 at 16:27
13

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:

http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#Hosting_Of_Static_Files

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.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • 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. – unmounted Jun 25 '09 at 00:01
  • 1
    Double thanks for the documentation. Best docs I've seen in a long time. Wish MORE people would read them!!!! – htmldrum Dec 17 '12 at 09:19