Reverse proxy and client IP
This page is for operators who put Adiutor behind a reverse proxy, a CDN or a load balancer. It covers keeping the app’s port private, telling Adiutor which header holds the client’s IP address, example setups for Caddy and nginx, and how to check it.
Adiutor uses the client’s IP address for its per-IP rate limits, the audit log, the list of signed-in devices and the security emails. If it reads the wrong address, everyone behind the proxy shares one rate limit (one person can then block password and email-link sign-in for the whole site), or someone can choose their own address and put a fake one in the audit log and in emails.
Keep the app’s port private
Section titled “Keep the app’s port private”Whatever your setup, clients must not be able to reach the app without going through your proxy or platform. Anyone who can talk to the app directly can send any IP header and choose their own address, which gets around the per-IP limits.
On a server with Docker, published ports bypass firewalls such as ufw. So either:
- publish the port on the loopback address only,
127.0.0.1:3000:3000instead of3000:3000, when the proxy runs on the host, or - don’t publish it at all, and put the proxy on the same Docker network as the app.
How Adiutor reads the client’s address
Section titled “How Adiutor reads the client’s address”Two settings decide it:
CLIENT_IP_HEADERSnames the headers to read, in order. The default isx-forwarded-for.TRUSTED_PROXIESlists the addresses or CIDR ranges of your own proxies, separated by commas.
Without TRUSTED_PROXIES, a header only counts when it holds a single address. A chain like client, proxy is ignored, since anyone can put an address at its left end. With TRUSTED_PROXIES, a chain is read from the right, skipping those addresses: the first other address is the client.
The safest setup is a proxy that replaces one header with the address that connected to it, and CLIENT_IP_HEADERS set to that header, with TRUSTED_PROXIES empty. The two examples below do that with X-Real-IP.
Caddy gets a certificate for the address by itself and redirects HTTP to HTTPS. In the Caddyfile, replace X-Real-IP with the address that connected:
adiutor.dev { reverse_proxy 127.0.0.1:3000 { header_up X-Real-IP {remote_host} }}That’s for Caddy on the host, with the app’s port published on the loopback address as in the Compose file. When Caddy runs in a container on the same Docker network as the app, use the app’s service name instead, app:3000. Caddy passes on the Host the browser asked for and sets X-Forwarded-Proto by itself (see nginx for why that matters). Then set this in Adiutor’s environment:
CLIENT_IP_HEADERS=x-real-ipand leave TRUSTED_PROXIES empty.
A whole server block for HTTPS, with the certificate paths from your certificate tool, and a block that sends HTTP to HTTPS:
server { listen 80; server_name adiutor.dev; return 301 https://$host$request_uri;}
server { listen 443 ssl; # nginx 1.25.1 or newer; older versions write "listen 443 ssl http2;" instead. http2 on; server_name adiutor.dev;
ssl_certificate /etc/letsencrypt/live/adiutor.dev/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/adiutor.dev/privkey.pem;
# Attachments up to 50 MB; nginx allows 1 MB unless told otherwise. client_max_body_size 51m;
location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; }}X-Real-IP is set to the address that connected, whatever the client sent. Keep the Host and X-Forwarded-Proto lines too. Adiutor takes its own address from BETTER_AUTH_URL, but Next.js, which it runs on, refuses every form whose origin doesn’t match Host, and builds the address it redirects signed-out visitors to from both headers. nginx passes the Range header and the 206 answers that videos need through, unless you turn on caching for this location. Then set:
CLIENT_IP_HEADERS=x-real-ipand leave TRUSTED_PROXIES empty. With the Docker image, use the app’s service name instead of 127.0.0.1 when nginx is on the same Docker network.
Keeping X-Forwarded-For
Section titled “Keeping X-Forwarded-For”If you would rather keep X-Forwarded-For, where each proxy adds the address it saw to the right end, set TRUSTED_PROXIES instead of CLIENT_IP_HEADERS:
- With a single proxy, any value works, for instance the proxy’s own address: it only makes Adiutor read the chain from the right.
- With more hops (a load balancer or CDN in front of the proxy), list every one of their addresses.
Otherwise Adiutor sees a hop’s address, or none, instead of the client’s. When it sees none, it logs Better Auth’s warning “Rate limiting could not determine a client IP”.
Vercel
Section titled “Vercel”Nothing to set. Vercel replaces X-Forwarded-For with the client’s address and drops what the client sent, so the default works: leave both settings empty. Don’t put another proxy or CDN in front of it without coming back to this page.
Cloudflare and load balancers
Section titled “Cloudflare and load balancers”Behind Cloudflare, set CLIENT_IP_HEADERS=cf-connecting-ip, and only accept traffic from Cloudflare’s addresses (in your firewall), or anyone could send that header themselves.
Platforms that add to a client’s X-Forwarded-For instead of replacing it, such as AWS load balancers and Heroku-style routers, need their addresses in TRUSTED_PROXIES, like a second hop above.
Check after deploying
Section titled “Check after deploying”-
Sign in and open your account’s Activity tab. Under “Signed-in devices”, this device must show your own public address. Compare with
curl https://icanhazip.com. -
Try a forged address, with a wrong password for your own account (here Alex Admin’s):
Terminal window curl https://adiutor.dev/api/auth/sign-in/email -H 'Content-Type: application/json' -H 'Origin: https://adiutor.dev' -H 'X-Forwarded-For: 203.0.113.7' -H 'X-Real-IP: 203.0.113.7' -d '{"email":"[email protected]","password":"not-the-password"}'As a staff Admin, open your own account under Users, on its Activity tab. Under Recent activity, that failed attempt must show your own address, not
203.0.113.7. -
From another machine, the app’s own port must not answer:
curl -m 5 http://<server address>:3000should fail. -
The app’s log must not show “Rate limiting could not determine a client IP”.
Example
Section titled “Example”Adiutor runs at https://adiutor.dev behind Caddy on the same server, with the app published on 127.0.0.1:3000. Alex Admin ([email protected]) sets CLIENT_IP_HEADERS=x-real-ip, restarts, then runs the checks above with the address 203.0.113.7. The failed attempt shows Alex’s own address, so it works.
Good to know
Section titled “Good to know”- A proxy that sets a header only helps while clients can’t reach the app around it: keep the port private.
- Adiutor needs HTTPS: a production server refuses an
http://BETTER_AUTH_URL, except on this computer.
Related
Section titled “Related”Adiutor's code is released under the MIT License. The Adiutor name and logo aren’t covered by it; see the notice.
Set in Inter and JetBrains Mono, under the SIL Open Font License (Inter, JetBrains Mono).