# knownprint proxy rule: Caddy

Part of the knownprint quickstart for AI coding agents, Step 4. Read
https://knownprint.com/quickstart.md first: it holds the rules that apply here (never read or print
secrets, ask before touching a live server, an unknown result is never a pass). This file covers the
proxy rule on Caddy.

Prepare these commands for the user's server; run them yourself only with the user's go-ahead. Use
the snippet from the account as it is.

## Install

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
   ```

2. Import it at the top level of the Caddyfile, then in each site block whose pages load the
   script. Close the admin endpoint, so no local process can make Caddy use the secret elsewhere;
   `reverse_proxy` already sends `X-Forwarded-For`:

   ```caddy
   {
   	admin off
   }

   import /etc/caddy/knownprint.caddy

   www.example.com {
   	import knownprint

   	handle {
   		reverse_proxy localhost:3000
   	}
   }
   ```

3. The user stores the proxy secret in a root-only environment file:

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

   It holds one line, `KNOWNPRINT_PROXY_SECRET=` followed by the secret. The snippet reads it as
   `{env.KNOWNPRINT_PROXY_SECRET}`; never write it as `{$KNOWNPRINT_PROXY_SECRET}`, which would put
   the secret into Caddy's configuration.

4. Load the file with a systemd drop-in (`sudo systemctl edit caddy`) and start Caddy without
   `--environ`, which would print the whole environment, the secret with it, to the journal:

   ```text
   [Service]
   EnvironmentFile=/etc/caddy/knownprint.env
   ExecStart=
   ExecStart=/usr/bin/caddy run --config /etc/caddy/Caddyfile
   ```

   If the unit starts Caddy differently (`systemctl cat caddy`), copy its `ExecStart` line and
   remove only `--environ`.

5. Validate and restart (with the admin endpoint off, Caddy applies changes by a restart):
   `caddy validate --config /etc/caddy/Caddyfile && sudo systemctl daemon-reload && sudo systemctl restart caddy`.
   Details: https://docs.knownprint.com/install/caddy.md

## Check

```bash
curl -sS -o /dev/null -w '%{http_code}\n' https://www.example.com/kp/agent.js
curl -sS -o /dev/null -w '%{http_code}\n' https://www.example.com/.well-known/knownprint.json
```

Both answer `200`. If not, see https://docs.knownprint.com/install/proxy.md (troubleshooting).

Next: https://knownprint.com/quickstart.md, Step 5.
