【问题标题】:Django and nginx with docker compose - static files are not loaded使用 docker compose 的 Django 和 nginx - 未加载静态文件
【发布时间】:2017-03-15 22:52:00
【问题描述】:

我知道有人问了几个类似的问题,但我没有设法在任何地方找到正确的答案。

我想码头化我的 Django 应用程序。该应用程序在 docker 容器中运行良好,但未加载静态文件(图像、css 等)。

我的docker-compose.yml

version: '2' 
services:   
  web:
    build: .
    command: ./start.sh
    volumes:
      - .:/app
      - /app/www/static
    ports:
      - "8000:8000"
    env_file: .env

  nginx:
    build: ./nginx
    links:
      - web
    ports:
      - "0.0.0.0:80:80"
    volumes_from:
      - web

start.sh 脚本

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

exec gunicorn audiobridge.wsgi:application --bind 0.0.0.0:8000 --workers 3

web 容器的 Dockerfile

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/

然后我的项目中有一个 nginx/ 目录,其中包含以下Dockerfile

FROM nginx
COPY sites-enabled/audiobridge /etc/nginx/sites-enabled/

在 nginx/sites-enabled 里面有 nginx 配置文件:

server {

listen 80;
server_name 127.0.0.1;
charset utf-8;

location /static {
    alias /app/www/static/;
}

location / {
    proxy_pass http://web:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

【问题讨论】:

  • 不完全相同的图像。但是与css文件有关,请检查一下是否有帮助? stackoverflow.com/questions/40280648/…
  • 您是否尝试在位置添加尾部斜杠以使其成为location /static/
  • 发布您的docker-compose logs

标签: django nginx docker docker-compose


【解决方案1】:

enter image description here

volumes:
  - "../nginx/conf.d:/etc/nginx/conf.d/templates"
  - "/static:/static"

您是否尝试将此代码添加到 nginx 和 django 应用程序?

【讨论】:

    猜你喜欢
    • 2016-08-15
    • 2020-07-18
    • 1970-01-01
    • 2016-07-17
    • 2017-03-07
    • 2018-09-28
    • 2021-08-08
    • 1970-01-01
    • 2019-12-25
    相关资源
    最近更新 更多