04 · Applications
Behind a reverse proxy — trust X-Forwarded-*
When deployed behind nginx / Caddy / Cloudflare, reconstruct the original URL from X-Forwarded-Proto and X-Forwarded-Host. Stored on the context for downstream handlers.
Code
behind-reverse-proxy.js
const { WebApp } = require(class="tk-str">39;@hyperbridge/forge/server39;); const app = new WebApp(); class="tk-cm">// Middleware: respect X-Forwarded-* from a trusted proxy. app.use(class="tk-str">39;*39;, async (c, next) => { const proto = c.req.header(class="tk-str">39;x-forwarded-proto39;); const host = c.req.header(class="tk-str">39;x-forwarded-host39;); if (proto && host) { const u = new URL(c.req.url); u.protocol = proto; u.host = host; c.set(class="tk-str">39;originalUrl39;, u.toString()); } await next(); }); app.get(class="tk-str">39;/me39;, (c) => c.json({ url: c.get(class="tk-str">39;originalUrl39;) ?? c.req.url })); app.listen(3000);
How it works
c.set() and c.get() are a per-request state bag. Middleware computes once, downstream handlers read without re-parsing.
Only trust these headers if you're behind a known proxy. In a zero-trust public deployment, ignore them.
The exact same pattern works for tenant resolution, auth context propagation, request IDs, geolocation lookup, A/B test bucket assignment.
Try it
Quickstart
curl -H 'X-Forwarded-Proto: https' -H 'X-Forwarded-Host: example.com' http://localhost:3000/me
Related modules
This is HBForge's port of Hono's example. Read the original at hono.dev/examples/behind-reverse-proxy.