Topic · Serving the app

Nginx & reverse proxies for Django in production

Nginx, Caddy, Traefik, Apache, and CDNs in front of Django: routing, buffering, limits, and the status codes they produce.

Nginx decides who answersRequesthttps://NginxTLS ends here/static/, /media/served from diskeverything elseproxy to Gunicorn socketHeaders, timeouts, and body limits here decide much of Django’s behaviour

Nginx sits in front of Django in most self-hosted deployments. It terminates TLS, serves static files, buffers slow clients, and passes the headers Django relies on to know the host and scheme.

Many Django errors are really proxy errors: 502s, redirect loops, 413s, and wrong client IPs. The guides here cover configuring Nginx well and reading what it tells you.

22Set it up
9Run it well
2Check it
37Fix it

Set it up

Guides that take this from nothing to running in production.

  • Deploy Django with Caddy and Automatic HTTPS

    If you want to deploy Django with Caddy in production, the main challenge is not just putting a reverse proxy in front of the app.

  • How to Configure Nginx for Django in Production

    A production Django stack should not expose Gunicorn or Uvicorn directly to the internet.

  • Deploy Django Behind Cloudflare

    Put Django behind Cloudflare correctly: Full (strict) TLS, origin lockdown, real client IPs, HTTPS detection, cache rules, and request limits.

  • Deploy Django with Traefik and Docker

    Put Traefik in front of a Dockerized Django app: automatic Let's Encrypt certificates, HTTP to HTTPS redirects, Docker labels for routing, static files, and upload limits.

  • Expose Django with Cloudflare Tunnel

    Serve a Django app through Cloudflare Tunnel with no open inbound ports: cloudflared setup, ingress rules, DNS routing, real client IPs, HTTPS settings, and protecting the admin.

  • Serve Django Under a URL Prefix (/app) Behind Nginx

    Run Django under a subpath such as example.com/app behind Nginx: forward SCRIPT_NAME correctly, set FORCE_SCRIPT_NAME, STATIC_URL, and cookie paths, and avoid broken admin links, redirects, and CSRF errors.

  • Load Balance Django With HAProxy

    Put HAProxy in front of several Django app servers: active health checks on a /health/ endpoint, TLS termination, X-Forwarded-Proto, connection limits per server, rolling deploys with drain, and stick tables for basic rate limiting.

  • Nginx vs Caddy for Django

    Compare Nginx and Caddy as the reverse proxy for Django: automatic HTTPS versus Certbot, configuration size, performance, static files, ecosystem and documentation, on-demand TLS for tenants, and which suits your deployment.

  • Deploy Django Behind Nginx Proxy Manager

    Put a Docker-based Django app behind Nginx Proxy Manager: create a proxy host with Let’s Encrypt, connect containers on a shared network, forward the right headers, serve static files, allow large uploads, and keep the admin UI private.

  • Deploy Django with Gunicorn and Nginx on Ubuntu

    Moving a Django app from local development to production usually fails on the same points: process management, reverse proxy configuration, static files, secrets, TLS, and safe...

  • Deploy Django with uWSGI and Nginx

    Run Django with uWSGI and Nginx: a strict uWSGI ini, socket permissions, systemd integration, graceful reloads, harakiri timeouts, and checks.

  • Deploy Django with Apache and mod_wsgi

    Deploy Django on Apache with mod_wsgi daemon mode, a matching virtualenv, static file aliases, secret loading, HTTPS, and code reloads without restarting Apache.

  • Host Multiple Django Sites on One Server

    Run several Django projects on one server safely: separate users and virtualenvs, systemd templates, per-site databases, and Nginx blocks.

  • Deploy Multi-Tenant Django with django-tenants

    Deploy a schema-per-tenant Django SaaS with django-tenants: wildcard DNS and TLS, Nginx for subdomains, migrate_schemas in releases, tenant-aware caching, media, Celery, and pooling.

  • Serve Private Django Media Files with Nginx X-Accel-Redirect

    Serve permission-checked Django downloads efficiently: Django authorises the request and returns X-Accel-Redirect, and an internal Nginx location streams the file without tying up workers.

  • Serve Django API and Admin on Separate Subdomains

    Serve one Django project on www, api, and admin subdomains: per-host URLconfs, host-only cookies, CORS and CSRF settings, Nginx server blocks, and restricting admin access by network.

  • Run Django Behind an AWS Application Load Balancer

    Configure Django correctly behind an AWS Application Load Balancer: HTTPS listeners with ACM, X-Forwarded-Proto and client IPs, ALLOWED_HOSTS for health checks, idle and keepalive timeouts, stickiness, and deregistration delay for deploys.

  • Password-Protect a Django Staging Site

    Keep a Django staging site private: HTTP basic auth in Nginx or Caddy with health-check and webhook exceptions, Django middleware for PaaS hosts, noindex headers, IP or VPN restrictions, and why basic auth interferes with API tokens.

  • Custom Domains With Automatic HTTPS for Django Tenants

    Let SaaS customers point their own domains at a multi-tenant Django app with automatic HTTPS: Caddy on-demand TLS with an ask endpoint in Django, domain verification, tenant lookup by Host, and protecting against abuse and rate limits.

  • Expose Django on Kubernetes With Gateway API or Ingress

    Route HTTPS traffic to Django on Kubernetes with the Gateway API or a maintained Ingress controller: HTTPRoutes or Ingress rules, TLS with cert-manager, forwarded headers, body size and timeouts, and moving off the retired ingress-nginx.

  • Protect Django Endpoints With mTLS Client Certificates in Nginx

    Require mutual TLS client certificates for Django admin, internal APIs, or partner integrations with Nginx: create a private CA, issue client certificates, configure ssl_verify_client, pass verified identity to Django, and handle revocation and renewal.

  • Put Varnish Cache in Front of Django

    Use Varnish as an HTTP cache in front of Django: send correct Cache-Control headers from views, write VCL that bypasses logged-in users and POST requests, strip tracking cookies, purge or ban pages on content changes, and run Varnish with Nginx for TLS.

