Python / Uvicorn Fundamentals Interview Questions
How do you configure SSL/TLS (HTTPS) in Uvicorn?
Uvicorn supports HTTPS natively by accepting a certificate and private key. This is useful for local development with HTTPS or for simple single-server deployments where a reverse proxy is not involved.
# Run with SSL (requires certificate and private key) uvicorn main:app \ --ssl-keyfile ./key.pem \ --ssl-certfile ./cert.pem \ --host 0.0.0.0 \ --port 443 # With a CA bundle (for mutual TLS / client certificates) uvicorn main:app \ --ssl-keyfile ./key.pem \ --ssl-certfile ./cert.pem \ --ssl-ca-certs ./ca-bundle.pem # Programmatic SSL config import ssl import uvicorn if __name__ == "__main__": uvicorn.run( "main:app", ssl_keyfile="./key.pem", ssl_certfile="./cert.pem", host="0.0.0.0", port=443, ) # Local development HTTPS with mkcert: # 1. Install mkcert: brew install mkcert # 2. mkcert -install # 3. mkcert localhost 127.0.0.1 # 4. uvicorn main:app --ssl-keyfile localhost-key.pem --ssl-certfile localhost.pem
Production approach: in most production architectures, SSL termination is handled by a reverse proxy (Nginx, Caddy, or a cloud load balancer) rather than by Uvicorn directly. The reverse proxy decrypts HTTPS traffic and forwards plain HTTP to Uvicorn. This pattern improves performance and simplifies certificate management.
More Related questions...