The lightning-fast ASGI server.
Complete command-line interface for running uvicorn from the terminal.
from uvicorn.main import mainuvicorn [OPTIONS] APPAPP ASGI application import string (e.g., "myapp:app")The application can be specified as:
myapp:app (imports app from myapp module)myapp:create_app with --factory flagmyapp.wsgi:applicationdef main() -> None:
"""
Main CLI entry point decorated with Click.
Parses command-line arguments and runs the ASGI server.
All options are passed to the run() function.
Exit codes:
- 0: Normal exit
- 3: Startup failure (STARTUP_FAILURE constant)
"""--host TEXTBind socket to this host. (default: 127.0.0.1)
--port INTEGERBind socket to this port. (default: 8000)
--uds TEXTBind to Unix domain socket at this path.
--fd INTEGERBind to socket from this file descriptor.
--reloadEnable auto-reload on file changes. (default: disabled)
--reload-dir PATHSet directories to watch for file changes. Can be specified multiple times. (default: current directory)
--reload-include TEXTSet glob patterns to include for reload watching. Can be specified multiple times.
--reload-exclude TEXTSet glob patterns to exclude from reload watching. Can be specified multiple times.
--reload-delay FLOATDelay in seconds between checking for file changes. (default: 0.25)
--workers INTEGERNumber of worker processes to run. (default: 1)
--loop [none|auto|asyncio|uvloop]Event loop implementation. (default: auto)
auto: Automatically select uvloop if available, otherwise asyncioasyncio: Use asyncio event loopuvloop: Use uvloop (requires uvloop package)none: No event loop configuration--http [auto|h11|httptools]HTTP protocol implementation. (default: auto)
auto: Automatically select httptools if available, otherwise h11h11: Use h11 pure-Python implementationhttptools: Use httptools (requires httptools package)--ws [auto|none|websockets|websockets-sansio|wsproto]WebSocket protocol implementation. (default: auto)
auto: Automatically select websockets if availablenone: Disable WebSocket supportwebsockets: Use websockets librarywebsockets-sansio: Use websockets sans-IO implementationwsproto: Use wsproto library--ws-max-size INTEGERMaximum WebSocket message size in bytes. (default: 16777216)
--ws-max-queue INTEGERMaximum WebSocket message queue length. (default: 32)
--ws-ping-interval FLOATWebSocket ping interval in seconds. (default: 20.0)
--ws-ping-timeout FLOATWebSocket ping timeout in seconds. (default: 20.0)
--ws-per-message-deflate / --no-ws-per-message-deflateEnable/disable WebSocket per-message deflate compression. (default: enabled)
--lifespan [auto|on|off]Lifespan event handling mode. (default: auto)
auto: Enable lifespan if application supports iton: Force lifespan events (error if not supported)off: Disable lifespan events--interface [auto|asgi3|asgi2|wsgi]Application interface type. (default: auto)
auto: Automatically detect interface typeasgi3: ASGI3 interfaceasgi2: ASGI2 interface (class-based)wsgi: WSGI interface--log-config PATHLogging configuration file path. Supports .json, .yaml, and .ini formats.
--log-level [critical|error|warning|info|debug|trace]Logging level. (default: info)
--access-log / --no-access-logEnable/disable access logging. (default: enabled)
--use-colors / --no-use-colorsEnable/disable colored logging output. (default: auto-detect)
--proxy-headers / --no-proxy-headersEnable/disable proxy header parsing (X-Forwarded-*). (default: enabled)
--server-header / --no-server-headerEnable/disable Server header in responses. (default: enabled)
--date-header / --no-date-headerEnable/disable Date header in responses. (default: enabled)
--forwarded-allow-ips TEXTComma-separated list of IPs to trust for proxy headers. (default: 127.0.0.1)
--header TEXTAdd custom default header in Name:Value format. Can be specified multiple times.
--root-path TEXTSet ASGI root_path for sub-mounted applications. (default: "")
--app-dir TEXTAdd directory to PYTHONPATH before importing application. (default: current directory)
--factoryTreat APP as a factory function that returns the application. (default: disabled)
--limit-concurrency INTEGERMaximum number of concurrent connections. (default: unlimited)
--backlog INTEGERSocket listen backlog size. (default: 2048)
--limit-max-requests INTEGERMaximum requests before worker restart. (default: unlimited)
--timeout-keep-alive INTEGERKeep-alive connection timeout in seconds. (default: 5)
--timeout-graceful-shutdown INTEGERGraceful shutdown timeout in seconds. (default: unlimited)
--timeout-worker-healthcheck INTEGERWorker health check timeout in seconds. (default: 5)
--ssl-keyfile TEXTSSL private key file path.
--ssl-certfile TEXTSSL certificate file path.
--ssl-keyfile-password TEXTPassword for encrypted SSL private key.
--ssl-version INTEGERSSL protocol version. (default: ssl.PROTOCOL_TLS_SERVER)
--ssl-cert-reqs INTEGERSSL certificate requirements. (default: ssl.CERT_NONE)
--ssl-ca-certs TEXTCA certificates file path for client verification.
--ssl-ciphers TEXTSSL cipher configuration. (default: "TLSv1")
--h11-max-incomplete-event-size INTEGERMaximum buffer size for h11 incomplete events in bytes.
--env-file PATHEnvironment file (.env) to load before starting.
--versionDisplay uvicorn version and exit.
--helpDisplay help message and exit.
# Run application from myapp module
uvicorn myapp:app
# Specify host and port
uvicorn myapp:app --host 0.0.0.0 --port 8000
# Run on all interfaces
uvicorn myapp:app --host 0.0.0.0# Enable auto-reload
uvicorn myapp:app --reload
# Watch specific directories
uvicorn myapp:app --reload --reload-dir ./src --reload-dir ./lib
# Watch specific file patterns
uvicorn myapp:app --reload \
--reload-include "*.py" \
--reload-include "*.yaml" \
--reload-exclude "*.pyc"
# Enable debug logging
uvicorn myapp:app --reload --log-level debug
# Enable trace logging
uvicorn myapp:app --reload --log-level trace# Run with multiple workers
uvicorn myapp:app --host 0.0.0.0 --port 8000 --workers 4
# Production configuration
uvicorn myapp:app \
--host 0.0.0.0 \
--port 8000 \
--workers 4 \
--limit-concurrency 1000 \
--limit-max-requests 10000 \
--timeout-keep-alive 5 \
--timeout-graceful-shutdown 30 \
--access-log
# Disable access logs for performance
uvicorn myapp:app --workers 4 --no-access-log# Enable HTTPS
uvicorn myapp:app \
--host 0.0.0.0 \
--port 443 \
--ssl-keyfile /path/to/key.pem \
--ssl-certfile /path/to/cert.pem
# HTTPS with client verification
uvicorn myapp:app \
--ssl-keyfile /path/to/key.pem \
--ssl-certfile /path/to/cert.pem \
--ssl-ca-certs /path/to/ca.pem \
--ssl-cert-reqs 2# Bind to Unix socket
uvicorn myapp:app --uds /tmp/uvicorn.sock
# Run behind Nginx with Unix socket
uvicorn myapp:app --uds /var/run/uvicorn.sock --workers 4# Use specific HTTP protocol
uvicorn myapp:app --http h11
uvicorn myapp:app --http httptools
# Use specific WebSocket protocol
uvicorn myapp:app --ws websockets
uvicorn myapp:app --ws wsproto
# Disable WebSocket support
uvicorn myapp:app --ws none
# Use specific event loop
uvicorn myapp:app --loop uvloop# Add custom default headers
uvicorn myapp:app \
--header "X-API-Version:1.0" \
--header "X-Server:MyAPI"
# Disable standard headers
uvicorn myapp:app --no-server-header --no-date-header# Trust proxy headers from localhost
uvicorn myapp:app --proxy-headers --forwarded-allow-ips "127.0.0.1"
# Trust multiple proxy IPs
uvicorn myapp:app --proxy-headers \
--forwarded-allow-ips "127.0.0.1,10.0.0.1,192.168.1.1"
# Trust proxy network
uvicorn myapp:app --proxy-headers \
--forwarded-allow-ips "10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
# Disable proxy headers
uvicorn myapp:app --no-proxy-headers# Mount application at subpath
uvicorn myapp:app --root-path /api/v1
# Add custom module directory
uvicorn myapp:app --app-dir /path/to/app
# Use factory function
uvicorn myapp:create_app --factory# Use custom logging config
uvicorn myapp:app --log-config logging.yaml
# JSON logging config
uvicorn myapp:app --log-config logging.json
# INI logging config
uvicorn myapp:app --log-config logging.ini
# Set log level
uvicorn myapp:app --log-level warning# Load environment file
uvicorn myapp:app --env-file .env
# Load production environment
uvicorn myapp:app --env-file .env.production# Configure WebSocket limits
uvicorn myapp:app \
--ws-max-size 33554432 \
--ws-max-queue 64 \
--ws-ping-interval 30.0 \
--ws-ping-timeout 30.0
# Disable WebSocket compression
uvicorn myapp:app --no-ws-per-message-deflate# Run ASGI2 application
uvicorn myapp:App --interface asgi2
# Run WSGI application (e.g., Flask)
uvicorn myapp:app --interface wsgi
# Auto-detect interface
uvicorn myapp:app --interface auto# High-performance configuration
uvicorn myapp:app \
--host 0.0.0.0 \
--port 8000 \
--workers 8 \
--http httptools \
--loop uvloop \
--limit-concurrency 2000 \
--backlog 4096 \
--no-access-log
# Connection limiting
uvicorn myapp:app --limit-concurrency 500
# Request limiting
uvicorn myapp:app --limit-max-requests 50000# Configure timeouts
uvicorn myapp:app \
--timeout-keep-alive 10 \
--timeout-graceful-shutdown 60 \
--timeout-worker-healthcheck 10# Run uvicorn as module
python -m uvicorn myapp:app
# With options
python -m uvicorn myapp:app --host 0.0.0.0 --port 8000 --reload# Force lifespan events
uvicorn myapp:app --lifespan on
# Disable lifespan events
uvicorn myapp:app --lifespan off
# Auto-detect lifespan support
uvicorn myapp:app --lifespan autouvicorn myapp:app \
--host 0.0.0.0 \
--port 8000 \
--workers 4 \
--loop uvloop \
--http httptools \
--ws websockets \
--proxy-headers \
--forwarded-allow-ips "10.0.0.0/8" \
--limit-concurrency 1000 \
--backlog 2048 \
--limit-max-requests 100000 \
--timeout-keep-alive 5 \
--timeout-graceful-shutdown 30 \
--timeout-worker-healthcheck 10 \
--access-log \
--log-level info \
--no-use-colorsuvicorn myapp:app \
--host 127.0.0.1 \
--port 8000 \
--reload \
--reload-dir ./src \
--reload-include "*.py" \
--reload-include "*.yaml" \
--reload-exclude "*.pyc" \
--reload-exclude "__pycache__/*" \
--log-level debug \
--use-colors \
--access-logUvicorn CLI also respects environment variables for configuration when using an env-file:
# .env file
HOST=0.0.0.0
PORT=8000
WORKERS=4
LOG_LEVEL=info
RELOAD=false# Load from env file
uvicorn myapp:app --env-file .env0: Normal exit (server shutdown gracefully)3: Startup failure (application failed to start)# Show version
uvicorn --version
# Output format:
# Running uvicorn 0.37.0 with CPython 3.11.0 on Linux# Show help
uvicorn --help
# Show detailed help for all options
uvicorn --helpUvicorn can also be used as a Gunicorn worker (deprecated, use uvicorn-worker package instead):
gunicorn myapp:app -w 4 -k uvicorn.workers.UvicornWorker# Dockerfile
FROM python:3.11
WORKDIR /app
COPY . .
RUN pip install uvicorn[standard]
CMD ["uvicorn", "myapp:app", "--host", "0.0.0.0", "--port", "8000"]# Build and run
docker build -t myapp .
docker run -p 8000:8000 myapp# /etc/systemd/system/uvicorn.service
[Unit]
Description=Uvicorn ASGI Server
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/var/www/myapp
ExecStart=/usr/local/bin/uvicorn myapp:app \
--host 0.0.0.0 \
--port 8000 \
--workers 4
Restart=always
[Install]
WantedBy=multi-user.target# Enable and start service
sudo systemctl enable uvicorn
sudo systemctl start uvicornInstall with Tessl CLI
npx tessl i tessl/pypi-uvicorn