Prompt
I'm building an API and want to run auth and redirects before the origin
Latest observation
Oct 26, 2026
OpenAI APIWeb search: off
If you mean “before my backend/origin sees the request,” then yes — that’s typically done at the edge or gateway layer.
Common options:
-
API Gateway / Reverse Proxy
- Run authentication, header checks, and redirect logic before forwarding to the origin.
- Examples: Kong, NGINX, Envoy, AWS API Gateway, Apigee.
-
CDN / Edge Compute
- Put logic at the edge so unauthorized requests or redirects are handled before hitting origin.
- Examples: Cloudflare Workers, Fastly Compute, AWS CloudFront Functions / Lambda@Edge.
-
Application middleware
- If you don’t have edge infrastructure, you can still do auth/redirects in your app before calling downstream services.
- This is “before origin” only in the sense of before your internal handlers.
Typical flow:
- Request hits edge
- Edge checks auth
- Edge applies redirect rules
- If allowed, request is forwarded to origin
- Origin handles business logic
A few design tips:
- Keep authentication stateless if possible at the edge
- Use signed tokens or session validation that can be checked without calling origin
- Be careful with redirect loops
- Preserve important headers like
Authorization,Cookie, andX-Forwarded-*if needed
If you want, I can suggest the best pattern for your stack:
- Cloudflare / AWS / NGINX / Kubernetes / Express / FastAPI / etc.