Optimize streaming

Some reverse proxies allow applications to delegate actual download to the proxy:

  • with Django, manage permissions, generate files…
  • let the reverse proxy serve the file.

As a result, you get increased performance: reverse proxies are more efficient than Django at serving static files.

Supported features grid

Supported features depend on backend. Given the file you want to stream, the backend may or may not be able to handle it:

View / File Nginx Apache Lighttpd
PathDownloadView Yes, local filesystem. Yes, local filesystem. Yes, local filesystem.
StorageDownloadView Yes, local and remote. Yes, local filesystem. Yes, local filesystem.
ObjectDownloadView Yes, local and remote. Yes, local filesystem. Yes, local filesystem.
HTTPDownloadView Yes. No. No.
VirtualDownloadView No. No. No.

As an example, Nginx X-Accel handles URL for internal redirects, so it can manage HTTPFile; whereas Apache X-Sendfile handles absolute path, so it can only deal with files on local filesystem.

There are currently no optimizations to stream in-memory files, since they only live on Django side, i.e. they do not persist after Django returned a response. Note: there is a feature request about “local cache” for streamed files [2].

How does it work?

View return some DownloadResponse instance, which itself carries a file wrapper.

django-downloadview provides response middlewares and decorators that are able to capture DownloadResponse instances and convert them to ProxiedDownloadResponse.

The ProxiedDownloadResponse is specific to the reverse-proxy (backend): it tells the reverse proxy to stream some resource.


The feature is inspired by Django's TemplateResponse

Available optimizations

Here are optimizations builtin django_downloadview:


If you need support for additional optimizations, tell us [1]!

Notes & references