python - 如何通过 nginx 在 Django 中提供生成的文件作为下载?
问题描述
我正在尝试通过 nginx 在我的媒体文件夹中提供生成的 zip 文件,但出现以下错误:open() "/etc/nginx/htmltest.zip" failed (2: No such file or directory)
不知何故,nginx 正在寻找格式错误的“/etc/nginx/html”中的文件,我不明白为什么。
我正在通过 docker-compose 使用 nginx,如果我不使用 nginx 并且只在开发模式下运行 Django,代码就可以正常工作。
folder_path = os.path.join(settings.MEDIA_ROOT, str(uuid.uuid4()))
os.mkdir(folder_path)
shutil.make_archive(os.path.join(folder_path, "test"), 'zip', folder_path, "test")
response = download(os.path.join(folder_path, f"{zip_name}.zip"))
def download(path):
if os.path.exists(path):
with open(path, 'rb') as f:
response = HttpResponse(f.read(), content_type="application/zip")
response['Content-Disposition'] = 'inline;filename=' + os.path.basename(path)
response['X-Accel-Redirect'] = os.path.basename(path)
return response
raise Http404
我的 nginx.conf:
upstream django_app {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://django_app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
client_max_body_size 100M;
}
location /static/ {
alias /code/static/;
}
location /media/ {
alias /code/media/;
}
}
settings.py 中的 MEDIA_ROOT:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
解决方案
您错误地使用了 nginxX-Accel-Redirect
。
使用X-Accel-Redirect
时不需要自己从 Django 读取和发送文件,nginx 会为您完成。这就是重点X-Accel-Redirect
:卸载从应用服务器发送的文件到 nginx(这更好)。
所以删除X-Accel-Redirect
标题,然后 Django 将直接提供文件。
如果要使用,X-Accel-Redirect
则需要按照文档中的说明设置 nginx(您的配置不完整,因为它缺少internal
位置),然后在标头中提供内部URLX-Accel-Redirect
(您错误地提供了文件系统路径)。
推荐阅读
- c# - 并行处理消息组,但组中的每条消息是连续的
- powerbi - Power Bi Embedded 数据集限制
- javascript - 为什么 Google Chrome 上没有 csrftoken cookie 而 Firefox 上是?- Javascript
- javascript - 在不同站点上运行 Javascript 代码
- aspnetboilerplate - Asp.Net BoilerPlate - PermissionManager.GetAllPermissions() 未获得新权限
- html - VML 按钮与文本不对齐
- file - 如何批量重命名文件夹中的文件?
- database - 无法连接 hikari 连接池 - 获取 IO 错误:套接字读取中断,连接延迟 44 毫秒,身份验证延迟 0 毫秒
- publish-subscribe - Velodyne 上的 ROS 订阅者提供过时的点云
- dart - 如何在展开时在 ExpansionTile 中加载内容?