I have a Flask app that redirects requests that should get served static files to NGINX through x-accel-redirect. On occasion, those downloads will get cut off before being finished. For example, through cURL, I'd see:
curl http://my_server/some_static_file.tar > temp.tar
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
77 14.4G 77 11.2G 0 0 55.8M 0 0:04:24 0:03:25 0:00:59 58.9M
curl: (18) transfer closed with 3449105332 bytes remaining to read
This seems to happen more often with very big files (10gb+), but I've seen it also happen on smaller files of ~90mb. Nginx access logs show requests coming through and being served different, incomplete amounts of data:
1.2.3.4 - - [18/Apr/2017:01:16:26 +0000] "GET /some/flask/static/file/path HTTP/1.1" 200 15146008576 "-" "curl/7.38.0" "5.6.7.8"
1.2.3.5 - - [18/Apr/2017:01:16:29 +0000] "GET /some/flask/static/file/path HTTP/1.1" 200 15441739776 "-" "curl/7.38.0" "6.7.8.9"
errors.log
has nothing useful.
My relevant flask config is as follows:
response = make_response('')
response.headers.set('X-Accel-Redirect', '/_special_nginx_path/' + file_name)
response.headers.set('Content-Disposition', 'attachment',
filename=file_name)
# have tried both with and without setting content-length
response.headers.set('Content-Length', os.path.getsize(file_path))
try:
response.mimetype = mimetypes.guess_type(file_name)[0]
if not response.mimetype:
response.mimetype = 'application/octet-stream'
except AttributeError:
response.mimetype = 'application/octet-stream'
return response
My relevant NGINX config is as follows (where a uWSGI server running my flask app is running at 127.0.0.1:1234):
location / {
proxy_pass http://127.0.0.1:1234;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /_special_nginx_path {
internal;
alias /path/to/static/files;
}