Skip to the page

Python services in production · mid

Nginx 502 and 504, proxy settings, static files and debug pages

Behind a proxy, each layer has its own timeout and its own view of the request. Wrong order of timeouts, a missing forwarded-protocol header or a missing collectstatic step gives errors that look random.

The notes

What you must know

  • 502: Nginx got no valid response from the upstream (connection refused, reset, closed early). Causes: a worker killed by Gunicorn timeout or the OOM killer, or a pod shutting down. Read the exact Nginx error log and Gunicorn's log (WORKER TIMEOUT, SIGKILL).
  • 504: the upstream did not answer within proxy_read_timeout (60 s by default). A 75-second report fails at 60 s while Gunicorn keeps a worker busy for nothing.
  • Rollout 502s: on SIGTERM Gunicorn stops accepting, but Nginx still sends requests for a few seconds. Use a readiness probe, a preStop sleep of 10 to 15 seconds and a longer graceful timeout. Setting 600 seconds everywhere only lets slow requests hold workers.
  • Order of timeouts: database statement_timeout < app and HTTP-client timeouts < Gunicorn timeout < Nginx proxy_read_timeout < load balancer idle timeout, so the app fails first with a clear error. Fix the slow report with an index, then a background job that writes to S3 and sends a link.
  • New domain: 400 is DisallowedHost (add the exact hosts to ALLOWED_HOSTS). Behind a load balancer that ends HTTPS, Django sees HTTP, so CSRF Origin checks fail and SECURE_SSL_REDIRECT loops. Set SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") only if the load balancer always sets and overwrites the header. Add CSRF_TRUSTED_ORIGINS with the scheme.
  • Also set SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE and start HSTS small. Test on a staging copy behind the same kind of load balancer and run manage.py check --deploy.
  • No CSS and 500s after a release: runserver serves static files only with DEBUG = True. In production, collectstatic copies files to STATIC_ROOT and WhiteNoise, Nginx or a CDN serves them. A manifest storage raises ValueError for a file missing from the manifest. Run collectstatic --noinput in the build and keep staging with DEBUG = False.
  • A Django debug page in production: set DEBUG = False first, rotate every secret and connection string shown (DATABASE_URL, SECRET_KEY with SECRET_KEY_FALLBACKS), parse booleans carefully ("false" is not "False"), add a startup check against DEBUG in production, and run manage.py check --deploy in CI.

The red pen

Common traps: mistake → fix

  • Mistake: 502 and 504 mean the same thing

    Fix: 502 is a bad or no response, 504 is a timeout.

  • Mistake: Raise every timeout to 600 s

    Fix: workers stay busy and a few clicks take the site down.

  • Mistake: Trust X-Forwarded-Proto always

    Fix: only if the load balancer sets and overwrites it.

  • Mistake: Staging worked, so production will

    Fix: staging with DEBUG = True hides static files and error pages.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between a 502 and a 504 from Nginx?
  2. Why do rollouts cause 502s and how do you stop them?
  3. How should the timeouts at each layer be ordered?
  4. After a move to a new domain behind a load balancer there are 400s, CSRF failures and a redirect loop. Why?
  5. A researcher sends a screenshot of a Django debug page from production. What do you do first?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview