Script Valley
FastAPI: Build Production Python APIs
Production DeploymentLesson 6.5

How to deploy FastAPI with Gunicorn and Uvicorn workers in production

Gunicorn UvicornWorker, worker count formula, process manager, graceful shutdown, Nginx reverse proxy, SSL termination, keep-alive, production checklist

Gunicorn with Uvicorn Workers

Uvicorn alone is a single process. In production, Gunicorn acts as a process manager, spawning multiple Uvicorn workers to use all CPU cores and enable zero-downtime restarts.

Install and run

pip install gunicorn uvicorn[standard]

gunicorn app.main:app \
    --workers 4 \
    --worker-class uvicorn.workers.UvicornWorker \
    --bind 0.0.0.0:8000 \
    --timeout 60 \
    --keep-alive 5 \
    --access-logfile -

Worker count formula: (2 × CPU cores) + 1. For a 2-core server, use 5 workers. Too many workers increases memory usage without improving throughput.

Nginx reverse proxy (minimal config)

server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Nginx handles SSL termination, static file serving, and connection buffering — offloading work from Python processes. Never expose Uvicorn or Gunicorn directly to the internet without a reverse proxy.

Graceful reload

kill -HUP $(cat gunicorn.pid)

HUP signal triggers a graceful reload — workers finish in-flight requests before restarting with new code. No dropped connections during deployments.