简体   繁体   English

无法在 ec2 上使用 ZE434023CF89D7DFB21F63D64F0F9D74Z 提供 static 文件,其中 django 和 gunicorn 在 Z05B6053C451A21DAEAB6Z6 容器内运行

[英]not able to serve static files with nginx on ec2 with django and gunicorn running inside docker container

I am using the below docker compose 'local.yml' to run django on ec2 server我正在使用下面的 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

it starts with start.sh script which is written as它以 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

On ec2 after deployment, server is running fine with gunicorn.在部署后的 ec2 上,服务器使用 gunicorn 运行良好。

Now I added nginx configuration as现在我将 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;
    }


}

This configuration is also running fine and I can access it locally on example.in:3000.此配置也运行良好,我可以在 example.in:3000 上本地访问它。

But when I try to open admin page then I am not able to see static files there.但是当我尝试打开管理页面时,我无法在那里看到 static 文件。

Also I tried to collectstatic with the below command我也尝试使用以下命令收集静态

docker-compose -f local.yml run --rm django python manange.py collectstatic --no-input

The files are collected successfully.文件收集成功。

What should i do to serve the static files?我应该怎么做才能提供 static 文件?

Map your /app/static/ folder from the container into a folder on the host, lets say: /home/ec2/static/ and make sure your nginx has access there. Map 您的/app/static/文件夹从容器到主机上的文件夹中,比如说: /home/ec2/static/并确保您的 nginx 可以访问那里。

volumes:
  - /home/ec2/static/:/app/static

nginx.conf nginx.conf

...
location /home/ec2/static/ {
        autoindex on;
        alias /static/;
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM