繁体   English   中英

docker nginx django gunicorn 未在生产中提供 static 文件

[英]docker nginx django gunicorn not serving static files in production

尝试使用 nginx + gunicorn + docker + django 部署网站。但 ngingx 不提供 static 个文件。 以下是配置:

Django 项目结构

在此处输入图像描述

设置文件production.py

STATIC_URL = "/static/"

"""STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)"""


STATIC_ROOT = "/app/forex/static/admin/"

Docker 文件为 nginx

FROM nginx:1.19.0

COPY ./default.conf /etc/nginx/conf.d/default.conf

nginx 配置

upstream django {
    server website:8000;
}

server {
    listen 80;
    
    client_max_body_size 100M;
    proxy_set_header X-Forwarded-Proto $scheme;

    location / {
        proxy_pass http://django;
    }
    
    location /media/ {
        alias /app/media/;
    }

    location /static/ {
        alias /app/forex/static/admin/;
    }
}

Gunicorn docker 文件

FROM python:3


ADD requirements.txt /app/requirements.txt

ADD . /app
WORKDIR /app
EXPOSE 8000:8000

RUN pip install --upgrade pip && pip install -r /app/requirements.txt
RUN python manage.py collectstatic --no-input --settings=forex.settings.production

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "forex.wsgi:application", "DJANGO_SETTINGS_MODULE=forex.settings.production"]

docker-compose.yml

services:
    website:
        build:
          context: .
          dockerfile: Dockerfile.app
        env_file:
          - env
        container_name: website_container_8
    nginx:
        build: ./nginx
        volumes:
            - static:/app/forex/static/admin/
        ports:
            - "80:80"
        depends_on:
            - website
volumes:
  static:

从 nginx 容器,它不会复制 static 文件。 在此处输入图像描述

我需要更改什么才能使其正常工作?

您的文件位于您的网站容器中,您需要将其与 nginx 容器共享:

services:
    website:
        build:
          context: .
          dockerfile: Dockerfile.app
        env_file:
          - env
        container_name: website_container_8
        volumes:
            - static:/app/forex/static/admin/ #<-- you want to share this
    nginx:
        build: ./nginx
        volumes:
            - static:/app/forex/static/admin/ #<-- with this folder
        ports:
            - "80:80"
        depends_on:
            - website
volumes:
  static: #<-- you can do it through this

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM