【发布时间】:2020-08-27 20:40:00
【问题描述】:
我正在尝试使用 postgres、gunicorn 和 nginx 对 django 项目进行 dockerize。我的问题是没有提供静态文件。但是在浏览器控制台中,我看到所有静态文件都被正确加载(代码 200)。我也可以轻松地直接访问任何静态文件,即127.0.0.1:8080/static/admin/.../base.css。 Nginx 也不会在控制台中显示任何错误。更让我感到奇怪的是,我的 redoc.yaml 加载正确。
这是我的 settings.py 的一部分,带有静态:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
这是我的主 Dockerfile:
FROM python:latest
RUN mkdir /code
COPY requirements.txt /code
RUN pip install -r /code/requirements.txt
COPY . /code
WORKDIR /code
CMD gunicorn api_yamdb.wsgi:application --bind 0.0.0.0:8000
这里是 nginx 的 Dockerfile:
FROM nginx:latest
COPY nginx/nginx.conf /etc/nginx/nginx.conf
这是 nginx.conf 文件:
events {}
http {
server {
location /static/ {
root /etc/nginx/html/;
}
location / {
proxy_pass http://web:8000;
}
}
}
最后是我的 docker-compose:
version: '3.8'
volumes:
postgres_data:
static:
services:
db:
image: postgres:latest
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env
web:
build: .
restart: always
command: gunicorn api_yamdb.wsgi:application --bind 0.0.0.0:8000
volumes:
- static:/static
ports:
- "8000:8000"
depends_on:
- db
env_file:
- ./.env
nginx:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./static:/etc/nginx/html/static
欢迎任何帮助。谢谢。
【问题讨论】:
-
如果响应为 200 OK,您如何确定文件未提供服务
-
页面看起来不像它应该的样子?
-
我不了解 nginx,但是当我运行 APACHE Django 应用程序时,我必须运行
python manage.py collectstatic? -
@alvo 不,这不是问题。