Prompt
How do I execute auth checks before requests hit my origin?
Latest observation
If you want auth checks to happen before traffic reaches your origin, you usually put the authorization layer at the edge or front door of your app rather than inside the app itself.
Common ways to do this
1) Reverse proxy / gateway auth
Put something like:
- Nginx
- Envoy
- Kong
- Traefik
- API Gateway products
in front of your origin, and configure it to:
- validate JWTs
- check session cookies
- call an auth service
- enforce API keys / mTLS / IP allowlists
This is the classic “block unauthorized requests before origin” setup.
2) Edge middleware / CDN auth
Use edge logic in:
- Cloudflare Workers
- Fastly Compute
- Akamai edge
- Vercel Middleware
- Netlify Edge Functions
- AWS CloudFront + Lambda@Edge
These can inspect requests at the edge and reject unauthenticated ones before they ever reach your server.
3) External authorization service
Have the proxy or gateway call an auth service via a policy check:
- request comes in
- gateway sends headers/token/context to auth service
- auth service returns allow/deny
- gateway forwards or blocks
This is common with:
- OPA / Open Policy Agent
- Envoy ext_authz
- custom auth microservices
4) Application firewall / identity-aware proxy
Some zero-trust products do this too:
- Cloudflare Access
- Google IAP
- Azure AD App Proxy
- Zscaler / Twingate / Okta ASA
These authenticate users at the network edge before your app is reachable.
Typical flow
- Client sends request
- Edge/gateway checks auth
- If unauthorized: return
401or403 - If authorized: forward to origin with trusted identity headers or claims
Important implementation notes
- Don’t trust client-provided identity headers unless they’re injected by your edge and stripped from incoming requests.
- Use signed tokens or secure session validation.
- Make sure origin is only reachable from the proxy/edge, otherwise bypass is possible.
- Decide whether you want:
- authentication: who are you?
- authorization: are you allowed?
Example patterns
Nginx with auth request
location / {
auth_request /auth;
proxy_pass http://origin;
}
location = /auth {
internal;
proxy_pass http://auth-service/validate;
}
Envoy ext_authz
Envoy can call an external auth service before routing to upstream.
Cloudflare Worker
Inspect cookies/JWT at the edge and return new Response("Unauthorized", { status: 401 }) if invalid.
If you tell me what stack you’re using, I can show the exact setup:
- Nginx
- Envoy
- AWS
- Cloudflare
- Kubernetes ingress
- Node/Express behind a gateway