簡體   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