---

title: Install on Caddy
description: Add knownprint's proxy rule to a Caddy server with one imported snippet, give Caddy the proxy secret as KNOWNPRINT_PROXY_SECRET, and check the result.
---

This page installs the knownprint proxy rule on Caddy 2. When it is done, your site answers `/kp/` paths by forwarding them to knownprint, and serves your disclosure file at `/.well-known/knownprint.json`. [Run knownprint from your domain](/install/proxy) explains what the rule does and why; this page is the procedure.

The rule is one Caddyfile snippet named `knownprint`. It reads the proxy secret from the environment variable `KNOWNPRINT_PROXY_SECRET` while it runs, so the secret is never written into your Caddyfile.

## Before you start

You need:

- Caddy 2 serving your site with a Caddyfile, run by systemd as the `caddy` service (the setup of the official packages), and root on the server.
- The files from your knownprint account:

| File               | What it is                                                                      | Where it goes                                                              |
| ------------------ | ------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| `knownprint.caddy` | The snippet `(knownprint) { ... }`: the `/kp/` handling and the disclosure file | `/etc/caddy/knownprint.caddy`, imported at the top level of your Caddyfile |
| `knownprint.json`  | Your disclosure file                                                            | `/usr/local/share/knownprint/knownprint.json`                              |
- Your environment's proxy secret: 32 characters. Use the secret of the environment whose public key your pages load ([The proxy secret](/install/proxy#the-proxy-secret)).

The snippet serves the disclosure file from `/usr/local/share/knownprint/`, so keep that path. The other paths on this page are suggestions.

