【发布时间】:2017-11-19 18:32:04
【问题描述】:
我已经在 StackOverflow 上阅读了一些关于此问题的帖子,但无法解决我的问题,正如标题所说,我正在尝试使用 django、docker 上的 nginx 提供静态服务。
起初我收到一条错误消息 PermissionError: [Errno 13] Permission denied: '/static/admin'。我发现我必须将 STATIC_ROOT 从 STATIC_ROOT = '/static' 更改为 STATIC_ROOT = os.path.join(BASE_DIR, 'static')。现在collectstatic 在 Docker 的终端上工作,但我的 webapp 的静态仍然无法工作,我想了解原因并修复它。
如果您需要更多信息,我已将项目放在Github。
project.conf
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
alias /static/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
docker-compose.yml
version: '2'
services:
nginx:
image: nginx:latest
container_name: nz01
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static
depends_on:
- web
web:
build: .
container_name: dz01
command: bash -c 'python manage.py collectstatic --noinput && python manage.py makemigrations && python manage.py migrate && gunicorn oqtor.wsgi -b 0.0.0.0:8000'
depends_on:
- db
volumes:
- ./src:/src
- /static:/static
expose:
- "8000"
links:
- redis
...
...
Dockerfile:
FROM python:latest
ENV PYTHONUNBUFFERED 1
#ENV C_FORCE_ROOT true
ENV APP_USER myapp
ENV APP_ROOT /src
RUN mkdir /src;
RUN groupadd -r ${APP_USER} \
&& useradd -r -m \
--home-dir ${APP_ROOT} \
-s /usr/sbin/nologin \
-g ${APP_USER} ${APP_USER}
WORKDIR ${APP_ROOT}
RUN mkdir /config
ADD config/requirements.pip /config/
RUN pip install -r /config/requirements.pip
USER ${APP_USER}
ADD . ${APP_ROOT}
【问题讨论】:
-
“不工作”是什么意思?怎么了?你期望会发生什么?如果有错误消息,提供它会有所帮助。
-
@MichaelMior 没有错误日志,静态不会加载。
-
“不会加载”是什么意思?当你尝试时会发生什么?
-
@MichaelMior 好吧,一切都显示出来了,有文本,有按钮但没有样式,图像也是空的,JS 也不在这里,这是一个只有文本的白页。
-
你有没有解决这个问题。如果是,请分享。我也被困住了
标签: python django nginx django-staticfiles