Deploy Gunicorn behind Nginx: workers and trusted HTTPS headers
Gunicorn is a process manager and application server for Python WSGI applications. In production, place it behind a front proxy such as Nginx and keep Gunicorn on a local or private interface. A synchronous worker handles one request at a time, so the proxy must buffer slow clients; and when Gunicorn reads the original HTTPS scheme from forwarded headers, limit which proxy IPs it trusts. This procedure follows the current Gunicorn docs for a Linux WSGI app such as Django or Flask behind one Nginx proxy. It does not cover ASGI, issuing TLS certificates, or complete service files for a particular system.
Gunicorn is a process manager and application server for Python WSGI applications. In production, place it behind a front proxy such as Nginx and keep Gunicorn on a local or private interface. A synchronous worker handles one request at a time, so the proxy must buffer slow clients; and when Gunicorn reads the original HTTPS scheme from forwarded headers, limit which proxy IPs it trusts. This procedure follows the current Gunicorn docs for a Linux WSGI app such as Django or Flask behind one Nginx proxy. It does not cover ASGI, issuing TLS certificates, or complete service files for a particular system.
Choose the request path and worker
Client -- HTTPS --> Nginx -- HTTP/loopback --> Gunicorn sync workers --> WSGI appNginx handles public TLS and slow client connections; Gunicorn handles application requests. Sync workers are simple and predictable, but requests wait while a worker handles another request. Gunicorn recommends checking proxy buffering in production so slow clients do not hold workers for too long.
For ordinary synchronous Django or Flask apps, start small with `sync`; adjust worker count using measured load and latency. CPU-based formulas are starting points, not universal recommendations.
Consider `gthread` when the app benefits from threads, keep-alive, or mixed I/O. Threads share process memory, so check that the app and its libraries are thread-safe.
Async workers such as `gevent` may help with WebSockets or long polling, but check blocking calls and library compatibility. ASGI frameworks use a different calling convention; use an appropriate ASGI worker and guide.
Bind Gunicorn to a local interface
Install Gunicorn in the project virtual environment and provide the app's WSGI entry point. Replace myproject.wsgi:application and <worker-count> with project-specific values.
python -m venv .venv
. .venv/bin/activate
python -m pip install gunicorn
gunicorn --bind 127.0.0.1:8000 --workers <worker-count> myproject.wsgi:applicationThe default bind is 127.0.0.1:8000, but when PORT is set the default may change to 0.0.0.0:$PORT. Check environment variables injected by the deployment platform. If only same-host Nginx should connect, specify loopback explicitly. If the proxy and app are in separate containers, each container's 127.0.0.1 refers to itself. Choose an internal container-network address and firewall rules; do not expose the upstream port publicly.
Make Nginx replace forwarded headers
In the HTTPS server block that terminates TLS, set the forwarding headers and upstream in the location. This is a location example, not a complete TLS server configuration.
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8000;
}This assumes one public Nginx directly receives clients. Setting X-Forwarded-For to $remote_addr replaces a client-supplied value instead of appending it. If a CDN or another proxy sits in front, first configure Nginx real-IP handling to trust only that proxy's actual IP ranges, then forward the resulting client address. Trusting arbitrary incoming headers can falsify IP logs, access controls, and HTTPS detection.
Gunicorn uses headers such as X-FORWARDED-PROTO: https and X-FORWARDED-SSL: on to set wsgi.url_scheme, but only when they come from an allowed frontend IP. The default --forwarded-allow-ips is 127.0.0.1,::1; IPs and CIDRs can be specified. The default may fit same-host Nginx over loopback. For containers or another host, set a narrow range matching the actual peer address. * disables IP checks; use it only when other controls ensure no unapproved proxy can reach Gunicorn. Unix-socket connections do not use the IP check, so restrict access with socket-file permissions.
Verify and reload safely
Check that Gunicorn listens on the intended address and port and that public networks cannot connect directly to the upstream.
From the same host, run `curl -i http://127.0.0.1:8000/` to check the local app path. This does not test the public proxy path.
Request the public HTTPS address and check the response, redirects and secure cookies based on the `https` scheme, and client-address logs. If the production domain or certificate is not configured, this check cannot be performed.
To apply settings, send `HUP` to the running master: it loads settings and starts new workers while old ones stop gracefully. Whether application code is reloaded depends on `preload_app`. This does not guarantee zero downtime; confirm new workers and real requests, and restore the previous configuration if needed.
For shutdown, `TERM` gives workers a chance to finish current requests, but they may be stopped after `graceful_timeout`. `QUIT` and `INT` are quick shutdown signals, not the usual graceful stop.
Diagnose common problems
**Nginx returns 502 or a connection error:** check the Gunicorn process, bind address, port, and Nginx upstream. For separate containers, check the internal address and network.
**All workers are busy during slow requests:** account for sync-worker concurrency and check Nginx buffering for slow clients. Adding workers also increases memory use and CPU contention.
**The app sees an HTTPS request as HTTP:** check that Nginx sets `X-Forwarded-Proto` to the external scheme and that Gunicorn allows the proxy's actual IP. Correct the peer address instead of widening trust to `*`.
**Client IP is wrong:** restrict networks that can connect directly to Gunicorn; check whether the proxy removes and replaces headers; and compare `forwarded_allow_ips` with the actual peer.
**Old code remains after reload:** check `preload_app` and HUP behavior, then inspect new-worker logs and the app response. Restore the prior settings and version if necessary.
Sources and limitations
[Gunicorn deployment guide](https://gunicorn.org/deploy/) — proxy recommendation, buffering for sync workers, and deployment setup
[Gunicorn design](https://gunicorn.org/design/) — pre-fork model, worker types, and selection considerations
[Gunicorn settings reference](https://gunicorn.org/reference/settings/) — bind defaults, worker count, trusted IPs, and secure-scheme headers
[Gunicorn signals](https://gunicorn.org/signals/) — `HUP`, `TERM`, worker-count adjustment, and reload limits
The official docs were checked on 2026-11-19. Commands and settings are illustrative and were not run. This article does not claim that a particular WSGI path, worker count, Nginx TLS setup, proxy address or buffering, network isolation, or reload result was verified.