首页 > 解决方案 > 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)

媒体/静态文件夹中没有嵌套文件夹。

标签: djangonginx

解决方案


推荐阅读