Django returns a 500 behind Gunicorn and the traceback is nowhere
500 Internal Server Error with no Python traceback in the logsShort 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.
Fix it fast
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)"2Match what you see, then jump to its fix
What does the error look like?
500 Internal Server Error with no Python traceback in the logsThe 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-pagerConfirmed by docs.djangoproject.com
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 ExecStart2Fix
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 debug3Verify
Send a request and read the error log.
tail -n 50 /var/log/gunicorn/error.logConfirmed by raw.githubusercontent.com
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:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
Related fixes
Servers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
cv2.imshow raises 'not implemented' because the wrong OpenCV package is installed
cv2.imshow() works in one environment and fails in another
The headless OpenCV packages are built without any GUI code, so cv2.imshow cannot open a window.
2 causes, each with a fixServers
pip install Cython succeeded, but the shell cannot find cythonize
a pip warning that a script was installed to a directory outside PATH
pip put the cythonize script in a folder your shell does not search.
3 causes, each with a fixServers
Docker build fails with apk: not found in a Python or Ubuntu image
/bin/sh: 1: apk: not found
apk is the package manager of Alpine Linux only.
2 causes, each with a fix