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
| Feature | Forward? | Why |
|---|---|---|
| Request body | Yes | The event itself (JSON, sent as text/plain). |
User-Agent | Yes | Browser, OS, device and bot filtering. |
X-Forwarded-For | Yes: the visitor's IP | City-level location. The IP is never stored. |
Cookie | No | Your visitors' cookies: auth sessions, other vendors' ids. DataPocket never reads them, and drops the header on /api/event if it arrives. |
Authorization | No | Same reason. |
Accept-Language and others | Not needed | The 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
- 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. - 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. - 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
- Open your site with the browser's Network tab, find the POST to
/dp/api/eventand check it answers{"ok":true}. - On your Install page, the site switches to "Receiving data" within a few seconds.
- 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-Forto 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.