从Django Docker向Nginx提供静态文件

时间:2019-01-05 03:06:25

标签: django docker nginx

我正在对Django应用程序进行docker化,但是没有提供静态文件。

settings.py

STATIC_URL = '/static/'

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

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

docker-compose.yml

services:
  nginx:
    image: nginx:alpine
    container_name: "originor-nginx"
    ports:
      - "10080:80"
      - "10443:43"
    volumes:
      - .:/app
      - ./config/nginx:/etc/nginx/conf.d
      - originor_static_volume:/app/static_cdn/static_root
      - originor_media_volume:/app/static_cdn/media_root
    depends_on:
      - web
  web:
    build: .
    container_name: "originor-web"
    command: ["./wait-for-it.sh", "db:5432", "--", "./start.sh"]
    volumes:
      - .:/app
      - originor_static_volume:/app/static_cdn/static_root
      - originor_media_volume:/app/static_cdn/media_root
    ports:
      - "9010:9010"
    depends_on:
      - db
  db:
    image: postgres:11
    container_name: "originor-postgres-schema"
    volumes:
      - originor_database:/var/lib/postgresql/data
    ports:
      - "5432:5432"
  pgadmin:
    image: dpage/pgadmin4
    container_name: "originor_pgadmin"
    volumes:
      - originor_pgadmin:/var/lib/pgadmin

volumes:
  originor_database:
  originor_static_volume:
  originor_media_volume:
  originor_pgadmin:

nginx.conf

error_log   /var/log/nginx/error.log;

include /etc/nginx/conf.d/proxy.conf;
proxy_headers_hash_bucket_size 128;

upstream dweb {
    ip_hash;
    server web:9010 fail_timeout=0;
}

server {
    listen 10080;
    server_name localhost;
    access_log /var/log/nginx/localhost.access.log combined;

    location /static/ {
        autoindex on;
        alias /app/static_cdn/static_root/;
    }

    location /media/ {
        alias /app/static_cdn/media_root/;
    }

    location / {
        proxy_pass http://dweb/;
    }
}

但是在浏览器中访问/admin/时,它将控制台

f032d416bce1_originor-web | Not Found: /static/admin/css/login.css
f032d416bce1_originor-web | Not Found: /static/admin/css/responsive.css
f032d416bce1_originor-web | Not Found: /static/admin/css/base.css
f032d416bce1_originor-web | Not Found: /static/admin/css/base.css

我可以执行以下命令来验证/app/static_cdn/static_root目录中的文件

docker exec -it <container_id> ls -la /app/static_cdn/static_root
  

编辑2:docker logs <container>

wait-for-it.sh: waiting 15 seconds for db:5432
wait-for-it.sh: db:5432 is available after 0 seconds
--: Starting application build


--: Creating migration
No changes detected
------: makemigrations complete


--: Running migration
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
------: migrate complete


--: load initial user data


--: load initial oauth app data


--: Running collectstatic

0 static files copied to '/app/static_cdn/static_root', 119 unmodified.
------: collectstatic complete


--: Starting Gunicorn.
[2019-01-11 13:26:47 +0000] [21] [INFO] Starting gunicorn 19.9.0
[2019-01-11 13:26:47 +0000] [21] [INFO] Listening at: http://0.0.0.0:9010 (21)
[2019-01-11 13:26:47 +0000] [21] [INFO] Using worker: sync
[2019-01-11 13:26:47 +0000] [23] [INFO] Booting worker with pid: 23
[2019-01-11 13:26:47 +0000] [24] [INFO] Booting worker with pid: 24
[2019-01-11 13:26:47 +0000] [25] [INFO] Booting worker with pid: 25
Not Found: /static/admin/css/fonts.css
  

编辑3:nginx日志

运行docker-compose up时会显示以下日志

enter image description here

但是运行docker logs originor-nginx并没有任何效果

1 个答案:

答案 0 :(得分:2)

在“网络”初始化的某些地方,您必须调用manage.py collectstatic才能将应用程序的静态文件放入您的卷中。更多信息https://docs.djangoproject.com/en/2.1/howto/static-files/

UPD: 用于uwsgi代理的nginx conf:

location / {
    uwsgi_pass uwsgi://web:9010;
    include uwsgi_params;
 }