首页 > 解决方案 > Nginx 没有在 docker 上正确地提供静态文件?

问题描述

我希望 nginx 充当反向代理并为 django (api) 提供静态文件。

码头工人-compose.yml:

version: '3'

services:
  api:
    build: api
    container_name: api
    volumes:
      - static_volume:/home/admin/api/static
    ports:
      - "8000:8000"
    env_file:
      - ./config/api/.env
    depends_on:
      - db
  
  db:
    image: postgres:latest
    container_name: db 
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./config/api/.env-db
  
  nginx:
    image: nginx:latest
    container_name: ngx
    volumes:
      - static_volume:/home/admin/api/static
      - ./config/nginx:/etc/nginx/conf.d
    ports:
      - "80:80"
    depends_on:
      - api

volumes:
  postgres_data:
  static_volume:

配置/nginx/default.conf:

upstream refridge {
    server api:8000;
}

server {
    listen 80;

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

    location /static/ {
        autoindex on;
        alias /home/admin/api/static/;
    }
}

docker-compose up -d --build使用 django 的 migrate 和 collectstatic 命令启动并运行它们之后,我在尝试访问 django 的管理页面时看到以下日志:

码头工人日志API

Waiting for postgres...
PostgreSQL started
[2020-08-17 20:26:04 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-08-17 20:26:04 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2020-08-17 20:26:04 +0000] [1] [INFO] Using worker: sync
[2020-08-17 20:26:04 +0000] [30] [INFO] Booting worker with pid: 30
[2020-08-17 20:26:04 +0000] [31] [INFO] Booting worker with pid: 31
[2020-08-17 20:26:04 +0000] [32] [INFO] Booting worker with pid: 32
Not Found: /static/admin/css/responsive.css
Not Found: /static/admin/css/login.css
Not Found: /static/admin/css/base.css

码头工人日志 ngx

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

为什么 api docker 容器在 /static 中查找静态文件?我相信将静态目录设置为 /home/admin/api/static。任何帮助,将不胜感激!

标签: djangodockernginxdeployment

解决方案


推荐阅读