:::note Early access
knownprint is in early access and signup is not open yet ("Get early access" at [knownprint.com](https://knownprint.com)). The snippet and the proxy secret come with your account.
:::

The Caddy snippet passes on the address of the connection Caddy accepted. Run it on the server that receives your visitors' connections directly. If a CDN or a load balancer stands in front of Caddy, talk to your knownprint contact first ([Sites behind a CDN or a load balancer](/install/proxy#sites-behind-a-cdn-or-a-load-balancer)); for a site behind Cloudflare, see [Sites behind Cloudflare](/install/cloudflare).

## Step 1: Install the snippet and the disclosure file

```bash
sudo install -m 0644 knownprint.caddy /etc/caddy/knownprint.caddy
sudo install -d -m 0755 /usr/local/share/knownprint
sudo install -m 0644 knownprint.json /usr/local/share/knownprint/knownprint.json
```

The snippet holds no secret, so it can be readable by everyone. Before you go live, edit the disclosure file's `operator`, `rights_url` and `contact` fields to describe your business and leave the other fields as delivered. [The disclosure file](/privacy#the-disclosure-file) explains every field.

## Step 2: Import the snippet in your Caddyfile

Import the file at the top level of the Caddyfile, outside every site block and before the first site block that uses it: that defines the snippet. Then write `import knownprint` inside each site block whose pages load the knownprint script:

```caddy title="/etc/caddy/Caddyfile"
{
	admin off
}

import /etc/caddy/knownprint.caddy

www.example.com {
	import knownprint

	# Your site, unchanged.
	handle {
		reverse_proxy localhost:3000
	}
}
```

The snippet adds two `handle` blocks to the site: `handle /kp/*` forwards every `/kp/` request to knownprint, and `handle /.well-known/knownprint.json` serves your disclosure file as a static file. A request either of them matches is answered there and never reaches the rest of your site block.

`reverse_proxy` sends your application the visitor's address in `X-Forwarded-For`, and the scheme in `X-Forwarded-Proto`, without further configuration; your backend's address check reads it from there ([The visitor address](/install/server#the-visitor-address)).

Wrap your own handling in a `handle` block of its own, as above. Caddy runs `handle` blocks at the same level mutually exclusively, so `/kp/` requests are never passed to your application as well. Without the wrapper Caddy still sorts the directives by its default order, but the explicit block keeps the routing obvious when you add more.

If `example.com` and `www.example.com` both serve pages that load the script, both site blocks get `import knownprint`.

The global option `admin off` belongs to step 3: it is explained there.

## Step 3: Give Caddy the proxy secret

The snippet reads the secret through the placeholder `{env.KNOWNPRINT_PROXY_SECRET}`, which Caddy resolves for each request from its own environment. The secret therefore never becomes part of Caddy's configuration. Three settings keep it from leaking another way.

**Keep the secret in a root-only environment file.** Create the file empty with mode `0600`, then write the secret with an editor so it never appears on a command line:

```bash
sudo install -m 0600 -o root -g root /dev/null /etc/caddy/knownprint.env
sudoedit /etc/caddy/knownprint.env
```

```text title="/etc/caddy/knownprint.env"
KNOWNPRINT_PROXY_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

systemd reads this file as root before it starts Caddy, so the `caddy` user never needs to read it.

**Load it with a systemd drop-in, and drop `--environ`.** Open a drop-in for the service:

```bash
sudo systemctl edit caddy
```

and write:

```text title="/etc/systemd/system/caddy.service.d/override.conf"
[Service]
EnvironmentFile=/etc/caddy/knownprint.env
ExecStart=
ExecStart=/usr/bin/caddy run --config /etc/caddy/Caddyfile
```

The empty `ExecStart=` clears the command of the stock unit, and the second line starts Caddy without `--environ`. The stock unit passes `--environ`, which prints Caddy's whole environment, the secret with it, to the journal at every start. If your unit starts Caddy differently, copy its `ExecStart` line and remove only `--environ`. Check the current line with `systemctl cat caddy`.

**Close the admin endpoint.** Caddy's admin endpoint answers any local process: it returns the running configuration and accepts a new one. The placeholder keeps the secret out of that configuration, but a process that can load a configuration of its own can make Caddy use the secret anywhere. Turn the endpoint off, or bind it to a Unix socket that only root and the `caddy` user can open. The Caddyfile in step 2 uses `admin off`. With the endpoint off, Caddy cannot reload its configuration in place: apply changes with a restart.

:::warning
Do not write the secret as `{$KNOWNPRINT_PROXY_SECRET}`. That form is replaced when the Caddyfile is loaded, so the secret becomes part of the configuration that the admin endpoint and `caddy adapt` show. Keep the snippet's `{env.KNOWNPRINT_PROXY_SECRET}` exactly as delivered.
:::

## Step 4: Validate and restart

```bash
sudo caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
sudo systemctl daemon-reload
sudo systemctl restart caddy
```

`caddy validate` checks the Caddyfile without starting the server. `daemon-reload` makes systemd read the drop-in (`systemctl edit` does that on its own; the line is there if you wrote the drop-in by hand). The restart applies both the new Caddyfile and the new environment. Check that the service is running and that it loads the environment file, without printing the secret:

```bash
systemctl is-active caddy
systemctl show caddy -p EnvironmentFiles
```

The second command names `/etc/caddy/knownprint.env`.

## Step 5: Check with curl

From any machine, check the loader:

```bash
curl -sSI https://www.example.com/kp/agent.js
```

The answer is `200` with these headers, and no `Set-Cookie`:

```http
content-type: text/javascript; charset=utf-8
cache-control: public, max-age=3600, s-maxage=60
```

Check an identification path with an empty body:

```bash
curl -sS -D - -o /dev/null -X POST https://www.example.com/kp/c \
  -H 'Content-Type: application/json' -d '{}'
```

The answer is `400` from knownprint, which refuses the empty body, with `content-type: application/problem+json; charset=utf-8` and `cache-control: no-store`. That proves the request reached knownprint through the snippet. An answer from your own application means the `import knownprint` line is missing from this site block; a `502` means Caddy cannot reach knownprint.

Check the disclosure file:

```bash
curl -sS https://www.example.com/.well-known/knownprint.json
```

The answer is your JSON file, with your own `operator`.

Last, the check that matters most: add [the browser agent](/install/browser) to a page, identify your own browser, and read the event from your backend. Its `network.ip` must be the public address of the machine you browsed from, not your server's. If it is your server's, Caddy did not have the secret when it started: check step 3 and restart. [The visitor's address](/install/proxy#the-visitor-s-address) explains what a wrong address means.

## Rotating the secret

Create the new secret as [The proxy secret](/install/proxy#the-proxy-secret) describes, write it into `/etc/caddy/knownprint.env` on every server, run `sudo systemctl restart caddy`, and only then retire the old secret. Caddy reads its environment only at start, so the new value is in use only after the restart.

## Updating the snippet

When knownprint sends you a new version of `knownprint.caddy`, replace the file whole, validate and restart, on every server that answers for your site in the same change.

## Troubleshooting

| What you see                                                                                           | Cause                                                                                                     | Fix                                                                                                 |
| ------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
| `caddy validate` fails at `import knownprint`, for example with `File to import not found: knownprint` | The top-level `import /etc/caddy/knownprint.caddy` is missing, or comes after the site block that uses it | Put the import at the top level, before the site blocks.                                            |
| `systemctl reload caddy` fails                                                                         | The admin endpoint is off, so Caddy cannot reload in place                                                | Use `systemctl restart caddy`.                                                                      |
| `/kp/agent.js` is answered by your application                                                         | The site block lacks `import knownprint`                                                                  | Add it to every site block whose pages load the script.                                             |
| Events show your server's address, or many `429` answers                                               | `KNOWNPRINT_PROXY_SECRET` is empty or wrong in Caddy's environment, or belongs to another environment     | Check the environment file and the drop-in, then restart.                                           |
| Events show a CDN's or load balancer's address                                                         | Caddy does not receive visitors' connections directly                                                     | See [Sites behind a CDN or a load balancer](/install/proxy#sites-behind-a-cdn-or-a-load-balancer).  |
| `/.well-known/knownprint.json` answers`404`                                                            | The file is not at `/usr/local/share/knownprint/knownprint.json`                                          | Install it there, mode `0644`.                                                                      |
| The journal shows the secret at start                                                                  | `--environ` is still in the command Caddy starts with                                                     | Remove it with the drop-in in step 3; restart.                                                      |

The general troubleshooting table, for `load_failed`, `403` and `429` answers and visitors who are not recognised, is on [Run knownprint from your domain](/install/proxy#troubleshooting).
