django - 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。任何帮助,将不胜感激!
解决方案
推荐阅读
- arrays - 如何使用 hive 查询查找 hive json 数组字段的长度
- node.js - 如何在 React 中重定向到另一个视图
- aws-cli - 无法通过 aws cli 向 Patch Group 注册 Patch baselineid
- python - 使布局小部件浮动在主窗口pyqt5之外
- python-3.x - 从文本文件创建列表列表,其中新列表基于条件
- python - 最小化成本
- image - 如何计算 Keras UpSampling2D 层的输出?
- python-3.x - user.is_authenticated DetailView 为 True
- python - 使用 selenium 进行动态网页抓取
- javascript - 单击搜索结果时,App.vue 中未打开弹出窗口