django-downloadview/docs/optimizations/index.txt

77 lines
2.2 KiB
Text

##################
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.
.. toctree::
:maxdepth: 2
nginx
Currently, only `nginx's X-Accel`_ is supported, but `contributions are
welcome`_!
*****************
How does it work?
*****************
The feature is inspired by `Django's TemplateResponse`_: the download views
return some :py:class:`django_downloadview.response.DownloadResponse` instance.
Such a response does not contain file data.
By default, at the end of Django's request/response handling, Django iterates
over the ``content`` attribute of the response. In a `DownloadResponse``,
this ``content`` attribute is a file wrapper.
It means that decorators and middlewares are given an opportunity to capture
the ``DownloadResponse`` before the content of the file is loaded into memory
As an example, :py:class:`django_downloadview.nginx.XAccelRedirectMiddleware`
replaces ``DownloadResponse`` intance by some
:py:class:`django_downloadview.nginx.XAccelRedirectResponse`.
*********
Configure
*********
Add `django_downloadview.DownloadDispatcherMiddleware` to `MIDDLEWARE_CLASSES`
in your Django settings.
Then register as many download middlewares as you wish in
`DOWNLOADVIEW_MIDDLEWARES`.
.. code:: python
DOWNLOADVIEW_MIDDLEWARES = (
('default',
'django_downloadview.nginx.XAccelRedirectMiddleware',
{'source_dir': MEDIA_ROOT, 'destination_url': '/proxied-download'}),
)
The first item is an identifier.
The second item is the import path of some download middleware factory
(typically a class).
The third item is a dictionary of keyword arguments passed to the middleware
factory.
.. rubric:: References
.. target-notes::
.. _`nginx's X-Accel`: http://wiki.nginx.org/X-accel
.. _`contributions are welcome`:
https://github.com/benoitbryon/django-downloadview/issues?labels=optimizations
.. _`Django's TemplateResponse`:
https://docs.djangoproject.com/en/1.5/ref/template-response/