使用docker+django+nginx时不提供静态文件

fkvaft9z  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(157)

我正在尝试将django项目dockerize,并使用nginx提供静态文件。除了没有提供静态文件外,一切似乎都正常工作。当我进入管理页面时,总是得到 Not Found: /django_static/rest_framework/css/prettify.css 用于django_静态文件夹中的文件。
文件夹结构:
后端
服务器
应用程序
django_静态
媒体
...
Docker
后端
dockerfile
wsgi-entrypoint.sh
nginx
生产
default.conf
发展
default.conf
dockerfile
这是我的docker compose:

services:
    nginx:
        build: ./docker/nginx
        ports:
            - 1234:80
        volumes:
            - ./docker/nginx/production:/etc/nginx/conf.d
            - django_static:/app/backend/server/django_static
            - media:/app/backend/server/media
        depends_on:
            - backend
        restart: "on-failure"
    backend:
        build:
            context: .
            dockerfile: ./docker/backend/Dockerfile
        entrypoint: /app/docker/backend/wsgi-entrypoint.sh
        ports:
            - "8001:8001"
        volumes:
            - django_static:/app/backend/server/django_static
            - media:/app/backend/server/media
        expose:
            - 8001

volumes:
    django_static:
    media:

django的dockerfile:

FROM python:3.7-slim

WORKDIR /app
ADD ./backend/requirements.txt /app/backend/
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install --upgrade pip
RUN pip install gunicorn
RUN pip install -r backend/requirements.txt

ADD ./backend /app/backend
ADD ./docker /app/docker

wsgi-entrypoint.py


# !/bin/sh

until cd /app/backend/server
do
    echo "Waiting for server volume..."
done

until ./manage.py migrate
do
    echo "Waiting for db to be ready..."
    sleep 2
done

./manage.py collectstatic --noinput

gunicorn server.wsgi --bind 0.0.0.0:8001 --workers 1 --threads 1

# ./manage.py runserver 0.0.0.0:8003

nginx docker文件:

FROM nginx:1.19.0-alpine

RUN rm /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]

nginx配置文件:

upstream glf {
    server  0.0.0.0:8001;
}

server {
    listen 80;

    location / {
        proxy_pass http://0.0.0.0:8001;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /django_static/ {
        alias /app/backend/server/django_static/;
    }

    location /media/ {
        alias /app/backend/server/media/;
    }
}

设置.py

MEDIA_URL = '/media/'
STATIC_URL = '/django_static/'
STATIC_ROOT = BASE_DIR / 'django_static'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

任何提示都将不胜感激!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题