> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Proxy CrispStats with Caddy

> Serve the CrispStats script and events from your own domain with Caddy: one handle_path block that rewrites to /p and strips cookies and authorization.

To proxy CrispStats with Caddy, add one `handle_path` block for `/k3x9qe/*` to your site in the Caddyfile. It rewrites the request to `/p/…`, removes the `Cookie` and `Authorization` headers and passes it to `https://in.crispstats.com`. Then point the script tag at the path with `data-api`, so ad blockers that stop known analytics hosts see a request to your own domain.

The dashboard's **Tracking code** tab has no Caddy example yet. This block follows the same rules as the other hosts, and the setup check below confirms it. Use your own path instead of `/k3x9qe`: **Settings**, **Tracking code**, **Proxy through your own domain** proposes one for your site. See [the proxy overview](https://docs.crispstats.com/tracking/proxy.md) for how the path, the check and signing work on every host.

## Set up the proxy

**1. Add the block to your site**

```text Caddyfile theme={"theme":{"light":"github-light","dark":"github-dark"}}
example.com {
	handle_path /k3x9qe/* {
		rewrite * /p{uri}
		reverse_proxy https://in.crispstats.com {
			header_up Host {upstream_hostport}
			# Never your visitors' cookies or authorization.
			header_up -Cookie
			header_up -Authorization
		}
	}

	# The rest of your site, unchanged.
}
```

`handle_path` strips `/k3x9qe` from the path, `rewrite` puts `/p` in front and keeps the query string, so `/k3x9qe/check?k=…` reaches `https://in.crispstats.com/p/check?k=…`. Recent Caddy versions set the `Host` header for an HTTPS upstream on their own; the `header_up Host` line keeps older versions working. Reload Caddy with `caddy reload`.

**2. Behind a load balancer or CDN, trust it**

Caddy sets `X-Forwarded-For` to the address that connected to it, and ignores the incoming value unless the sender is listed in `trusted_proxies`. If Caddy sits behind a load balancer or CDN, add its address ranges to `trusted_proxies` in the global `servers` options, or every visit looks like it comes from one address.

**3. Point the script tag at the path**

`src` loads the script from your domain, and `data-api` sends events and visitor id requests to the same path:

```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<script
  defer
  data-site="cs_pk_xxxxxxxx"
  data-domains="example.com"
  data-api="/k3x9qe"
  src="/k3x9qe/d.js"
></script>
```

`data-api` is required. Without it the script sends events to `/e` on your own domain, and those requests fail.

**4. Run the setup check**

Open the check through your proxy, in a browser, on your own site:

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
https://example.com/k3x9qe/check?k=cs_pk_xxxxxxxx
```

Look for `"proxied": true`, `"visitorAddressForwarded": true` and `"cookiesForwarded": false`. The [proxy overview](https://docs.crispstats.com/tracking/proxy.md#check-it-works) explains every field.

## What this block forwards

Unlike the nginx example, `reverse_proxy` passes request headers on by default, so the block removes the two that must never reach a third party: `Cookie` and `Authorization`. The collector needs only `content-type`, `user-agent`, `accept-language`, `referer`, `origin`, `if-none-match` and the visitor's address. If you want Caddy to send nothing else, remove more headers with further `header_up -Name` lines. Caddy also adds `X-Forwarded-Proto` and `X-Forwarded-Host`, which name your own site, not the visitor. See [what a proxy forwards](https://docs.crispstats.com/tracking/proxy.md#what-a-proxy-forwards).

## Visitor address and location

The collector takes the first public address in `X-Forwarded-For`, uses it for the daily visitor hash and your excluded IP addresses, then discards it. Caddy does not know where the visitor is, so the collector looks the country, region and city up from that address itself, as with nginx. See [location](https://docs.crispstats.com/tracking/proxy.md#location).

## Sign the requests (optional)

Signing proves a forwarded request came from your proxy. Reveal the secret (`cs_px_…`) in **Settings**, **Tracking code**, **Proxy through your own domain**, **Advanced: sign proxied requests**. Start Caddy with it in the environment as `CRISPSTATS_PROXY_SECRET` and add this line inside `reverse_proxy`:

```text Caddyfile theme={"theme":{"light":"github-light","dark":"github-dark"}}
header_up x-crispstats-proxy-secret {$CRISPSTATS_PROXY_SECRET}
```

`{$CRISPSTATS_PROXY_SECRET}` is read from the environment when Caddy loads the Caddyfile, so the secret stays out of the file. Reload, then look for `"signed": true` in the setup check. See [sign proxied requests](https://docs.crispstats.com/tracking/proxy.md#sign-proxied-requests).

## FAQ

> [accordion] Why handle\_path and rewrite instead of a path in reverse\_proxy?
>
> Caddy does not accept a path in the upstream address. `handle_path` removes your prefix and `rewrite` adds `/p`, which gives the same result.

> [accordion] Every visitor looks like one visitor. Why?
>
> Caddy is behind something that it does not trust, so `X-Forwarded-For` carries that hop's address. Add its ranges to `trusted_proxies`, reload, and check for `"visitorAddressForwarded": true`.

> [accordion] Does crispstats.id() work behind the proxy?
>
> Yes. It asks `/k3x9qe/id`, which the same block forwards.

## Next steps

> [card] [Proxy overview](https://docs.crispstats.com/tracking/proxy.md)
>
> The path, the check, caching, limits and signing.

> [card] [Proxy with nginx](https://docs.crispstats.com/tracking/proxy/nginx.md)
>
> The same rule for nginx.

> [card] [Ad blockers](https://docs.crispstats.com/help/ad-blockers.md)
>
> What a proxy recovers and what it does not.

> [!NOTE]
>
> Caddy details (`handle_path` stripping the prefix, `rewrite` changing path and query, no paths in upstream addresses, `header_up -Name`, the automatic `Host` for HTTPS upstreams since v2.11.0, `X-Forwarded-For` and `trusted_proxies`, and `{$ENV}` substitution) checked against [https://caddyserver.com/docs/caddyfile/directives/reverse\_proxy](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy), [https://caddyserver.com/docs/caddyfile/directives/handle\_path](https://caddyserver.com/docs/caddyfile/directives/handle_path) and [https://caddyserver.com/docs/caddyfile/concepts](https://caddyserver.com/docs/caddyfile/concepts) on 2026-11-23. This configuration was not run against the live collector while writing this page; the setup check is how you confirm it.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.