Debian Codegraph @

docs/optimizations/lighttpd.txt

raw
1 ########
2 Lighttpd
3 ########
4
5 If you serve Django behind `Lighttpd`, then you can delegate the file streaming
6 to `Lighttpd` and get increased performance:
7
8 * lower resources used by Python/Django workers ;
9 * faster download.
10
11 See `Lighttpd X-Sendfile documentation`_ for details.
12
13 .. note::
14
15 Currently, `django_downloadview` supports ``X-Sendfile``, but not
16 ``X-Sendfile2``. If you need ``X-Sendfile2`` or know how to handle it,
17 check `X-Sendfile2 feature request on django_downloadview's bugtracker`_.
18
19
20 *****************
21 Known limitations
22 *****************
23
24 * Lighttpd needs access to the resource by path on local filesystem.
25 * Thus only files that live on local filesystem can be streamed by Lighttpd.
26
27
28 ************
29 Given a view
30 ************
31
32 Let's consider the following view:
33
34 .. literalinclude:: /../demo/demoproject/lighttpd/views.py
35 :language: python
36 :lines: 1-6, 8-17
37
38 What is important here is that the files will have an ``url`` property
39 implemented by storage. Let's setup an optimization rule based on that URL.
40
41 .. note::
42
43 It is generally easier to setup rules based on URL rather than based on
44 name in filesystem. This is because path is generally relative to storage,
45 whereas URL usually contains some storage identifier, i.e. it is easier to
46 target a specific location by URL rather than by filesystem name.
47
48
49 ***************************
50 Setup XSendfile middlewares
51 ***************************
52
53 Make sure ``django_downloadview.SmartDownloadMiddleware`` is in
54 ``MIDDLEWARE`` of your `Django` settings.
55
56 Example:
57
58 .. literalinclude:: /../demo/demoproject/settings.py
59 :language: python
60 :lines: 63-70
61
62 Then set ``django_downloadview.lighttpd.XSendfileMiddleware`` as
63 ``DOWNLOADVIEW_BACKEND``:
64
65 .. literalinclude:: /../demo/demoproject/settings.py
66 :language: python
67 :lines: 80
68
69 Then register as many ``DOWNLOADVIEW_RULES`` as you wish:
70
71 .. literalinclude:: /../demo/demoproject/settings.py
72 :language: python
73 :lines: 84, 101-110
74
75 Each item in ``DOWNLOADVIEW_RULES`` is a dictionary of keyword arguments passed
76 to the middleware factory. In the example above, we capture responses by
77 ``source_url`` and convert them to internal redirects to ``destination_dir``.
78
79 .. autoclass:: django_downloadview.lighttpd.middlewares.XSendfileMiddleware
80 :members:
81 :inherited-members:
82 :undoc-members:
83 :show-inheritance:
84 :member-order: bysource
85
86
87 ****************************************
88 Per-view setup with x_sendfile decorator
89 ****************************************
90
91 Middlewares should be enough for most use cases, but you may want per-view
92 configuration. For `Lighttpd`, there is ``x_sendfile``:
93
94 .. autofunction:: django_downloadview.lighttpd.decorators.x_sendfile
95
96 As an example:
97
98 .. literalinclude:: /../demo/demoproject/lighttpd/views.py
99 :language: python
100 :lines: 1-7, 18-
101
102
103 *************************************
104 Test responses with assert_x_sendfile
105 *************************************
106
107 Use :func:`~django_downloadview.lighttpd.decorators.assert_x_sendfile`
108 function as a shortcut in your tests.
109
110 .. literalinclude:: /../demo/demoproject/lighttpd/tests.py
111 :language: python
112
113 .. autofunction:: django_downloadview.lighttpd.tests.assert_x_sendfile
114
115 The tests above assert the `Django` part is OK. Now let's configure `Lighttpd`.
116
117
118 **************
119 Setup Lighttpd
120 **************
121
122 See `Lighttpd X-Sendfile documentation`_ for details.
123
124
125 *********************************************
126 Assert everything goes fine with healthchecks
127 *********************************************
128
129 :doc:`Healthchecks </healthchecks>` are the best way to check the complete
130 setup.
131
132
133 .. rubric:: References
134
135 .. target-notes::
136
137 .. _`Lighttpd X-Sendfile documentation`:
138 http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file
139 .. _`X-Sendfile2 feature request on django_downloadview's bugtracker`:
140 https://github.com/jazzband/django-downloadview/issues/67