Deploy Uvicorn: Nginx proxy and trusted forwarded headers
Uvicorn is an ASGI server for Python applications. For production on Linux, run it behind a proxy such as Nginx, bind it to a local interface, and explicitly choose the worker count and trusted forwarded-header peers. Honor X-Forwarded-For and X-Forwarded-Proto only when the connection comes from a trusted proxy. This guide covers one same-host Nginx proxy and a directly managed Uvicorn ASGI app. It excludes certificate issuance, WebSocket proxying, container networks, and Gunicorn integration.
Uvicorn is an ASGI server for Python applications. For production on Linux, run it behind a proxy such as Nginx, bind it to a local interface, and explicitly choose the worker count and trusted forwarded-header peers. Honor X-Forwarded-For and X-Forwarded-Proto only when the connection comes from a trusted proxy. This guide covers one same-host Nginx proxy and a directly managed Uvicorn ASGI app. It excludes certificate issuance, WebSocket proxying, container networks, and Gunicorn integration.
Request path and process command
Client -- HTTPS --> Nginx -- HTTP/loopback --> Uvicorn workers --> ASGI appNginx accepts public HTTPS and forwards requests to Uvicorn. Uvicorn defaults to host and port 127.0.0.1:8000. Keep loopback when Nginx runs on the same host; a public bind would break that isolation assumption.
Install Uvicorn and the ASGI app in the project virtual environment, then name the app module and object. Replace main:app and <worker-count> with project values.
python -m venv .venv
. .venv/bin/activate
python -m pip install uvicorn
uvicorn main:app --host 127.0.0.1 --port 8000 --workers <worker-count> --proxy-headers --forwarded-allow-ips 127.0.0.1The default worker count is 1, or the value of WEB_CONCURRENCY when set. Start small and tune with measured CPU, memory, latency, and application behavior. --workers cannot be combined with --reload; keep automatic reload for development. If calling uvicorn.run(..., workers=...) from Python, place it under if __name__ == "__main__": so multiprocessing can safely start workers.
Have Nginx set external request details
Set forwarding headers and the local upstream in a location inside the HTTPS server block that terminates TLS. This is a location example, not a complete 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 receives clients directly. Replacing X-Forwarded-For with $remote_addr does not preserve a client-supplied value. If a CDN or proxy is in front, first restrict Nginx real-IP trust to that proxy's actual IP ranges. Uvicorn only applies forwarded headers when the connecting peer is allowed.
--proxy-headers is enabled by default, but Uvicorn trusts only 127.0.0.1 and ::1 by default. --forwarded-allow-ips accepts individual IPs, CIDR networks, or a Unix socket path. * trusts every connection, so use it only if network controls prevent direct access from untrusted clients. For a Unix socket, check both the allowed socket path and file permissions.
Verify and restart safely
Confirm Uvicorn listens on `127.0.0.1:8000`, Nginx points to that upstream, and public networks cannot reach the app port directly.
On the host, run `curl -i http://127.0.0.1:8000/` to check the app's local response. This does not verify Nginx or public HTTPS.
Request the public HTTPS address and confirm the app sees the `https` scheme and correct client address. If the production domain, certificate, or proxy is not configured, do not claim this check passed.
On restart, Uvicorn gracefully closes connections and waits for tasks within its configured shutdown timeout. Whether a manager starts replacement workers first or shares a socket depends on deployment setup. After restart, check new process logs and real requests; restore the prior settings if errors appear.
Limits and troubleshooting
--limit-concurrency does not queue excess requests: once the connection or in-flight task limit is reached, new requests receive an immediate 503. --backlog configures a separate operating-system listen queue. If you set a concurrency limit, decide whether immediate 503 is intended; use a proxy queue or more workers when requests should wait for capacity.
**Nginx returns 502 or connection refused:** check Uvicorn's process, host, port, and Nginx upstream. Ensure a remote host or container is not trying to reach a loopback-only listener.
**The app sees the wrong client IP or scheme:** check that Nginx replaces forwarded headers and that the actual connecting peer is in `--forwarded-allow-ips`. Correct the proxy route and IP instead of broadening trust to `*`.
**Load causes 503 responses:** check `--limit-concurrency`; open keep-alive connections also count toward it. The setting is not a queue.
**Development reload conflicts with workers:** do not set `--reload` and `--workers` together. Use the deployment's separate process-management procedure for production workers.
Sources and limits
[Uvicorn deployment guide](https://uvicorn.dev/deployment/) — Nginx proxy, forwarded-header trust, Unix sockets, and graceful shutdown
[Uvicorn settings](https://uvicorn.dev/settings/) — bind defaults, worker settings, reload restriction, proxy headers, and concurrency options
[Uvicorn server behavior](https://uvicorn.dev/server-behavior/) — concurrency limit versus backlog and shutdown behavior
[Uvicorn release notes](https://uvicorn.dev/release-notes/) — confirmation that IPv6 loopback is included in the current default trusted IPs
Official docs were read on 2026-11-19. Commands and settings are illustrative and were not run. No specific app, Nginx configuration, worker count, network path, or zero-downtime restart was tested.