Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Django returns a 500 behind Gunicorn and the traceback is nowhere

Error500 Internal Server Error with no Python traceback in the logs

Short answer

With DEBUG set to False, Django's default logging does not print errors to the console. It only emails ADMINS, so Gunicorn has nothing to write. Add a LOGGING entry that sends the django logger to the console, and start Gunicorn with an error log file and a log level so you can read it.

  • Applies to Django 5.2, Gunicorn 23, nginx 1.26, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which log destination Gunicorn is using and whether DEBUG is off.

    systemctl cat gunicorn.service | grep ExecStart
    python manage.py shell -c "from django.conf import settings; print(settings.DEBUG)"
  2. 2Match what you see, then jump to its fix

    1. 1DEBUG prints False and the Gunicorn log only shows access linesDjango's default logging sends production errors to email, not the consoleFix
    2. 2Your print() output and library warnings are missing, or the log level hides messagesGunicorn is not writing, or not capturing, the output you expectFix

What does the error look like?

  • 500 Internal Server Error with no Python traceback in the logs
  • The traceback shows locally with runserver but not under Gunicorn

Cause 1 of 2

Django's default logging sends production errors to email, not the console

1Check

In Django's default configuration the console handler has a require_debug_true filter and mail_admins has require_debug_false. A 5XX response is logged at ERROR level, so with DEBUG off it goes to AdminEmailHandler and not to the terminal.

python manage.py shell -c "import logging; print(logging.getLogger('django').handlers)"

2Fix

Add a LOGGING setting that attaches a console handler to the django logger. Do not turn DEBUG on in production to get a traceback.

# settings.py
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {"console": {"class": "logging.StreamHandler"}},
    "loggers": {
        "django": {"handlers": ["console"], "level": "INFO"},
        "django.request": {"handlers": ["console"], "level": "ERROR", "propagate": False},
    },
}

3Verify

Restart Gunicorn, trigger the failing request, and the traceback appears in the Gunicorn error log.

sudo systemctl restart gunicorn
journalctl -u gunicorn -n 50 --no-pager

Confirmed by docs.djangoproject.com

Did this fix it?

Cause 2 of 2

Gunicorn is not writing, or not capturing, the output you expect

1Check

Gunicorn's error log goes to stderr by default. Access logging is off by default. Standard output and error of your app are redirected into the error log only when capture_output is on.

systemctl cat gunicorn.service | grep ExecStart

2Fix

Give Gunicorn explicit log options. Use --log-level debug while you investigate and go back to info afterwards.

gunicorn myproject.wsgi:application \
  --error-logfile /var/log/gunicorn/error.log \
  --access-logfile - \
  --capture-output \
  --log-level debug

3Verify

Send a request and read the error log.

tail -n 50 /var/log/gunicorn/error.log

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Keep DEBUG = False in production and rely on logging, not the debug page.
  • Send logs to one place that survives restarts, and rotate that file.
  • Set ADMINS and email settings, or use an error tracker, so server errors reach someone.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.