Run it well

Tuning, reliability, and operations once it works.

  • Cache Django Responses with Nginx proxy_cache

    Use Nginx proxy_cache in front of Django safely: cache zones, bypassing logged-in users, respecting Cache-Control, stale serving, and purging.

  • Rate Limit Django Endpoints with Nginx

    Protect Django login, API, and expensive endpoints with Nginx limit_req: zones, burst and nodelay, 429 responses, real client IPs, and dry runs.

  • Enable HTTP/2 and HTTP/3 for Django with Nginx

    Serve a Django site over HTTP/2 and HTTP/3 (QUIC) with Nginx: version-specific directives, Alt-Svc, UDP 443, keeping HTTP/1.1 to Gunicorn, verifying with curl, and what actually gets faster.

  • Tune Nginx Keepalive and Worker Connections for Django

    Tune Nginx for a Django backend: worker_processes and worker_connections, client keepalive_timeout, upstream keepalive connection pools to Gunicorn or Uvicorn, proxy buffering, and file descriptor limits, with when each setting matters.

  • Enable Gzip and Brotli Compression for Django with Nginx

    Compress Django responses and static files correctly: Nginx gzip settings, precompressed static assets, Brotli support, BREACH caveats, and checks.

  • Django Maintenance Mode During Deploys

    Put a Django site into maintenance mode safely: an Nginx flag file returning 503 with Retry-After, a static maintenance page, staff IP bypass, health checks, and when to avoid maintenance windows.

  • Set Cache-Control Headers in Django for CDNs and Browsers

    Control how browsers and CDNs cache Django responses: cache_control and never_cache decorators, public versus private, max-age versus s-maxage, Vary headers from sessions and cookies, ETags, and safe defaults for logged-in pages.

  • Cache Django Pages at Cloudflare's Edge

    Cache Django HTML at Cloudflare’s edge safely: Cache Rules for anonymous pages, bypassing cache when session or CSRF cookies exist, respecting origin Cache-Control, purging on deploy or content change, and avoiding cached private pages.

  • Custom 404 and 500 Error Pages for Django in Production

    Create custom 404, 500, 403, and 400 pages for a Django site in production: templates and handler views, a 500 page that cannot fail, static assets that load, Nginx error pages for when Django is down, and testing them with DEBUG off.

Check it

Review lists to run before launch or after a change.

  • Nginx Production Checklist for Django

    A production checklist for Nginx in front of Django: server blocks and redirects, TLS, proxy headers, static and media serving, body size and timeouts, buffering, compression, security headers, logging, and safe reloads.

  • Django Load Balancer Configuration Checklist

    A checklist for putting Django behind a load balancer: health checks that pass host validation, forwarded headers and SECURE_PROXY_SSL_HEADER, correct client IPs, idle and request timeouts, connection draining, sticky sessions avoided, and TLS policy.

Fix it

Errors and symptoms, each traced to a cause and a fix.

Learning paths through this topic

Related topics