Behind a reverse proxy
此内容尚不支持你的语言。
The server speaks plaintext HTTP on port 3000 and does not terminate TLS. Anywhere other than a laptop, something in front of it almost always does.
This page is about running FoxAuth itself behind a proxy; to make your proxy ask FoxAuth before it lets anyone into
another web app, see Put sign-in in front of a web app, whose
check reads X-Forwarded-Host only to compare it with that app’s address, never to choose a project.
Four things to get right
Section titled “Four things to get right”TLS terminates at the proxy. Bind the server’s port to loopback (or to a private network) so nothing reaches 3000 from outside, and let the proxy hold the certificate.
ISSUER is the public https URL. Not the internal address, not http://. It is the iss
claim of every token, the base of every endpoint in the discovery document, and the redirect target
of the console client. A client library that fetches discovery will follow whatever it says, so a
wrong ISSUER produces a deployment that half-works, in ways that tend to be tedious to diagnose.
See the environment reference.
Pass the original Host through. The server reads that header, and only that header, to work out
which project a request is addressed to when projects are given subdomains or hostnames of their own. It
deliberately does not read X-Forwarded-Host: that header is set by whatever spoke to your proxy, so a
proxy which does not strip an inbound copy would let a caller choose which project, with its own population of users, answers them.
That choice is the most powerful thing a request could influence here. Host is what the client connected
to, and what your proxy’s own routing already matched on.
The nginx block below sets proxy_set_header Host $host; for this reason, and Caddy’s reverse_proxy
preserves it by default. A proxy that rewrites Host to the upstream’s internal name leaves every
project on a subdomain or a hostname of its own unreachable. Path-addressed projects are unaffected,
so the fault is easy to miss.
Check how the server is reached. The next section covers clientAddress.source.
Subdomain projects
Section titled “Subdomain projects”When projects may have subdomains, the proxy must
accept every name one label beneath the instance’s host, *.auth.example.com, hold a wildcard certificate for
it, and send those requests to the same server with their own Host. In nginx that is a second name on the same
server block, server_name auth.example.com *.auth.example.com;. Caddy can serve a site address of
*.auth.example.com, but Let’s Encrypt issues a wildcard certificate only through the DNS challenge, so Caddy
needs credentials for your DNS provider and a build with that provider’s module. The server still never reads X-Forwarded-Host, for subdomains as for hostnames.
How the server finds the caller’s address
Section titled “How the server finds the caller’s address”Every limit counts requests per caller address: the rate limits, the failed-credential charges and the mail
limit. A header such as X-Forwarded-For can be written by anyone, so the server believes one only on a
connection from a proxy it trusts. Which proxies it trusts is
clientAddress.source:
| Choice | When to use it | What is read |
|---|---|---|
automatic (default) |
Reached directly, or behind nginx, Caddy, an Ingress or a load balancer on a private network | X-Forwarded-For, only on a connection from a private network |
direct |
Reached directly, and you want no header read at all | Nothing |
fly |
Fly.io | Fly-Client-IP, which Fly’s edge overwrites |
cloudflare |
Cloudflare in front, directly or before your own proxy | X-Forwarded-For, also trusting Cloudflare’s published ranges |
listed |
A proxy with a public address that is not Cloudflare | X-Forwarded-For, also trusting the addresses you list in clientAddress.trustedProxies |
A private network here means the IPv4 private ranges, loopback, link-local and IPv6 unique-local addresses.
Shared address space (100.64.0.0/10, used by carrier-grade NAT and by Tailscale) is not one: a client
connecting from it is the caller.
How X-Forwarded-For is read
Section titled “How X-Forwarded-For is read”A proxy appends the address it received the request from, and keeps whatever the caller sent to the left of
it. So the server reads the header from the right: it skips every entry a trusted proxy wrote, and the first
entry that is not one is the caller. If every entry is a trusted proxy, the rightmost one is used. An entry that
is not an IP address stops the walk, and the caller is the last trusted hop. Entries such as
203.0.113.7:51234 and [2001:db8::1]:443 are read without the port.
Your proxy does not need to overwrite the header: appending, as nginx’s $proxy_add_x_forwarded_for does, is
what the server expects, and overwriting works too.
X-Real-IP, CF-Connecting-IP and Forwarded (RFC 7239) are never read.
Check it
Section titled “Check it”Open Settings → Sign-in & abuse in the console. The card under the setting shows the address the server sees for your own request, where it took it from, and a warning when the choice does not match what arrived. If the address is not the one you are connecting from, every limit is counting the wrong caller.
Sample nginx and Caddy configuration
Section titled “Sample nginx and Caddy configuration”server { listen 443 ssl http2; server_name auth.example.com;
ssl_certificate /etc/letsencrypt/live/auth.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/auth.example.com/privkey.pem;
location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1;
proxy_set_header Host $host; # Appends the caller to whatever arrived; the server reads the entry this proxy wrote. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }
# Never rate-limit or auth-gate the liveness probe. location = /health { proxy_pass http://127.0.0.1:3000; access_log off; }}
server { listen 80; server_name auth.example.com; return 301 https://$host$request_uri;}auth.example.com { reverse_proxy 127.0.0.1:3000 { # Caddy sets X-Forwarded-For to the caller by default; nothing to add. }}Caddy obtains and renews the certificate itself, which is most of why its block is so much shorter.
Client certificates (mTLS)
Section titled “Client certificates (mTLS)”This section applies only to RFC 8705: clients authenticating with a TLS certificate, or certificate-bound access tokens. TLS ends at the proxy, so the server never sees the handshake: the proxy has to verify the client certificate and forward it in a header, and the server has to be told to believe that header.
The header is RFC 9440’s Client-Cert: the DER certificate as a byte sequence, base64 between
colons. A bare base64 X-Client-Cert is read when Client-Cert is absent, for proxies configured
before RFC 9440. The server reads neither until
mTLS.trustProxyCertificateHeader is
on, and that setting has a wrong answer in each direction: off, no certificate is ever read; on
behind a proxy that passes an incoming header through, anyone can send a certificate (certificates
are public) and be believed.
So the proxy must set the header on every request, overwriting whatever the client sent, and remove the other one. In Caddy:
auth.example.com { tls { client_auth { mode verify_if_given trust_pool file /etc/caddy/client-ca.pem } } reverse_proxy 127.0.0.1:3000 { header_up Client-Cert ":{http.request.tls.client.certificate_der_base64}:" header_up -X-Client-Cert }}verify_if_given keeps every other client working without a certificate. nginx has no variable
holding the DER certificate as base64. Its $ssl_client_escaped_cert is URL-encoded PEM, which this
server does not read. So behind nginx, either put a proxy that can produce Client-Cert in front of
the server, or override features.mTLS.getCertificate to read nginx’s form. Whichever you choose,
clear both headers on requests that carry no certificate, then switch the setting on.
Checking it worked
Section titled “Checking it worked”curl -sI https://auth.example.com/healthcurl -s https://auth.example.com/.well-known/openid-configuration | jq -r .issuerThe second command must print the public https URL you set as ISSUER. If it prints an internal
address or an http:// URL, fix that before anything else: every client will believe it.
To confirm the forwarded header is reaching the server, watch what happens when you exceed a limit.
A 429 with Retry-After that arrives for your address alone, while other callers carry on, is
the resolver working. No RateLimit-* headers are emitted, and that is intentional: they would
tell a caller probing for the threshold exactly where it is.