Debian Codegraph @

docs/optimizations/apache.txt

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