【发布时间】:2017-09-14 23:40:25
【问题描述】:
我有一个 Flask 应用程序,它通过 x-accel-redirect 将应该获得静态文件的请求重定向到 NGINX。有时,这些下载会在完成之前被切断。例如,通过 cURL,我会看到:
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
这似乎更常发生在非常大的文件 (10gb+) 上,但我已经看到它也发生在 ~90mb 的较小文件上。 Nginx 访问日志显示通过和服务的请求不同、不完整的数据量:
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 没有任何用处。
我的相关烧瓶配置如下:
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
我的相关 NGINX 配置如下(运行我的烧瓶应用程序的 uWSGI 服务器在 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;
}
【问题讨论】:
标签: python nginx flask uwsgi x-accel-redirect