Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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

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

#!/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.

Now I added nginx configuration as

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.

But when I try to open admin page then I am not able to see static files there.

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 ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

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

nginx.conf

...
location /home/ec2/static/ {
        autoindex on;
        alias /static/;
    }
...
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda