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