Обслуживание статических файлов django с помощью nginx в docker

У меня проблема со статическими файлами для админ-панели django. Для фронтенд-панели rest framework файлы обслуживаются без проблем

settings.py

STATIC_URL = "/static/"

STATIC_ROOT = BASE_DIR / "staticfiles"
STATIC_ROOT.mkdir(exist_ok=True)

Dockerfile:

FROM python:3.10-slim-buster

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

RUN python manage.py collectstatic --noinput

docker-compose:

version: '3.8'

services:
  web:
    build: .
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - .:/app
#      - static_volume:/usr/share/nginx/html/static

    ports:
      - "8000:8000"
  nginx:
    image: nginx:1.19-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./certbot/conf:/etc/letsencrypt
      - ./certbot/www:/var/www/certbot
#      - static_volume:/usr/share/nginx/html/static  # Ensure this matches the alias in nginx.conf
      - static_volume:/app/staticfiles

volumes:
  static_volume:

nginx/conf.d.default.conf

server {
    listen 80;
    server_name localhost;

    location /static/ {
        alias /usr/share/nginx/html/static;  # Ensure this matches the volume mapping in docker-compose.yml
        autoindex on;  # Optionally enable to troubleshoot file listing
    }


    location / {
        proxy_pass http://web:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Я впервые создаю приложение, используя django, gunicorn, nginx мои конфигурационные файлы взяты из chatgpt и учебников. если я делаю какую-то ошибку и кто-то укажет на нее, было бы здорово

Вернуться на верх