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

921
Vistas
How can I serve Django static files via nginx Docker container?

During development I like to deploy Django static files in an as close to production as possible setup. To achieve this I wrap the Django backend into an image (Dockerfile) and frontend JS and backend Django static files into another image together with nginx configured as webserver (Dockerfile_nginx). The setup is as follows:

File system structure:

<projekt-repo>
  /frontend
  /backend
    /static (generated with python manage.py collectstatic)
    settings.py
  manage.py
  nginx.conf
  Dockerfile
  Dockerfile_nginx

settings.py:

STATIC_ROOT = os.path.join(BASE_DIR, "backend/static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

nginx.conf:

server {
  listen 0.0.0.0:8080;
  root /var/www;
  location / {
    try_files $uri $uri/ /index.html;
  }
}

server {
  listen 0.0.0.0:8000;
  root /var/www/django;
  location /static/ {
    autoindex on;
    alias /var/www/django/static/;
  }
}

Dockerfile_nginx:

FROM nginx:1.17.8-alpine
COPY nginx.conf /etc/nginx/conf.d/nginx.conf
COPY edge_frontend/www /var/www
COPY edge_backend/static /var/www/django/static

If I run the application and try to login via the Django admin site (localhost:8000/admin) the site is not styled properly and the log output states

backend  | Not Found: /static/admin/css/base.css
backend  | Not Found: /static/admin/css/login.css
backend  | Not Found: /static/admin/css/responsive.css
backend  | Not Found: /favicon.ico
backend  | Not Found: /static/admin/css/base.css
backend  | Not Found: /static/admin/css/login.css
backend  | Not Found: /static/admin/css/responsive.css
backend  | Not Found: /favicon.ico

Obviously there is some mismatch in the setup which I am not able to spot right now? Can someone help?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

create a folder in your project with the name "static" and then

add in your settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

or you want another path?

over 4 years ago · Santiago Trujillo Denunciar

0

You have to set BASE_DIR, STATIC_URL and STATIC_ROOT in settings.py as below...

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'

And run python manage.py collectstatic

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