我正在使用下面的 docker compose 'local.yml' 在 ec2 服務器上運行 django
services:
django: &django
build:
context: .
dockerfile: ./compose/local/django/Dockerfile
image: name_docker
container_name: django
depends_on:
- mariadb
- mailhog
volumes:
- .:/app:z
env_file:
- ./.envs/.local/.django
- ./.envs/.local/.mariadb
oom_kill_disable: True
deploy:
resources:
limits:
cpus: '0.50'
memory: '3G'
ports:
- "8000:8000"
command: /start
它以 start.sh 腳本開頭,腳本如下
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
# python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:8000 --timeout 10000 --workers 5 --threads 5 --chdir=/app
在部署后的 ec2 上,服務器在使用 gunicorn 時運行良好。
現在我將 nginx 配置添加為
server {
listen 3000;
server_name domain_name.in;
access_log /var/log/nginx/django_project-access.log;
error_log /var/log/nginx/django_project-error.log info;
add_header 'Access-Control-Allow-Origin' '*';
keepalive_timeout 5;
# path for staticfiles
location /static {
autoindex on;
alias /static;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://0.0.0.0:8000;
}
}
此配置也運行良好,我可以在 example.in:3000 上本地訪問它。
但是當我嘗試打開管理頁面時,我看不到那里的靜態檔案。
我也嘗試使用以下命令收集靜態
docker-compose -f local.yml run --rm django python manange.py collectstatic --no-input
檔案已成功收集。
我應該怎么做才能為靜態檔案提供服務?
uj5u.com熱心網友回復:
將您的/app/static/檔案夾從容器映射到主機上的檔案夾,讓我們說:/home/ec2/static/并確保您的 nginx 可以訪問那里。
volumes:
- /home/ec2/static/:/app/static
組態檔
...
location /home/ec2/static/ {
autoindex on;
alias /static/;
}
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382104.html
標籤:姜戈 码头工人 nginx docker-compose 独角兽
