knownprintDocs

Install

Install on nginx

Add knownprint's proxy rule to an nginx server step by step, from copying the files and storing the proxy secret to the checks with curl.

This page installs the knownprint proxy rule on nginx. 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 explains what the rule does and why; this page is the procedure.

If Cloudflare proxies your site's DNS (the orange cloud), follow this page but use the Cloudflare variant in step 5, as Sites behind Cloudflare explains.

Before you start

You need:

  • nginx serving your site over https, with a server block you can edit, and root on the server.
  • The nginx files from your knownprint account:
FileWhat it isWhere it goes
knownprint-map.confDefinitions nginx needs once, at the http level: they decide which cookies may reach knownprint/etc/knownprint/knownprint-map.conf, included once in the http block
knownprint.confThe rule itself: the /kp/ locations and the disclosure file location/etc/knownprint/knownprint.conf, included in your site's TLS server block
knownprint-cloudflare.confknownprint.conf for a site behind Cloudflare's proxied DNSInstead of knownprint.conf, only behind Cloudflare
proxy-secret.conf.exampleAn example of the proxy secret fileCopied to /etc/knownprint/proxy-secret.conf with your secret in it
knownprint.jsonYour 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).

knownprint.conf includes /etc/knownprint/proxy-secret.conf and serves the disclosure file from /usr/local/share/knownprint/knownprint.json, so keep those two paths. The other paths on this page are suggestions; if you put knownprint-map.conf and knownprint.conf elsewhere, change the two include lines to match.

Step 1: Copy the files

The nginx worker processes run under a user and group of their own: www-data on Debian and Ubuntu, nginx on Fedora, RHEL and the packages from nginx.org. Check yours:

Shell
grep -E '^\s*user\s' /etc/nginx/nginx.conf

Create /etc/knownprint, readable by root and that group only, and copy the two configuration files into it. The commands use www-data; put your group in its place:

Shell
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/

Behind Cloudflare, copy knownprint-cloudflare.conf as well.

Do not put knownprint.conf in /etc/nginx/conf.d/ or any other directory your nginx.conf includes from the http block: it holds location blocks, which belong in a server block, and nginx refuses to start with them at the http level.

Step 2: Store the proxy secret

knownprint.conf reads the secret from /etc/knownprint/proxy-secret.conf, a file of one line that sets the nginx variable $knownprint_proxy_secret. Create the file empty with the right owner and mode first, then write the secret into it with an editor, so the secret never appears on a command line or in your shell history:

Shell
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 exactly this line, with your environment's proxy secret between the quotes:

/etc/knownprint/proxy-secret.conf
set $knownprint_proxy_secret "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

Keep this file out of version control. If you manage nginx with configuration management, write the file from your secret store at deploy time with the same owner and mode.

Step 3: Install the disclosure file

Your server publishes knownprint.json at /.well-known/knownprint.json. It must be readable by the worker processes, so it lives outside /etc/knownprint:

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

Before you go live, edit its operator, rights_url and contact fields to describe your business, and leave the other fields as delivered. The disclosure file explains every field.

Step 4: Include the map file in the http block

Add one include line inside the http block of /etc/nginx/nginx.conf, next to your other includes. It must appear once in the whole configuration, and never inside a server block:

