Fastapi Behind Proxy

Published
Length
3 min read

Luis Felipe Dias

All posts

Running a fastapi behind proxy server requires passing client headers and path prefixes to your app. You set up the web server to forward host details so the app builds right links. Most teams place a reverse router in front of Python web apps to handle web traffic. This front server handles TLS certs and sends requests to the local Python runner. When teams grant broad network trust to upstream hosts, anyone on the network can spoof headers. Attackers can fake client IP addresses or force the a

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Running a fastapi behind proxy server requires passing client headers and path prefixes to your app. You set up the web server to forward host details so the app builds right links. Most teams place a reverse router in front of Python web apps to handle web traffic. This front server handles TLS certs and sends requests to the local Python runner. When teams grant broad network trust to upstream hosts, anyone on the network can spoof headers. Attackers can fake client IP addresses or force the app to build unsafe redirect links.

How to set up a fastapi behind proxy architecture

A Python web app runs inside an ASGI server on a local port. In production, you do not expose this runner to the open internet. Instead, you place a web server or load balancer in front of the runner. This front server takes incoming HTTPS traffic from users and strips the encryption. It then sends plain HTTP requests to the Python runner over a private network.

When the front server forwards a request, the Python app only sees the local server IP. It loses the real user IP address and the original HTTPS scheme. To fix this, the front server adds forwarding headers to every HTTP request. You must tell the ASGI runner to trust those headers from the specific front server IP. Once trusted, the runner reads the headers and updates the request state for the app.

Path prefixes create another common setup problem when routing traffic to multiple services. Your front server might route traffic from a sub-path like a versioned folder to the app. The front server often strips that folder prefix before passing the request to the Python runner. If the Python app does not know about the stripped prefix, its doc pages break. The interactive docs page tries to fetch the schema file from the root path and fails. You fix this by setting the root path value inside the Python app to match. This setting tells the schema generator to prepend the sub-path to every URL it builds.

What native header rules solve and what they leave open

Setting up header trust and root paths solves routing and link generation for your web app. Your interactive doc pages load the schema file from the right public address. Redirect responses keep the HTTPS scheme instead of dropping users back to plain HTTP. Your app logs record the real client IP address instead of the local load balancer address. Rate limit rules inside the app can also track real users rather than blocking the router.

Database Proxy (ProxySQL, PgBouncer): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Still, network routing rules only control how web traffic reaches the Python runner. They do not manage how engineers or scripts interact with the backend service or its data. A reverse router decides which network ports are open and how headers move. Once a request passes the front server, the network layer does not inspect the payload. If an engineer needs to debug the live service, they often need direct server credentials. Sharing static keys or wide network roles gives users full control over the host machine. Also, standard web server logs only record URL paths and status codes. They do not mask sensitive fields like emails, phone numbers, card numbers, or national IDs in responses. Teams still lack fine control over who runs specific queries or views sensitive data in flight.

Check HTTP API requests and responses on the wire

API gateways and auth headers decide who may call an internal service. Request checks alone cannot read the response body where sensitive data leaves the service. With hoop.dev, you run an open-source proxy, a sidecar for your HTTP API. It runs beside an internal HTTP service or behind Envoy while keeping the API's own auth in place.

Dynamic path segments like IDs and UUIDs collapse to wildcards so one rule in hoop.dev guardrails can refuse banned methods or paths before execution with a 403 error. On the way back, hoop.dev data masking walks JSON payloads by key path and rewrites matched sensitive values in memory. Every request and decision is logged per caller, and hoop.dev is MIT licensed on GitHub.

Frequently asked questions

Why do my interactive doc pages fail to load behind a reverse router?

The front server strips the URL path prefix before sending the request to your Python runner. Your app then builds a schema link pointing to the root domain instead of the sub-path. You must set the root path in your app so it matches the public URL prefix.

Why does my Python app log the load balancer IP instead of the user IP?

Your ASGI runner ignores forwarding headers by default to prevent IP spoofing from untrusted networks. You need to configure the runner to trust forwarding headers coming from your load balancer IP. Once enabled, the runner swaps the load balancer address with the real client IP.

The front server ends the TLS session and sends plain HTTP traffic to the Python runner. The app sees an HTTP request and builds redirect links using that plain scheme. Configure your front server to send the forwarded protocol header and tell your runner to trust it.

The query runs. The emails stay hidden.