首页 > 解决方案 > Django 和 nginx 以及 gunicorn 和 docker compose - 配置 URL

问题描述

我正在用 Django 2、Docker Compose 和 Nginx 构建一个练习应用程序。旋转图像是成功的,但是,任何非 Django/Python 文件都不会加载。

例如,/static/bootstrap-3.2/dist/css/bootstrap.css(或 /static 中的任何文件)不会加载。我已经在 SO 上看到了一些其他相关的问题,但出于某种原因,我认为我的配置仍然缺少一些东西。有控制台日志和终端行指示静态或其他目录资源的 404 错误。

感谢任何帮助,谢谢!

工作目录树

|- gunicorn/
|- nginx\
|-- nginx.conf
|-- ...
|- django/
|-- __init__.py
|-- templates/
|-- static/
|-- manage.py
|-- settings.py
|-- forms.py
|-- views.py
|-- urls.py
|-- wsgi.py 
|- docker-compose.yml
|- Dockerfile
|- requirements.txt

Dockerfile

FROM python
ENV PYTHONUNBUFFERED 1

ADD . /ljingo
WORKDIR /ljingo

RUN pip install -r requirements.txt
RUN python -m nltk.downloader punkt
RUN python -m nltk.downloader wordnet
RUN python -m nltk.downloader averaged_perceptron_tagger

要求.txt

Django==2.0
django-crispy-forms
psycopg2
gunicorn
nltk==3.3

nginx/nginx.conf

server {
    root /;

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    access_log /logs/access.log;

    error_log /logs/error.log;

    location /media  {
        alias /django/media;
    }

    location /static {
        alias /django/static;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://django:8000;
    }
}

码头工人-compose.yml

version: '2'
services:
  nginx:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - .:/ljingo
      - ./nginx:/etc/nginx/conf.d
      - ./static:/static
    depends_on:
      - django

  django:
    build: .
    image: ljingo
    command: python3 manage.py collectstatic
    command: python3 manage.py migrate
    # command: python3 manage.py runserver 0.0.0.0:80
    command: gunicorn ljingo.wsgi -c ./gunicorn/gunicorn.py -b 0.0.0.0:8000
    # depends_on:
    #   - db
    volumes:
      - .:/ljingo
    ports:
      - "8000:8000"

端子 STDOUT 输出

mac:django-gunicorn bmalone$ docker-compose up
django-gunicorn_django_1 is up-to-date
Starting django-gunicorn_nginx_1 ... done
Attaching to django-gunicorn_django_1, django-gunicorn_nginx_1
django_1  | [2018-08-01 00:23:22 +0000] [1] [INFO] Starting gunicorn 19.9.0
django_1  | [2018-08-01 00:23:22 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
django_1  | [2018-08-01 00:23:22 +0000] [1] [INFO] Using worker: sync
django_1  | [2018-08-01 00:23:22 +0000] [9] [INFO] Booting worker with pid: 9
django_1  | [2018-08-01 00:23:23 +0000] [10] [INFO] Booting worker with pid: 10
django_1  | [2018-08-01 00:23:23 +0000] [11] [INFO] Booting worker with pid: 11
django_1  | [2018-08-01 00:23:23 +0000] [12] [INFO] Booting worker with pid: 12
django_1  | [2018-08-01 00:23:23 +0000] [13] [INFO] Booting worker with pid: 13
django_1  | Not Found: /static/custom.css
django_1  | Not Found: /static/bootstrap-3.2/dist/css/bootstrap.css
django-gunicorn_nginx_1 exited with code 0

标签: djangonginxstaticdocker-composenltk

解决方案


您的docker-compose.yml文件不能有多个command条目。您需要将调用合并manage.pygunicorn一个命令中。

command: python3 manage.py collectstatic && python3 manage.py migrate && gunicorn ljingo.wsgi -c ./gunicorn/gunicorn.py -b 0.0.0.0:8000

由于这有点长且难以阅读,我建议创建一个简单的 bash 脚本来运行每个命令,并从docker-compose.yml配置中调用:

start_django.sh

#!/usr/bin/env bash

set -ex

python3 manage.py collectstatic
python3 manage.py migrate
gunicorn ljingo.wsgi -c ./gunicorn/gunicorn.py -b 0.0.0.0:8000

码头工人-compose.yml

django:
  # ...all of the other config goes here, but a single command entry...
  command: ./start_django.sh

确保一旦你创建了它,start_django.sh你就在容器内赋予它可执行权限。这可以通过您的Dockerfile

Dockerfile

ADD . /ljingo
RUN chmod +x /lingo/start_django.sh
WORKDIR /ljingo

推荐阅读