/etc/nginx/nginx.conf
http {
    # ... your existing settings ...

    include /etc/knownprint/knownprint-map.conf;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

The file only defines variables. It changes nothing until knownprint.conf uses them, so it is safe to add on its own.

Step 5: Include the recipe in your site's server block

Add one include line to the TLS server block of every host whose pages load the knownprint script. Put it before your own location blocks:

/etc/nginx/sites-available/www.example.com
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;

    ssl_certificate     /etc/ssl/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/ssl/www.example.com/privkey.pem;

    include /etc/knownprint/knownprint.conf;

    # Your site. Pass the visitor's address and scheme on to your application.
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

nginx sends no X-Forwarded-For to your application unless you set it. Your backend compares the event's network.ip with the address your proxy appended to that header (The visitor address), so keep the two proxy_set_header lines in the location that serves your application; without them the address check fails for every visitor. If your application location already sets them, leave it as it is.

Behind Cloudflare's proxied DNS, include /etc/knownprint/knownprint-cloudflare.conf instead. Include one of the two, never both.

If example.com and www.example.com both serve pages that load the script, both server blocks get the include. A plain http server block that only redirects to https does not need it.

The include adds these locations to the server block:

LocationWhat it serves
location = /kp/agent.jsThe loader. No cookie in either direction; cached for an hour by browsers and a minute by shared caches.
location ^~ /kp/a/The script files. No cookie in either direction; they keep knownprint's one-year immutable cache header.
location ^~ /kp/, with a nested location for /kp/c, /kp/i, /kp/l/ and /kp/b/Every other /kp/ request. Only knownprint's cookie is forwarded; the identification paths are no-store.
location = /.well-known/knownprint.jsonYour disclosure file, as application/json.

Exact (=) and ^~ locations win over regular expression locations, so a rule of yours such as location ~* \.js$ that sets long cache headers for static files does not apply to these paths. If your server block already has a location /kp/ or location ^~ /kp/ of its own, remove it: nginx refuses two locations for the same path.

Each knownprint location sets its own proxy headers, so proxy_set_header lines at your server level do not reach knownprint. That is intended; do not add any to the knownprint locations.

Step 6: Repeat server-wide response headers

nginx applies the add_header lines of a server block only to locations that set no add_header of their own. Two knownprint locations set Cache-Control: the loader (location = /kp/agent.js) and the identification paths (the nested location ~ ^/kp/(c|i|l/|b/)). If your server block sets response headers with add_header, such as Strict-Transport-Security, those two locations do not send them.

If you need them on every answer, add the same lines to those two locations in your copy of the file, beside the recipe's own add_header, and keep every other line as it is:

/etc/knownprint/knownprint.conf (excerpt)
location = /kp/agent.js {
    # ... the recipe's lines, unchanged ...
    add_header Cache-Control "public, max-age=3600, s-maxage=60" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}

Do the same inside the nested identification location. Remember these additions when you install a new version of the file.

Step 7: Test and reload

Shell
sudo nginx -t && sudo systemctl reload nginx

Run nginx -t with sudo: the secret file is readable only by root and the worker group, so a test as your own user fails with Permission denied. A reload keeps open connections and takes the new configuration without downtime. If the test fails, nginx keeps running the old configuration; see Troubleshooting.

Step 8: Check with curl

From any machine, check the loader:

Shell
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:

Shell
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 your rule. A 404 page of your own site means the include is not in the server block that answers for this host; a 502 or 504 means nginx cannot reach knownprint.

Check the disclosure file:

Shell
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 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. The visitor's address explains what a wrong address means.

Several servers

Every nginx server that answers for your site needs the same files and the same proxy secret: all instances behind a load balancer, every region. If a load balancer or CDN other than Cloudflare stands in front of nginx, configure the real IP module so nginx sees the visitor's address (Sites behind a CDN or a load balancer).

When knownprint sends you a new version of the files, replace each file whole (keeping your step 6 additions), run nginx -t and reload, on every server in the same change.

Troubleshooting

Errors from nginx -t:

MessageCauseFix
open() "/etc/knownprint/proxy-secret.conf" failed (2: No such file or directory)The secret file is missingCreate it (step 2).
open() "/etc/knownprint/..." failed (13: Permission denied)nginx -t ran without sudo, or the files belong to the wrong groupRun it with sudo; check owner root, the worker group, and modes 0750 and 0640.
unknown "knownprint_proxy_secret" variableThe secret file exists but does not set the variableWrite the one set line exactly as in step 2.
unknown "knownprint_..." variable for any other variable of the recipeThe map file is not included in the http block, or it is older than your knownprint.confInclude the knownprint-map.conf that came with your knownprint.conf (step 4); always install the two files together.
"map" directive is not allowed hereThe map file is included inside a server blockMove its include to the http block.
"location" directive is not allowed hereknownprint.conf is included at the http level, often because it sits in /etc/nginx/conf.d/Move it out of that directory and include it in the server block.
duplicate location "/kp/"Your server block has its own /kp/ locationRemove yours.
unknown directive "real_ip_header"The Cloudflare variant on an nginx built without the real IP moduleInstall an nginx package that includes it; most distribution and nginx.org packages do.

Problems at run time:

What you seeCauseFix
/.well-known/knownprint.json answers 403The worker processes cannot read the file or a directory above itMode 0644 for the file and 0755 for /usr/local/share/knownprint.
The loader or the identification answers lack your Strict-Transport-Security or other headersnginx does not inherit add_header into those locationsRepeat them there (step 6).
Events show your server's addressThe secret is wrong or of another environment, or the request skipped the ruleCheck the secret, then The visitor's address.

The general troubleshooting table, for load_failed, 403 and 429 answers and visitors who are not recognised, is on Run knownprint from your domain.