Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 '/'>
    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.

like image 867
unmounted Avatar asked Apr 08 '09 22:04

unmounted


People also ask

Can Django serve static files in production?

Static Files in Development Mode During development, as long as you have DEBUG set to TRUE and you're using the staticfiles app, you can serve up static files using Django's development server. You don't even need to run the collecstatic command.

Can uWSGI serve static files?

You can directly store a static file in the uWSGI cache during startup using the option --load-file-in-cache <filename> (you can specify it multiple times). The content of the file will be stored under the key <filename>.

How do you serve a static file?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets. For more information on the options argument, see express.static.


1 Answers

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)

like image 192
Van Gale Avatar answered Oct 20 '22 11:10

Van Gale