Skip to Content
ZarexDocs

The visitor's IP address

How Zarex finds your visitors' real IP address, how to set it for your hosting, and when not to rely on it.

Rate limiting, IP or country filters and bot detection use the visitor's IP address. This page explains how Zarex finds it, and how to set it up.

The principle

Your server sees the address of the machine connecting directly to it.

  • Nothing between the visitor and your server: that address is the visitor's.
  • An intermediary in the middle (nginx, Caddy, Cloudflare, Vercel…): your server sees the intermediary's address. The visitor's real address is written by the intermediary in a header, for example X-Real-IP or CF-Connecting-IP.

But a visitor can also write anything they want in a header. The ipSource option therefore tells Zarex where to read the real address, and whom to trust. It blocks nothing by itself.

If the address is wrong

Either all your visitors seem to come from the same address (your proxy's), and rate limiting blocks them together; or an attacker chooses their address, and gets around your rules.

ipSource values

ValueWhen to use it
"socket"Nothing in front of your application: the connection address.
"express"Let Express compute the address (req.ip, according to your trust proxy setting). Default of the Express SDK.
"framework"Let the framework compute the address (Fastify, SvelteKit). Default of their SDKs.
"vercel"Hosted on Vercel.
"fly"Hosted on Fly.io.
"cloudflare-workers"Your code runs in a Cloudflare Worker (or Pages).
"cloudflare"Behind Cloudflare, in front of your server. The header is only read if the connection really comes from Cloudflare.
{ proxies: n }n proxies of your own in front of your application. Simple, but does not check where the connection comes from: see Several proxies.
{ trustedProxies: [...] }The addresses (or CIDR ranges) of your proxies. Recommended behind your own proxies: see What to put in trustedProxies.
{ cloudflareVia: [...] }Behind Cloudflare, then your own proxy (nginx, Caddy): your proxy's addresses. See Cloudflare, then your proxy. SDK 0.2.7 or later.
{ header: "x-real-ip" }The header that your proxy (nginx, Caddy…) writes itself.
"auto"Detect Vercel, Fly.io or Cloudflare Workers.

Next.js: header-based settings only

Next.js never provides the connection address. "socket", "cloudflare", { proxies } and { trustedProxies } need it: with Next.js, they never find the address, and requests go through without being checked. With Next.js, use "vercel", "fly", "cloudflare-workers" or { header }.

If ipSource is omitted

  • Express, Fastify, SvelteKit: the address computed by the framework is used. Behind a proxy, configure the framework to match your infrastructure (trust proxy for Express, trustProxy for Fastify, the adapter configuration for SvelteKit).
  • Next.js, Bun: Zarex detects Vercel, Fly.io and Cloudflare Workers. Anywhere else, the application refuses to start, with a message listing the options, rather than guessing.

By hosting

HostingNext.jsExpress, Fastify, SvelteKit, Bun
VercelNothing to doNothing to do (or "vercel")
Fly.ioNothing to doNothing to do (or "fly")
Cloudflare Workers / PagesNothing to do—
VPS without a proxyNot supported: put a proxy in front"socket" (or the framework's default setting)
VPS with nginx or Caddy{ header: "x-real-ip" }{ trustedProxies: ["127.0.0.1", "::1"] } (proxy on the same machine), or your proxy's address: see What to put in trustedProxies
Cloudflare in front of a VPS{ header: "cf-connecting-ip" }"cloudflare"
Cloudflare, then nginx or Caddy, in front of a VPSNot documented yet{ cloudflareVia: ["127.0.0.1", "::1"] }: see Cloudflare, then your proxy

Several proxies

Each proxy appends to the X-Forwarded-For header the address of whoever contacted it. With a load balancer then nginx in front of your application:

Visitor 41.2.3.4 → load balancer 10.0.0.1 → nginx 10.0.0.2 → your application

X-Forwarded-For received : 41.2.3.4, 10.0.0.1
Connection address       : 10.0.0.2

Zarex reads this chain from the end: the end is written by your machines, the beginning may have been written by the visitor. You only need to tell it where your machines stop:

  • { proxies: 2 }: "the last 2 links are mine". The visitor is the link just before.
  • { trustedProxies: ["10.0.0.0/8"] }: "every 10.x.x.x address is mine". Zarex skips your addresses; the first one that is not yours is the visitor.

A visitor who writes X-Forwarded-For: 8.8.8.8 themselves gains nothing: their fake address stays at the beginning and is never read.

{ proxies: n }{ trustedProxies: [...] }
What you providethe number of proxiestheir addresses
Variable number of proxies❌✅
Someone reaches your application directly, without going through your proxies⚠️ they choose their address✅ caught: their real address is used

With { proxies }, close your application to the public

{ proxies } counts links without checking where the connection comes from. If your application can be reached without going through your proxies, an attacker can write a fake chain and choose their address, with no warning at all. Make the application listen on 127.0.0.1 or on a private network, or prefer { trustedProxies }.

What to put in trustedProxies

The address from which your proxy contacts your application, as your application sees it. Not your server's public address.

Your setupWhat the application seesWhat to put
nginx or Caddy and the application on the same machine127.0.0.1 (or ::1)["127.0.0.1", "::1"]
nginx or Caddy on the machine, application in a Docker container (published port)the gateway of the container's Docker network, for example 172.17.0.1that address (docker network inspect <network>, Gateway field)
Proxy and application in containers on the same Docker networkthe proxy container's addressthe address or range of that network
Load balancer and proxy on other machinestheir private addressesthose addresses, or their range (for example ["10.0.0.0/8"])
Cloudflare, then your proxyCloudflare, then your proxyuse { cloudflareVia } instead: see Cloudflare, then your proxy

Never put a range that contains visitors (for example 0.0.0.0/0): an address in the list is always skipped, so that visitor would become invisible.

The safe way: look once

Log once the connection address your application receives, behind your real proxy: that is the address to put in the list.

FrameworkConnection address
Expressreq.socket.remoteAddress
Fastifyrequest.raw.socket.remoteAddress
Bunserver.requestIP(request)?.address
SvelteKitevent.getClientAddress() (address already computed by the adapter)
Next.jsnone: use { header }
Express
app.use((req, _res, next) => { console.log(req.socket.remoteAddress); next(); });

Cloudflare, then your proxy

A common setup: Visitor → Cloudflare → nginx or Caddy → your application. Your application only sees your proxy. The visitor's real address is in CF-Connecting-IP, but it must only be trusted if the request really went through Cloudflare.

ipSource: { cloudflareVia: ["127.0.0.1", "::1"] }, // YOUR proxy's addresses, as seen by the application (SDK 0.2.7 or later)

For each request, Zarex checks in X-Forwarded-For who contacted your proxy: that address is written by your proxy itself. If it is Cloudflare, it reads CF-Connecting-IP, which Cloudflare always overwrites. Otherwise, someone bypassed Cloudflare: their real address is used, with a warning.

Your proxyWhat you need
CaddyNothing: it forwards X-Forwarded-For by default.
nginxOne line, because nginx does not forward X-Forwarded-For by default: proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Without that line, going through Cloudflare cannot be checked: the request goes through without being checked, and the SDK tells you which line to add.

With { trustedProxies }, it does not always work

{ trustedProxies: [...CLOUDFLARE_RANGES, "127.0.0.1"] } looks for the visitor in X-Forwarded-For: it only works if your proxy appends to that chain. Caddy replaces it by default, and nginx does not send it by default: in both cases, every request goes through without being checked. Behind Cloudflare then your proxy, use { cloudflareVia }.

So that nobody can bypass Cloudflare, only accept connections from Cloudflare's addresses on your proxy.

What the SDK warns you about

The SDK writes a warning to your logs when the setting looks wrong (each message at most once per period):

SituationMessage
A request does not come from the declared proxy (direct access, or a proxy missing from the list)A request did not come from the proxy declared in ipSource…
Fewer links than declared proxiesX-Forwarded-For has fewer addresses than the proxies declared…
A proxy header is present while you chose "socket"A proxy header … is present but ipSource is "socket"…
The address found is private or local (often: a misconfigured proxy)The engine received a private/local/shared IP address…
{ cloudflareVia }: someone reached your proxy without going through CloudflareA request reached your proxy without going through Cloudflare…
{ cloudflareVia }: your proxy does not send X-Forwarded-ForYour proxy does not send X-Forwarded-For…
Your proxy usually connects from a private address, but a request arrived from a public address and its header was trusted (SDK 0.3.0 or later)A request reached your app from a public address…
{ header }: this time, the header does not match what your proxy added to X-Forwarded-For (SDK 0.3.0 or later)A request's … header did not match…

What the SDK cannot see

Seen from your application, an honest request and a forged one are identical. Since version 0.3.0, with Express, Fastify and Bun, a direct access that bypasses your proxy with { proxies } or { header } is reported on the first request that uses it (once the first 200 requests have been observed), and a header your proxy does not overwrite is reported if that proxy also sends X-Forwarded-For. Still invisible: direct access with Next.js, and an address forged before your proxy. A report comes after the fact: the rule does not change, close the application to everything except your proxy.

The ipSource check

Since version 0.3.0, the SDK observes the first requests after your application starts and tells you, in your logs, if your ipSource setting looks wrong.

What it observes. Where connections come from (a private address such as 10.0.0.3 is necessarily one of your machines), what your proxies wrote at the end of X-Forwarded-For, and how many different addresses it sees (hundreds of visitors, or a single proxy). From this it deduces the most likely setting.

What it never does. It changes nothing: not the computed address, not the decision. It only sends Zarex a short code (see In the dashboard), never an address or a header value, and keeps no visitor address (only fingerprints in memory, to count different addresses). It never proposes a public address: it cannot know whether that address is yours or an attacker's.

When it speaks. After at least 200 requests (up to 2,000 when unsure), once per start, and only if your setting looks wrong: when everything is fine, or when it has not seen enough visitors, it says nothing. It stays silent with "vercel", "fly" and "cloudflare-workers", whose source is already reliable. After that, it only reports the anomalies described in What the SDK warns you about.

Example, with { proxies: 1 } behind two proxies:

[zarex] ipSource check after 200 requests (current setting: "proxies:1"):
  Observed: Connections come from your proxy (10.10.0.3, a private or local address); X-Forwarded-For is present, and the address just before your proxies is public and varies from one visitor to another. You declared proxies: 1, but requests cross 2 of your proxies.
  Most likely setting: ipSource: { trustedProxies: ["10.10.0.3", "10.10.0.2"] }
  Nothing was changed: check that this matches your setup, then change ipSource yourself. Docs: https://zarex.dev/en/docs/reference/ip (to turn this check off: ipDiagnostic: false)

Check that the proposal matches your setup, then change ipSource yourself. During development, on your own machine, a message that mentions 127.0.0.1 can be ignored.

To turn it off:

ipDiagnostic: false,

It is also off during your automated tests (NODE_ENV=test) and with logger: false.

In the dashboard

Since version 0.3.2, the SDK also attaches a short code to its decisions: its conclusion (for example trusted-proxies, or ok when everything is fine), and direct-access or header-mismatch on a request where it spotted one of the anomalies in What the SDK warns you about. No extra request: the code travels with a decision that is sent anyway. Never an address or a header value.

The dashboard home page then shows, for the last 24 hours in production:

  • an alert if requests reached your application without going through your proxy, or with a header your proxy did not overwrite;
  • a suggestion if the SDK proposes a different setting, with your current setting. The exact setting, with your proxies' addresses, stays in your logs (line [zarex] ipSource check);
  • nothing when the setting is right.

ipDiagnostic: false also turns this off.

With your own proxy: { header }

Your proxy writes the real address in a header, and Zarex reads it.

/etc/nginx/sites-available/app
location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header X-Real-IP $remote_addr;
}
ipSource: { header: "x-real-ip" },

If the header contains a list (for example X-Forwarded-For), Zarex takes the last value: the one your proxy added. Earlier values may come from the visitor and are ignored.

Two security conditions

  1. The proxy must overwrite the header, not append to it: that is what proxy_set_header X-Real-IP $remote_addr; does.
  2. The application must be reachable only through the proxy: listen on 127.0.0.1 or close its port to the public. Otherwise, a visitor who bypasses the proxy can write the header themselves and choose their address.

If the header is missing (a request that did not go through the proxy), the request goes through without being checked, and the SDK writes it to your logs.

Outside Next.js, { trustedProxies } is safer: it checks where the connection comes from (see What to put in trustedProxies).

Locally

Locally, there is no proxy: the visitor is you, with the address 127.0.0.1 (or ::1). The country shows as unknown (ZZ): that is normal.

  • Express, Fastify, SvelteKit, Bun: it works with no particular setting ("socket" for Bun).
  • Next.js: next dev passes your address in x-forwarded-for. With "vercel", requests are checked, with warnings that are expected locally. With { header: "x-real-ip" }, the header is missing and requests go through unchecked: to test, add it yourself:
curl -H "X-Real-IP: 41.0.0.1" http://localhost:3000/api/products

Do not rely on the IP address alone

Even when set correctly, an IP address does not always designate a single person:

  • on mobile networks, many subscribers share the same public address;
  • all the devices on the same Wi-Fi (office, cybercafé, family) go out with the same address.

A per-IP limit can then block several people because of one. When you know the user, identify them with characteristics instead: the limit applies per account, whatever their address.

rules: [rateLimit({ max: 20, window: "1m", characteristics: ["userId"] })],
call: (req) => ({ characteristics: { userId: req.user?.id } }),

You can use any stable identifier: an account identifier, a session, an API key of your own service. Values are hashed (SHA-256) before being sent to Zarex. See Rate limiting.

On this page