Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda