繁体   English   中英

使用 Docker、nginx 和 gunicorn 服务 Django static 文件

[英]serving Django static files with Docker, nginx and gunicorn

我正在为我们设置一个Django 2.0应用程序,其中包含Dockernginxgunicorn

它正在运行服务器,但 static 文件不工作。

这里是settings.py的内容

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_my_project')
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'static_root')

在开发时,我将 static 文件放在static_my_project中,在运行时collectstatic复制到static_cdn/static_root

目录结构就像

app
 |- myapp
    |- settings.py
 |- static_my_project
 |- static_cdn
    |- static_root
 |- config
    |- nginx
       |- nginx.conf
 |- manage.py
 |- Docker
 |- docker-compose.yml

跑步时

docker-compose up --build

在运行collectstatic时,它给出了将复制 static 个文件的路径

库伯开发| --: 运行 collectstatic
库伯开发|
库伯开发| 您已要求在目的地收集 static 个文件
myapp-开发| 在您的设置中指定的位置:
myapp-开发|
myapp-开发| /app/static_cdn/static_root
myapp-开发|
myapp-开发| 这将覆盖现有文件!
myapp-开发| 你确定要这么做吗?
myapp-开发|
myapp-开发| 输入“是”继续,或输入“否”取消:
myapp-开发| 0 static 个文件复制到“/app/static_cdn/static_root”,210 未修改。

config/nginx/nginx.conf文件包含以下设置

upstream web {
    ip_hash;
    server web:9010;
}

server {
    location /static {
        autoindex on;
        alias /static/;
    }

    location / {
        proxy_pass http://web;
    }
    listen 10080;
    server_name localhost;
}

docker-compose.yml

version: '3'

services:
  nginx:
    image: nginx:latest
    container_name: "koober-nginx"
    ports:
      - "10080:80"
      - "10443:43"
    volumes:
      - .:/app
      - ./config/nginx:/etc/nginx/conf.d
      - ./static_cdn/static_root/:/static
    depends_on:
      - web
  web:
    build: .
    container_name: "koober-dev"
    command: ./start.sh
    volumes:
      - .:/app
      - ./static_cdn/static_root/:/app/static_cdn/static_root
    ports:
      - "9010:9010"
    depends_on:
      - db
  db:
    image: postgres
    container_name: "koober-postgres-db"

Dockerfile

FROM ubuntu:18.04

# -- Install Pipenv:
FROM python:3
ENV PYTHONUNBUFFERED 1

ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

# -- Install Application into container:
RUN set -ex && mkdir /app

WORKDIR /app
ADD requirements.txt /app/

RUN pip install -r requirements.txt

# -- Adding dependencies:
ADD . /app/

但它没有加载 static 个文件。

您需要有一个共享卷到STATIC_ROOT目录,以便您的nginx容器可以反向代理到 Web 服务器和由您的 Web 服务器生成的静态文件。

docker-compose.yml

services:
  nginx:
    image: nginx:alpine
    volumes:
      - ./static_cdn/static_root/:/static
    ports:
      - 80:80
  web:
    build: .
    volumes:
      - ./static_cdn/static_root/:/app/static_cdn/static_root

现在在你的 nginx.conf 添加:

location /static/ {
    alias /static/;
}

Nginx Dockerfile:

FROM nginx:stable-alpine

COPY default.conf /etc/nginx
COPY default.conf /etc/nginx/conf.d

EXPOSE 80

上面Dockerfile中提到的default.conf:

server {

    listen 80 default_server;
    server_name _;

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

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

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

}

注意:上面提到的default.conf和Dockerfile在同一个文件夹下,构建那个镜像,把它作为下面docker-compose文件中的Nginx镜像使用。

docker-compose 文件如下所示:

version: '3.8'

services:
  web:
    image: <django-image-name>
    command: gunicorn --bind 0.0.0.0:8000 licensing_platform.wsgi --workers=4
    volumes:
      - static_volume:/app/static
      - media_volume:/app/media
    expose:
      - "8000"
    networks:
      - django-network

  nginx:
    image: <nginx-image-name>
    restart: always
    volumes:
      - static_volume:/app/static
      - media_volume:/app/media
    ports:
      - "80:80"
    depends_on:
      - web
    networks:
      - django-network

networks:
  django-network:
    name: django-network

volumes:
  media_volume:
  static_volume:

引用的app/路径取决于工作目录: django 应用程序 Dockerfile 将以:

FROM ubuntu:20.04
ADD . /app
WORKDIR /app
EXPOSE 8000

代码参考: https://github.com/addu390/licensing-as-a-platform

上面提到的代码参考是我正在从事的一个开源项目,没有任何商业利益。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM