Django Static Files Not Loading in Production (DEBUG=False) with Gunicorn on Kubernetes

Problem

I'm running a Django application in a Kubernetes pod with Gunicorn, and my static files (admin panel CSS/JS) are not loading when DEBUG=False.

In local development, I use runserver, and everything works fine with DEBUG=True. However, when I set DEBUG=False, my static files return 404.

Error Logs (Browser Console / Django Logs)

GET /static/admin/css/base.efb520c4bb7c.css HTTP/1.1" 404 179
GET /static/admin/js/nav_sidebar.7605597ddf52.js HTTP/1.1" 404 179

Relevant Django Settings (settings.py)

DEBUG = False

INSTALLED_APPS = [
    "django.contrib.staticfiles",
    # Other apps...
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # Other middleware...
]


BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATICFILES_DIRS = [
    BASE_DIR / 'static',  
]
STATIC_ROOT = BASE_DIR / 'staticfiles'

How I Run My App (Docker Entrypoint)

#!/bin/sh

APP_PORT=${PORT:-8000}

echo "Migrating database..."
/opt/venv/bin/python manage.py migrate --noinput

echo "Collecting static files..."
/opt/venv/bin/python manage.py collectstatic --noinput
echo "Static files collected"

echo "Starting server..."
/opt/venv/bin/gunicorn secureuri.wsgi:application --bind "0.0.0.0:${APP_PORT}" --workers 1 --access-logfile - --error-logfile - --log-level debug

What I Tried

  • Checked that collectstatic is running.

  • Running ls -la staticfiles/admin/css/ shows that files exist inside the container. Tried running Django’s built-in dev server with DEBUG=False

  • Running python manage.py runserver --insecure does not work

  • Checked Gunicorn Logs

  • Gunicorn is running fine, and there are no errors related to static files.

  • Tried adding whitenoise ❌ (Didn’t work)

INSTALLED_APPS = [
    "whitenoise.runserver_nostatic",
    "django.contrib.staticfiles",
]

MIDDLEWARE = [
    "whitenoise.middleware.WhiteNoiseMiddleware",
]

  • Restarted the pod, but static files still return 404.

Question

  1. How can I serve static files correctly when DEBUG=False without using an external service like Nginx or S3?
  2. Is there something I’m missing in my Gunicorn/Kubernetes setup?
Вернуться на верх