Python / Uvicorn Fundamentals Interview Questions
What are proxy headers in Uvicorn and how do you configure trusted proxies?
When Uvicorn runs behind a reverse proxy (Nginx, Caddy, a load balancer), the proxy forwards requests on behalf of the original client. Without proxy header support, Uvicorn would see the proxy's IP as the client address and the internal scheme (http) rather than the original (https). The --proxy-headers flag tells Uvicorn to trust X-Forwarded-For and X-Forwarded-Proto headers from trusted proxies.
# Enable proxy headers (enabled by default, trusts only 127.0.0.1) uvicorn main:app --proxy-headers # Trust a specific proxy IP uvicorn main:app --proxy-headers --forwarded-allow-ips 10.0.0.1 # Trust multiple proxy IPs or a subnet uvicorn main:app \ --proxy-headers \ --forwarded-allow-ips "10.0.0.0/8,172.16.0.0/12" # Trust ALL proxies (use ONLY on trusted internal networks!) uvicorn main:app --proxy-headers --forwarded-allow-ips "*" # Disable proxy headers (if no reverse proxy in front) uvicorn main:app --no-proxy-headers # Set via environment variable: export FORWARDED_ALLOW_IPS="10.0.0.1,10.0.0.2" uvicorn main:app --proxy-headers
Security warning: setting --forwarded-allow-ips "*" trusts ALL connecting IPs to set forwarded headers. This means a malicious client could spoof their IP address by sending a fake X-Forwarded-For header. Only trust IPs of known, controlled proxy servers.
| Setting | When to use |
|---|---|
| --forwarded-allow-ips 127.0.0.1 (default) | Reverse proxy on the same machine |
| --forwarded-allow-ips | Known proxy IP (e.g. internal load balancer) |
| --forwarded-allow-ips "*" | Trust everything - only on fully controlled internal networks |
| --no-proxy-headers | Uvicorn exposed directly (no reverse proxy) |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
