Serving static files with mod_wsgi and Django
Solution 1
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.
Solution 2
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)
Erusa Chan
js, python, maps, visualization, data. github.com/bmount
Updated on June 18, 2022Comments
-
Erusa Chan almost 2 years
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.
-
htmldrum over 11 yearsDouble thanks for the documentation. Best docs I've seen in a long time. Wish MORE people would read them!!!!
-
Cheekysoft over 10 yearsFor your root dir: It's super belt-and-braces, but from a security POV, i would put a <Files wsgi.py> section inside your <Directory> and move your "Allow from all" into there.