django - Nginx 不提供媒体文件。Dockerizing django/nginx/gunicorn/postgresql
问题描述
我有一个在 3 个 docker 容器中运行的项目。一个用于 django 项目本身,另一个用于 postgres,第三个用于 nginx。我的静态文件服务很好,而所有媒体文件都没有找到。但是,上传的每个文件都正确保存在媒体文件夹的 Web 容器中。
这是 docker-compose 文件:
version: '3.8'
volumes:
postgres_data:
static:
services:
db:
image: postgres:latest
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env
web:
build: .
restart: always
command: gunicorn foodgram.wsgi:application --bind 0.0.0.0:8000
volumes:
- ./static:/static
- ./media:/media
ports:
- "8000:8000"
depends_on:
- db
env_file:
- ./.env
nginx:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./static:/etc/nginx/html/static
- ./media:/etc/nginx/html/media
depends_on:
- web
这是码头文件:
FROM python:3.8.5
WORKDIR /code
COPY . /code
RUN pip install -r /code/requirements.txt
这是 nginx.conf:
events {}
http {
include mime.types;
server {
location / {
proxy_pass http://web:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
location /static/ {
autoindex on;
root /etc/nginx/html/;
}
location /media/ {
autoindex on;
root /etc/nginx/html/;
}
}
}
最后是 nginx dockerfile:
FROM nginx:latest
COPY nginx/nginx.conf /etc/nginx/nginx.conf
返回错误:
19#19: *1 open() "/etc/nginx/html/media/4.jpg" failed (2: No such file or directory)
媒体/静态文件夹中没有嵌套文件夹。
解决方案
推荐阅读
- c# - 如果我不关闭 StardandInput,C# 重定向其他控制台应用程序 StandardOutput 失败
- python - 保留 JSON obj 中的某些值,使其结构更深
- python - 检查 2D numpy 数组中的每个元素是否都在集合中
- .net - 将带有 OdbcConnection 的 .Net/C# 项目迁移到带有 Informix DB 的 SqlConnection
- python-3.x - DRF 标注作者有书的所有店铺
- python - 有没有一种巧妙的方法可以从这个字符串列表中提取日期,其中一些字符串没有日期模式?
- angular - 带有远程 URL 的 SimpleLightbox
- arrays - 将文件中的多种数据类型扫描到c中的一个数组中
- sequence - 考虑整数溢出的重新排序序列
- angular - Angular 12 http请求返回未定义的参数