# knownprint proxy rule: nginx

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 nginx, and on nginx behind Cloudflare's proxied DNS.

Prepare these commands for the user's server; run them yourself only with the user's go-ahead. Use
the recipe files from the account as they are.

## Install

1. Copy the files to `/etc/knownprint/`, readable by root and the nginx workers' group only
   (`www-data` on Debian and Ubuntu, `nginx` on Fedora, RHEL and the nginx.org packages; check with
   `grep -E '^\s*user\s' /etc/nginx/nginx.conf`). Behind Cloudflare's proxied DNS, copy
   `knownprint-cloudflare.conf` as well:

   ```bash
   sudo install -d -m 0750 -o root -g www-data /etc/knownprint
   sudo install -m 0640 -o root -g www-data knownprint-map.conf knownprint.conf /etc/knownprint/
   ```

2. The user stores the proxy secret, so it never appears on a command line:

   ```bash
   sudo install -m 0640 -o root -g www-data /dev/null /etc/knownprint/proxy-secret.conf
   sudoedit /etc/knownprint/proxy-secret.conf
   ```

   The file holds the one line of `proxy-secret.conf.example`, with the secret in it.

3. Put the disclosure file where the recipe serves it from:

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

4. Include the two files:

   ```nginx
   http {
       # once, inside the http block:
       include /etc/knownprint/knownprint-map.conf;

       server {
           listen 443 ssl;
           server_name www.example.com;
           # ... the site's existing TLS and site configuration ...

           # inside the site's TLS server block
           # (behind Cloudflare's proxied DNS: knownprint-cloudflare.conf instead):
           include /etc/knownprint/knownprint.conf;

           location / {
               proxy_pass http://127.0.0.1:3000;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header X-Forwarded-Proto $scheme;
           }
       }
   }
   ```

   Never put `knownprint.conf` in `conf.d/` or any directory included at the `http` level: it
   holds `location` blocks. The backend needs the visitor's address (Step 6):
   - With `proxy_pass`, the app's location must send `X-Forwarded-For` as above. nginx gives a
     location that sets any `proxy_set_header` none of the `proxy_set_header` lines of the
     `server` or `http` level, so if those levels set some (`Host`, `Upgrade`, `Connection`),
     copy them into the location beside the two new lines.
   - With `fastcgi_pass` (PHP-FPM), add nothing: `REMOTE_ADDR` is already the visitor's address.
   - If the site block sets response headers with `add_header`, the recipe's locations do not
     inherit them; https://docs.knownprint.com/install/nginx.md (step 6) says what to repeat.

5. Test and reload: `sudo nginx -t && sudo systemctl reload nginx`.

Behind Cloudflare, also make sure no Cloudflare cache rule caches `/kp/` paths, and keep the
Cloudflare address ranges in `knownprint-cloudflare.conf` current:
https://docs.knownprint.com/install/cloudflare.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.
