简体   繁体   English

无法通过 nginx 和 docker 提供 django 静态文件

[英]Unable to serve django static files via nginx and docker

I am having issue with configuring a nginx server using docker and django.我在使用 docker 和 django 配置 nginx 服务器时遇到问题。 I think the issues lies in volume path.我认为问题在于音量路径。
Here is my directory structure这是我的目录结构

-nginx
--default.conf
--Dockerfile
-portfolio_app (django webpapp)
--main_app
---settings.py
---wsgi.py
--sub_app
---views.py
---static
---media
-docker-compose.yml
-Dockerfile (django related)
-entrypoint.sh (to start django server)

Regarding django it working, but i am unable to serve the static files.关于 django 它工作,但我无法提供静态文件。 I think i am not giving the path correctly.我认为我没有正确给出路径。

Here is Dockerfile related to django这是与 django 相关的 Dockerfile

FROM python:3.8.13-slim-buster

WORKDIR /app
RUN pip install --upgrade pip
COPY ./requirements.txt ./
RUN pip install -r requirements.txt

COPY ./portfolio_app ./

COPY ./entrypoint.sh ./
ENTRYPOINT ["sh","/app/entrypoint.sh"]

nginx files nginx 文件
default.conf默认.conf

upstream django {
    server portfolio_application:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://django;
    }

    location /static/ {
        alias /static_volume/;
    }
}

Dockerfile Dockerfile

FROM nginx:1.19.0-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf

Here is docker-compose.yml file这是 docker-compose.yml 文件

version: '3.3'
services:
  portfolio_application:
    build: 
      context: .
    container_name: portfolio_app
    volumes:
      - static_volume:/app/sub_app/static
      - media_volume:/app/sub_app/media
    ports:
      - "8000:8000"
    env_file:
      - .env

  nginx:
    build: ./nginx
    volumes:
      - static_volume:/app/sub_app/static
      - media_volume:/app/sub_app/media
    ports:
      - "80:80"
    depends_on:
      - portfolio_application


volumes:
  media_volume:
  static_volume:

I am not sure about the path of volumes in yml file and settings of default.conf.我不确定 yml 文件中卷的路径和 default.conf 的设置。 Here are the logs这是日志

Attaching to portfolio_app, docker_nginx_1
portfolio_app            | [2022-07-21 09:22:10 +0000] [7] [INFO] Starting gunicorn 20.1.0
nginx_1                  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1                  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
portfolio_app            | [2022-07-21 09:22:10 +0000] [7] [INFO] Listening at: http://0.0.0.0:8000 (7)
portfolio_app            | [2022-07-21 09:22:10 +0000] [7] [INFO] Using worker: sync
nginx_1                  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
portfolio_app            | [2022-07-21 09:22:10 +0000] [9] [INFO] Booting worker with pid: 9
nginx_1                  | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_1                  | 10-listen-on-ipv6-by-default.sh: /etc/nginx/conf.d/default.conf differs from the packages version, exiting
nginx_1                  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1                  | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1                  | 172.19.0.1 - - [21/Jul/2022:09:22:13 +0000] "GET / HTTP/1.1" 200 48080 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" "-"
nginx_1                  | 2022/07/21 09:22:13 [error] 29#29: *1 open() "/static_volume/lib/animate/animate.min.css" failed (2: No such file or directory), client: 172.19.0.1, server: , request: "GET /static/lib/animate/animate.min.css HTTP/1.1", host: "0.0.0.0", referrer: "http://0.0.0.0/"
nginx_1                  | 172.19.0.1 - - [21/Jul/2022:09:22:13 +0000] "GET /static/lib/animate/animate.min.css HTTP/1.1" 404 153 "http://0.0.0.0/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0" "-"

Edit This is how i added static url and file in settings.py编辑这就是我在settings.py中添加静态url和文件的方式

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

In your nginx service you have mounted the volume as below.在您的 nginx 服务中,您已安装卷,如下所示。 Where static_volume is the host volume and /app/sub_app/static is the container directory where it is mounted to.其中 static_volume 是主机卷, /app/sub_app/static是挂载到的容器目录。

static_volume:/app/sub_app/static

But in your nginx config file you are routing the static requests to /static_volume/ .但是在您的 nginx 配置文件中,您将静态请求路由到/static_volume/ Instead you need to point it to your container directory as shown below相反,您需要将其指向您的容器目录,如下所示

location /static/ {
        alias /app/sub_app/static/;
    }

default.conf:默认配置:

instead of this:而不是这个:

location /static/ {
        alias sub_app/static/;
    }

try this:尝试这个:

location /static/ {
        alias ./static/;
    }

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

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