【发布时间】:2021-02-25 03:28:48
【问题描述】:
我正在尝试通过 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')
【问题讨论】: