简体   繁体   English

Django + NGINX仅提供一些静态文件

[英]Django + NGINX only serving some static files

This is probably the millionth django+nginx post, but since I didn't find an answer after more than 5 hours here it goes: 这可能是django + nginx帖子的百万分之一,但是由于我在5个多小时后没有找到答案,所以它去了:

My issue is that not all static files get served, only some. 我的问题是,并非所有静态文件都可以提供,只有一些。 The whole thing runs inside Docker where I run 整个过程都在我运行的Docker内部运行

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

on each start, but it does not serve my new .js and .css files although they are in the same directories as the old ones. 每次启动时,尽管它们与旧文件位于同一目录中,但它不提供我的新.js和.css文件。

I also see the message: 我也看到消息:

132 static files copied to '/static'.

and above that is the list of files being copied there, including the new ones. 上方是要复制到其中的文件列表,包括新文件。

Project Structure: 项目结构:

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

settings.py: settings.py:

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

nginx.conf: 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;
}

Why aren't all static files being served? 为什么不提供所有静态文件?

EDIT: 编辑:

Dockerfile: 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: 泊坞窗,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:

Ok, with the additional info I think I know what's going on. 好的,加上其他信息,我想我知道发生了什么事。 You run your python script to generate static files (which I assume it does) on build. 您在构建时运行python脚本以生成静态文件(我认为是的)。 On run, you mount the directory, so it will overwrite everything in the static directory. 在运行时,您将挂载目录,因此它将覆盖静态目录中的所有内容。 Either don't mount it or add the scripts 不要挂载它或添加脚本

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

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

Also, the mounting of static into the nginx container doesn't do anything since you are only reverse proxying into the django container and not giving a root path . 同样,将静态对象安装到nginx容器中也没有任何作用,因为您只是反向代理到django容器中,而没有给出根路径 Or if it does work by some coincident, I'm not sure if it would get the newly generated files on startup. 或者,如果它确实偶然发生,我不确定它是否会在启动时获取新生成的文件。

Hope this helps, had a lot of fun with that stuff when I started with docker... 希望这会有所帮助,当我开始使用docker时,可以从中获得很多乐趣...

A workaround for now is to create the static folder on my local machine and then transfer the files to the desired position inside docker-compose. 现在的解决方法是在本地计算机上创建静态文件夹,然后将文件传输到docker-compose中的所需位置。

When I find a real solution, on why collect static is not working within docker I will update this 当我找到一个真正的解决方案时,关于为什么为什么在docker中无法收集静态信息的问题,我将对此进行更新

Edit: Solution provided by LevinM 编辑:LevinM提供的解决方案

updated the Dockerfile (This only provides the image and installs python libraries from requirements.txt due to the 'onbuild' parameter): 更新了Dockerfile(由于提供了“ onbuild”参数,因此仅提供映像并从requirements.txt安装python库):

FROM python:3.6.4-onbuild

updated docker-compose.yml: 更新了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:

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

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