First-party proxy: what to forward

Updated

Forward the request body, the User-Agent and the visitor's IP in X-Forwarded-For, and nothing else: never the browser's Cookie header. DataPocket reads the visitor's IP from the right-most public address in the X-Forwarded-For your proxy sends, so a proxy that leaves it out makes every visitor appear to be at your server's location.

What to forward

FeatureForward?Why
Request bodyYesThe event itself (JSON, sent as text/plain).
User-AgentYesBrowser, OS, device and bot filtering.
X-Forwarded-ForYes: the visitor's IPCity-level location. The IP is never stored.
CookieNoYour visitors' cookies: auth sessions, other vendors' ids. DataPocket never reads them, and drops the header on /api/event if it arrives.
AuthorizationNoSame reason.
Accept-Language and othersNot neededThe language comes from the event body.

A rewrite (next.config.js rewrites, a catch-all proxy_pass or reverse_proxy without header rules) forwards the browser's request as it is, cookies included, because the script's request is same-origin. Every guide's snippet forwards only the headers above: Next.js, Express, NGINX, Caddy and the others in the install guides.

How DataPocket reads the visitor's IP

  1. The connecting address is the one Cloudflare reports (CF-Connecting-IP), trusted only on requests that really came through Cloudflare's network. For a proxied event that's your proxy server.
  2. If the request carries X-Forwarded-For, the visitor is the right-most public address in it that isn't the connecting address. Private and invalid addresses are skipped.
  3. Requests from Cloudflare Workers that didn't forward an IP get no location at all, rather than the Worker's own (which geolocates to Portland, Oregon).

So the proxy must put the visitor's address in X-Forwarded-For. If your proxy itself sits behind a CDN or load balancer, read the visitor's IP from what that layer reports (for example CF-Connecting-IP, or your platform's X-Forwarded-For), not from the socket.

Spoofed addresses

Bot filtering works with a proxy too. Requests that arrive straight from a browser without Sec-Fetch-* and Accept-Language headers are treated as scripts, but proxied requests are judged by the visitor's address in X-Forwarded-For, so a proxy running on a cloud server is fine as long as it forwards that header.

Anyone can send X-Forwarded-For. A claimed address changes only the location of the events that request sends, and the sender controls those events entirely anyway. Rate limits apply per connecting address as well as per claimed visitor (200 events per second per proxy, bursts to 4,000), so rotating claimed addresses doesn't get around them. Excluding an IP address in your settings matches the claimed address, so it's for your own office or test devices, not a security control.

Check it works

  1. Open your site with the browser's Network tab, find the POST to /dp/api/event and check it answers {"ok":true}.
  2. On your Install page, the site switches to "Receiving data" within a few seconds.
  3. In the live view, your own visit should show your city, not your server's.

Questions

Why does every visitor show the same city?
The proxy isn't forwarding the visitor's IP, so DataPocket sees your server. Set X-Forwarded-For to the visitor's address, read from your platform's own header if the proxy is behind a CDN.
Does the script work without a proxy?
Yes. The proxy only gets past ad blockers that block third-party analytics domains.

Related

First-party proxy: what to forward · DataPocket