Django+;NGINX只提供一些静态文件
这可能是django+nginx发布的第一百万篇文章,但由于我在5个多小时后没有找到答案,所以这里是: 我的问题是并不是所有的静态文件都得到服务,只有一些。整件事都在Docker里面我跑的地方Django+;NGINX只提供一些静态文件,django,nginx,static,Django,Nginx,Static,这可能是django+nginx发布的第一百万篇文章,但由于我在5个多小时后没有找到答案,所以这里是: 我的问题是并不是所有的静态文件都得到服务,只有一些。整件事都在Docker里面我跑的地方 RUN python manage.py collectstatic --noinput; RUN python manage.py makemigrations; RUN python manage.py migrate; 在每次启动时,它都不会为我的新.js和.css文件提供服务,尽管它们与旧文件位
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:
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:
好的,有了更多的信息,我想我知道发生了什么。 您可以运行python脚本在构建时生成静态文件(我假设它会这样做)。在运行时,您将挂载目录,因此它将覆盖静态目录中的所有内容。 要么不挂载它,要么添加脚本
python manage.py collectstatic --noinput;
python manage.py makemigrations;
python manage.py migrate;
在入口点()
此外,将static装载到nginx容器中并没有任何作用,因为您只是反向代理到django容器中,而没有给出根路径。或者,如果它确实在某种巧合下工作,我不确定它是否会在启动时获得新生成的文件
希望这有帮助,当我开始使用docker时,我对这些东西很感兴趣…现在的解决方法是在本地机器上创建静态文件夹,然后将文件传输到docker compose中所需的位置 当我找到一个真正的解决方案,关于为什么收集静态不在docker中工作,我将更新这个 编辑:LevinM提供的解决方案 更新了Dockerfile(由于'onbuild'参数,这仅提供映像并从requirements.txt安装python库): 更新的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:
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:
进行了编辑,但要回答您的问题:1。如果我没有挂载它,collectstatic、makemigrations命令等将抛出错误。2.是的,它们与其他文件3在一起。添加了更多信息,感谢它的工作。我会在这里更新我自己的答案,让它更清楚。我还是不明白为什么它会起作用。此外,我还需要执行“-static content:/static”,以便在两者中与nginx共享该卷,否则它将无法访问这些文件。您必须在运行时和构建时之间有很大的区别。装载发生在运行时,这就是为什么它会覆盖映像中的内容。对于nginx静态的东西,我猜web容器中没有路由?(取决于您使用的框架/应用程序)您可以通过curl{docker host of web}/static进行检查。可能什么都没有。现在nginx正在访问您装载到的/static目录。我建议你解决这个问题。我有一个类似的问题,发现即使我正在将新文件复制到映像中,Docker仍会像使用旧卷一样使用以前的文件。我需要停止容器,用
docker compose-f-a web nginx
吹走容器和卷,然后用docker compose-d web nginx
@WillKeeling将容器和卷带回来。我甚至多次删除所有容器和图像,但都没有用:/