
FastAPI
Production servers require different configuration than development. Here's how to set up FastAPI for production environments.
Development:
uvicorn main:app --reload --host 127.0.0.1 --port 8000Production:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:8000Install:
pip install gunicornRun:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:appThe `-w 4` creates 4 worker processes for handling concurrent requests.
workers = (2 × CPU_cores) + 1Example:
Create `gunicorn_config.py`:
import multiprocessing
workers = (multiprocessing.cpu_count() * 2) + 1
worker_class = "uvicorn.workers.UvicornWorker"
bind = "0.0.0.0:8000"
timeout = 120
keepalive = 5
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(D)s'Run with config:
gunicorn -c gunicorn_config.py main:appDockerfile:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "main:app", "--bind", "0.0.0.0:8000"]Build and run:
docker build -t fastapi-app .
docker run -p 8000:8000 fastapi-appupstream uvicorn {
server localhost:8000;
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://uvicorn;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}from pydantic import BaseSettings
class Settings(BaseSettings):
debug: bool = False
database_url: str
secret_key: str
class Config:
env_file = ".env"
settings = Settings().env file:
DEBUG=false
DATABASE_URL=postgresql://user:password@db:5432/mydb
SECRET_KEY=your-secret-key-here@app.get("/health")
async def health_check():
return {"status": "healthy"}Docker healthcheck:
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s \
CMD curl -f http://localhost:8000/health || exit 1import time
from starlette.middleware.base import BaseHTTPMiddleware
class TimingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
start = time.time()
response = await call_next(request)
duration = time.time() - start
response.headers["X-Process-Time"] = str(duration)
return response
app.add_middleware(TimingMiddleware)Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward