Django nginx sendfile example
Use nginx sendfile (X-Accel-Redirect) function to serve files but pass traffic through django. Can be used to serve media files only to logged-in users.
- django
- media
- sendfile
- nginx
Use nginx sendfile (X-Accel-Redirect) function to serve files but pass traffic through django. Can be used to serve media files only to logged-in users.
An HttpResponse for giving the user a file download, taking advantage of X-Sendfile if it's available, using FileWrapper if not. Usage: HttpResponseSendfile('/path/to/file.ext') To bypass the fallback: HttpResponseSendfile('/path/to/file.ext', fallback=False) This has been tested working with Lighttpd 1.4.28's mod_fastcgi.
This snippet demonstrates how you can send a file (or file-like object) through Django without having to load the whole thing into memory. The FileWrapper will turn the file-like object into an iterator for chunks of 8KB. This is a full working example. Start a new app, save this snippet as views.py, and add the views to your URLconf. The send_file view will serve the source code of this file as a plaintext document, and the send_zipfile view will generate a Zip file with 10 copies of it. Use this solution for dynamic content only, or if you need password protection with Django's user accounts. Remember that you should serve static files directly through your web server, not through Django: http://www.djangoproject.com/documentation/modpython/#serving-media-files
3 snippets posted so far.