【问题标题】:Django + NGINX only serving some static filesDjango + NGINX 只提供一些静态文件
【发布时间】:2018-04-16 17:48:55
【问题描述】:

这可能是 django+nginx 的第 100 万篇帖子,但由于我在 5 个多小时后没有找到答案,所以就这样了:

我的问题是不是所有的静态文件都得到服务,只有一些。整个事情在我运行的 Docker 中运行

RUN python manage.py collectstatic --noinput;
RUN python manage.py makemigrations;
RUN python manage.py migrate;

在每次启动时,它不会为我的新 .js 和 .css 文件提供服务,尽管它们与旧文件位于相同的目录中。

我也看到了消息:

132 static files copied to '/static'.

上面是正在复制的文件列表,包括新文件。

项目结构:

/djangoapp
  /app
    /static
      /css
      /js
  /django
  /Dockerfile
/docker-compose

settings.py:

DEBUG = False
STATIC_URL = '/static/'
STATIC_ROOT = '/static'

nginx.conf:

upstream web {  
  ip_hash;
  server web:8000;
}

server {

    location /static {    
        autoindex on;    
        alias /static; 
    }

    location / {
        proxy_connect_timeout   3600;
        proxy_send_timeout      3600;
        proxy_read_timeout      3600;
        proxy_pass http://web/;
    }
    listen 8000;
    server_name localhost;
}

为什么不提供所有静态文件?

编辑:

Dockerfile:

FROM python:3.6.4-onbuild
RUN mkdir /config;
RUN mkdir /src;  
COPY . /src
WORKDIR /src 
RUN python manage.py collectstatic --noinput;
RUN python manage.py makemigrations;
RUN python manage.py migrate;
RUN chmod 775 -R /static
#this shows that the new files reside with the others in  the same directory
RUN ls -R /static/ 
CMD gunicorn WebInterface.wsgi -b 0.0.0.0:8000 --timeout 3600

docker-compose.yml:

version : '3'

services:
web:
  build: ./WebInterface
  container_name: WebDocker
  volumes: 
    - static-content:/static
  expose:
    - "80"

nginx:
  image: nginx:1.12.2
  container_name: NGINXDocker
  ports:
  - "8000:8000"
  volumes:
    - ./WebInterface:/src
    - ./config/nginx:/etc/nginx/conf.d/
    - static-content:/static
  depends_on:
    - web

volumes:
  static-content:

【问题讨论】:

  • 我遇到了类似的问题,发现即使我将新文件复制到映像中,Docker 仍会像使用旧卷一样继续使用以前的文件。我需要停止容器,用docker-compose -f -a web nginx 吹走容器和卷,然后用docker-compose up -d web nginx 恢复容器和卷
  • @WillKeeling 我什至多次删除所有容器和图像都无济于事:/

标签: django nginx static


【解决方案1】:

好的,有了附加信息,我想我知道发生了什么。 您在构建时运行您的 python 脚本来生成静态文件(我假设它会这样做)。运行时,您挂载目录,因此它将覆盖静态目录中的所有内容。 要么不挂载它,要么添加脚本

python manage.py collectstatic --noinput;
python manage.py makemigrations;
python manage.py migrate;

在入口点 (https://docs.docker.com/engine/reference/builder/#entrypoint)

此外,将静态安装到 nginx 容器中不会做任何事情,因为您只是反向代理到 django 容器中而不提供根路径。或者如果它确实在某些巧合下工作,我不确定它是否会在启动时获取新生成的文件。

希望这会有所帮助,当我开始使用 docker 时,对这些东西有很多乐趣......

【讨论】:

  • 进行了编辑,但要回答您的问题: 1. 如果我没有挂载它,collectstatic、makemigrations 命令等会引发错误。 2. 是的,它们与其他文件一起 3. 添加了更多信息
  • 谢谢它的工作。我将在这里更新我自己的答案以使其更清楚。 Tbh 我还是不太明白它为什么会起作用。我还需要在两者中都执行“-static-content:/static”以与 nginx 共享该卷,否则它将无法访问这些文件
  • 很高兴听到。您必须非常区分运行时间和构建时间。挂载发生在运行时,这就是它覆盖图像中的内容的原因。使用 nginx 静态的东西,我猜 web 容器内部没有路由? (取决于您使用的框架/应用程序)您可以通过 curl {docker-host-of-web}/static 检查。可能什么都没有。现在 nginx 正在访问您挂载到的 /static 目录。我建议你解决这个问题。
【解决方案2】:

目前的解决方法是在我的本地计算机上创建静态文件夹,然后将文件传输到 docker-compose 内的所需位置。

当我找到一个真正的解决方案时,关于为什么 collect static 在 docker 中不起作用,我会更新这个

编辑:LevinM 提供的解决方案

更新了 Dockerfile(由于 'onbuild' 参数,这仅提供图像并从 requirements.txt 安装 python 库):

FROM python:3.6.4-onbuild

更新了 docker-compose.yml:

version : '3'

services:

    web:
      build: ./WebInterface
      entrypoint: bash -c  "python manage.py collectstatic --noinput &&  python manage.py makemigrations && python manage.py migrate &&  gunicorn WebInterface.wsgi -b 0.0.0.0:8000 --timeout 3600";
      container_name: WebDocker
      volumes: 
        - ./WebInterface:/src
        - static-content:/static
      expose:
        - "80"
    
    nginx:
      image: nginx:1.12.2
      container_name: NGINXDocker
      ports:
      - "8000:8000"
      volumes:
        - ./WebInterface:/src
        - ./config/nginx:/etc/nginx/conf.d/
        - static-content:/static
      depends_on:
        - web

volumes:
  static-content:

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2011-01-27
    • 2015-11-30
    • 2016-01-28
    相关资源
    最近更新 更多