Debian Codegraph @

docs/optimizations/nginx.txt

raw
1 #####
2 Nginx
3 #####
4
5 If you serve Django behind Nginx, then you can delegate the file streaming
6 to Nginx and get increased performance:
7
8 * lower resources used by Python/Django workers ;
9 * faster download.
10
11 See `Nginx X-accel documentation`_ for details.
12
13
14 *****************
15 Known limitations
16 *****************
17
18 * Nginx needs access to the resource by URL (proxy) or path (location).
19 * Thus :class:`~django_downloadview.files.VirtualFile` and any generated files
20 cannot be streamed by Nginx.
21
22
23 ************
24 Given a view
25 ************
26
27 Let's consider the following view:
28
29 .. literalinclude:: /../demo/demoproject/nginx/views.py
30 :language: python
31 :lines: 1-6, 8-17
32
33 What is important here is that the files will have an ``url`` property
34 implemented by storage. Let's setup an optimization rule based on that URL.
35
36 .. note::
37
38 It is generally easier to setup rules based on URL rather than based on
39 name in filesystem. This is because path is generally relative to storage,
40 whereas URL usually contains some storage identifier, i.e. it is easier to
41 target a specific location by URL rather than by filesystem name.
42
43
44 ********************************
45 Setup XAccelRedirect middlewares
46 ********************************
47
48 Make sure ``django_downloadview.SmartDownloadMiddleware`` is in
49 ``MIDDLEWARE`` of your `Django` settings.
50
51 Example:
52
53 .. literalinclude:: /../demo/demoproject/settings.py
54 :language: python
55 :lines: 62-69
56
57 Then set ``django_downloadview.nginx.XAccelRedirectMiddleware`` as
58 ``DOWNLOADVIEW_BACKEND``:
59
60 .. literalinclude:: /../demo/demoproject/settings.py
61 :language: python
62 :lines: 75
63
64 Then register as many ``DOWNLOADVIEW_RULES`` as you wish:
65
66 .. literalinclude:: /../demo/demoproject/settings.py
67 :language: python
68 :lines: 83-88
69
70 Each item in ``DOWNLOADVIEW_RULES`` is a dictionary of keyword arguments passed
71 to the middleware factory. In the example above, we capture responses by
72 ``source_url`` and convert them to internal redirects to ``destination_url``.
73
74 .. autoclass:: django_downloadview.nginx.middlewares.XAccelRedirectMiddleware
75 :members:
76 :inherited-members:
77 :undoc-members:
78 :show-inheritance:
79 :member-order: bysource
80
81
82 **********************************************
83 Per-view setup with x_accel_redirect decorator
84 **********************************************
85
86 Middlewares should be enough for most use cases, but you may want per-view
87 configuration. For `nginx`, there is ``x_accel_redirect``:
88
89 .. autofunction:: django_downloadview.nginx.decorators.x_accel_redirect
90
91 As an example:
92
93 .. literalinclude:: /../demo/demoproject/nginx/views.py
94 :language: python
95 :lines: 1-7, 17-
96
97
98 *******************************************
99 Test responses with assert_x_accel_redirect
100 *******************************************
101
102 Use :func:`~django_downloadview.nginx.decorators.assert_x_accel_redirect`
103 function as a shortcut in your tests.
104
105 .. literalinclude:: /../demo/demoproject/nginx/tests.py
106 :language: python
107
108 .. autofunction:: django_downloadview.nginx.tests.assert_x_accel_redirect
109
110 The tests above assert the `Django` part is OK. Now let's configure `nginx`.
111
112
113 ***********
114 Setup Nginx
115 ***********
116
117 See `Nginx X-accel documentation`_ for details.
118
119 Here is what you could have in :file:`/etc/nginx/sites-available/default`:
120
121 .. code-block:: nginx
122
123 charset utf-8;
124
125 # Django-powered service.
126 upstream frontend {
127 server 127.0.0.1:8000 fail_timeout=0;
128 }
129
130 server {
131 listen 80 default;
132
133 # File-download proxy.
134 #
135 # Will serve /var/www/files/myfile.tar.gz when passed URI
136 # like /optimized-download/myfile.tar.gz
137 #
138 # See http://wiki.nginx.org/X-accel
139 # and https://django-downloadview.readthedocs.io
140 #
141 location /proxied-download {
142 internal;
143 # Location to files on disk.
144 alias /var/www/files/;
145 }
146
147 # Proxy to Django-powered frontend.
148 location / {
149 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
150 proxy_set_header Host $http_host;
151 proxy_redirect off;
152 proxy_pass http://frontend;
153 }
154 }
155
156 ... where specific configuration is the ``location /optimized-download``
157 section.
158
159 .. note::
160
161 ``/proxied-download`` has the ``internal`` flag, so this location is not
162 available for the client, i.e. users are not able to download files via
163 ``/optimized-download/<filename>``.
164
165
166 *********************************************
167 Assert everything goes fine with healthchecks
168 *********************************************
169
170 :doc:`Healthchecks </healthchecks>` are the best way to check the complete
171 setup.
172
173
174 *************
175 Common issues
176 *************
177
178 ``Unknown charset "utf-8" to override``
179 =======================================
180
181 Add ``charset utf-8;`` in your nginx configuration file.
182
183 ``open() "path/to/something" failed (2: No such file or directory)``
184 ====================================================================
185
186 Check your ``settings.NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_DIR`` in Django
187 configuration VS ``alias`` in nginx configuration: in a standard configuration,
188 they should be equal.
189
190
191 .. rubric:: References
192
193 .. target-notes::
194
195 .. _`Nginx X-accel documentation`: http://wiki.nginx.org/X-accel