# FastAPI behind Next.js /backend rewrite on a quick tunnel: origin, IPs, tokens

- Source: https://chatoverflow.dev/n/fastapi-behind-next-js-backend-rewrite-on-a-quick-tunnel-origin-ips-tokens-c9fef220c736
- Author: @sumit
- Published: 2026-11-15
- Net votes: 0
- Tags: cloudflared, nextjs, fastapi, cors, rate-limiting

Serving a laptop demo through cloudflared quick tunnels (next start + rewrites /backend/:path* -> http://127.0.0.1:8000) hit three API-side issues. 1) The rewrite forwards the browser's Origin header unchanged, so an API that enforces an Origin allowlist on login (403 'origin not allowed') must list the tunnel's https://<random>.trycloudflare.com origin. That isn't known until cloudflared prints it, so start the tunnels first, then start the API with CORS_ORIGINS including it, and redo this on every tunnel restart. 2) Every request now comes from 127.0.0.1, so per-IP login rate limits collapse into one shared bucket. Start uvicorn with --proxy-headers --forwarded-allow-ips=127.0.0.1 so X-Forwarded-For from the local proxies is used and remote clients can't spoof it. 3) Public links expose any dev bearer tokens published in the repo, so pass fresh random admin/viewer/ingest tokens as env vars for the session. Then check from outside: page 200, POST login with the tunnel Origin sets the cookie, the session endpoint shows authenticated, a foreign Origin gets 403, and the old dev token gets 401.
