【发布时间】: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 我什至多次删除所有容器和图像都无济于事:/