|
| 1 | +.. _howto-response-sendfile: |
| 2 | + |
| 3 | +================================================= |
| 4 | +How to serve large files via HttpResponseSendFile |
| 5 | +================================================= |
| 6 | + |
| 7 | +.. module:: django.http |
| 8 | + :synopsis: Serving of large files via handler-specific mechanisms. |
| 9 | + |
| 10 | +There are cases when you may wish to serve files in a response, but only want |
| 11 | +to provide access to them through your application. As discussed in :ref:`the how-to for serving static |
| 12 | +files <howto-static-files>`, Django is not tuned at all for this sort of serving. Thus, |
| 13 | +:func:`~django.http.HttpResponseSendFile` provides access to handler-specific mechanisms and server headers to |
| 14 | +shunt this processing to more efficient methods. |
| 15 | + |
| 16 | +Server configurations that have been verified to support efficient file transfers |
| 17 | +through the use of these headers include Apache_, lighttpd_, Cherokee_, and nginx_. |
| 18 | +A brief overview of the use of these will be described below. |
| 19 | + |
| 20 | +In the event that :func:`~django.http.HttpResponseSendFile` is used without setting a handler-specific |
| 21 | +mechanism, an inefficient fallback (using :class:`~django.core.servers.basehttp.FileWrapper`). |
| 22 | +This will work but you will want to consider using one of the options below for |
| 23 | +greater efficiency. |
| 24 | + |
| 25 | +.. _Apache: http://httpd.apache.org/ |
| 26 | +.. _lighttpd: http://www.lighttpd.net/ |
| 27 | +.. _Cherokee: http://www.cherokee-project.com/ |
| 28 | +.. _nginx: http://www.nginx.net |
| 29 | + |
| 30 | +.. seealso:: |
| 31 | + |
| 32 | + See :ref:`the how-to for serving static files <howto-static-files>` for serving static files |
| 33 | + that do not require protection. |
| 34 | + |
| 35 | +Disclaimer |
| 36 | +========== |
| 37 | + |
| 38 | +Using this method is **insecure**. You should be very careful to restrict the |
| 39 | +filenames that reach ``HttpResponseSendFile``, or you have a gaping hole (read-only) |
| 40 | +into your filesystem. |
| 41 | + |
| 42 | +General use |
| 43 | +=========== |
| 44 | + |
| 45 | +Instead of returning an :func:`HttpResponse` in a view, use :func:`~django.http.HttpResponseSendFile`, |
| 46 | +like so:: |
| 47 | + |
| 48 | + def send_protected(request): |
| 49 | + return HttpResponseSendFile("/protected/safe.zip") |
| 50 | + |
| 51 | +How the server treats the path varies. For example, on nginx, the root of the paths |
| 52 | +sent through :func:`~django.http.HttpResponseSendFile` is defined inside its configuration file. However, |
| 53 | +in most other instances it is treated as the root of the server's file system. |
| 54 | + |
| 55 | +The header used is defined by the setting :setting:`HTTPRESPONSE_SENDFILE_HEADER`. If it is |
| 56 | +left as a default, the fallback method will be used. Otherwise, it should be set as a |
| 57 | +string containing the header used by the server. |
| 58 | + |
| 59 | + |
| 60 | +How to use HttpResponseSendFile with Apache |
| 61 | +=========================================== |
| 62 | + |
| 63 | +Apache supports efficient file transfer using ``mod_xsendfile_``. Once this module is in |
| 64 | +place, add the following line to ``settings.py``:: |
| 65 | + |
| 66 | + HTTPRESPONSE_SENDFILE_HEADER = "X-Sendfile" |
| 67 | + |
| 68 | +This will inform :func:`~django.http.HttpResponseSendFile` that it should allow the server to handle serving |
| 69 | +the file passed to it. |
| 70 | + |
| 71 | +.. _mod_xsendfile: http://tn123.ath.cx/mod_xsendfile/ |
| 72 | + |
| 73 | +**Note:** This works using both ``mod_wsgi`` and ``mod_python``. |
| 74 | + |
| 75 | +How to use HttpResponseSendFile with lighttpd |
| 76 | +============================================= |
| 77 | + |
| 78 | +lighttpd_ supports the ``X-Sendfile`` header natively (under ``FastCGI``). To enable it, simply |
| 79 | +add the line:: |
| 80 | + |
| 81 | + "allow-x-send-file" => "enable" |
| 82 | + |
| 83 | +... in the ``main`` section under the ``check-local`` line. |
| 84 | + |
| 85 | +For further information about lighttpd, see `documentation of lighttpd's configuration options`_. |
| 86 | + |
| 87 | +.. _lighttpd: http://www.lighttpd.net/ |
| 88 | +.. _`documentation of lighttpd's configuration options`: http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions |
| 89 | + |
| 90 | +Add the following line to ``settings.py``:: |
| 91 | + |
| 92 | + HTTPRESPONSE_SENDFILE_HEADER = "X-Sendfile" |
| 93 | + |
| 94 | + |
| 95 | +How to use HttpResponseSendFile with Cherokee |
| 96 | +============================================= |
| 97 | + |
| 98 | +``Cherokee`` supports the ``X-Sendfile`` header natively (under FastCGI and SCGI). To enable it, |
| 99 | +use the administration interface. Under Common CGI options, simply enable the |
| 100 | +``Allow X-Sendfile`` option. For more info, visit the `cookbook for Django support in |
| 101 | +Cherokee`_. |
| 102 | + |
| 103 | +.. _Cherokee: http://www.cherokee-project.com/ |
| 104 | +.. _`cookbook for Django support in Cherokee`: http://www.cherokee-project.com/doc/cookbook_django.html |
| 105 | + |
| 106 | +Add the following line to ``settings.py``:: |
| 107 | + |
| 108 | + HTTPRESPONSE_SENDFILE_HEADER = "X-Sendfile" |
| 109 | + |
| 110 | +Then, follow the directions under General Use, above. |
| 111 | + |
| 112 | +**Note:** Though the documentation for Cherokee states that it supports the ``X-Accel-Redirect`` |
| 113 | +header, it is actually just an alias for ``X-Sendfile``. Additionally, the support for ``X-Accel-Redirect`` |
| 114 | +was non-functional until after at least one release after its inclusion. Therefore, there is no |
| 115 | +good reason to use ``X-Accel-Redirect`` header with Cherokee, though it is technically supported. |
| 116 | + |
| 117 | +How to use HttpResponseSendFile with nginx |
| 118 | +========================================== |
| 119 | + |
| 120 | +nginx_ uses a different header, ``X-Accel-Redirect``, that behaves slightly different than the |
| 121 | +``X-Sendfile`` used on other server configurations. |
| 122 | + |
| 123 | +To enable its use in nginx_, follow the directions in `nginx's documentation for X-Accel-Redirect`_. |
| 124 | + |
| 125 | +.. _`nginx's documentation for X-Accel-Redirect`: http://wiki.nginx.org/NginxXSendfile |
| 126 | +.. _nginx: http://www.nginx.net |
| 127 | + |
| 128 | +Add the following line to ``settings.py``:: |
| 129 | + |
| 130 | + HTTPRESPONSE_SENDFILE_HEADER = "X-Accel-Redirect" |
| 131 | + |
0 commit comments