Skip to content
Plausible Analytics · Chapter 6 of 14 Day 4: HTTPS and Reverse Proxy

Day 4: HTTPS and Reverse Proxy

Goal: Caddy serves Plausible and itdrills.com over HTTPS, script and events run through your own domain past ad blockers, and you recognize and fix the classic behind every proxy: all visitors are one. · Duration: approx. 75 min · Prerequisite: Day 3

Briefing

  • Caddy is a web server with built-in HTTPS. In the lab it issues the certificates with its own lab CA (tls internal); on the internet it gets them from Let’s Encrypt – the same configuration without that line.
  • From then on Plausible only listens on 127.0.0.1:8000. From outside, everything goes through Caddy.
  • Ad blockers block known analytics addresses. If script and events come from itdrills.com/js/script.js and itdrills.com/api/event, the browser only sees the site itself – Caddy passes them on to Plausible in the background.
  • Plausible counts visitors via the IP address (Day 2). Behind a proxy, every request comes from the proxy. For the real IP to arrive, the proxy must write it into X-Forwarded-For – and trust an upstream proxy (Cloudflare).
 Browser ── https://itdrills.com/books/  ──► Caddy :443 ──► site (nginx) :8080
         ── https://itdrills.com/js/script.js ──► Caddy ──► Plausible /js/pa-….js
         ── https://itdrills.com/api/event    ──► Caddy ──► Plausible /api/event
 Admin   ── https://plausible.lab/        ──► Caddy ──► Plausible 127.0.0.1:8000

Drill 1: Caddy with a lab CA

The script ID from Day 1 goes into the rewrite of /js/script.js.

# on argo
cd ~/plausible/ce
ID=$(docker compose exec -T plausible_db psql -U postgres -d plausible_db \
  -tAc "select id from tracker_script_configuration")
mkdir -p ~/plausible/proxy && cd ~/plausible/proxy
cat > Caddyfile <<EOF
plausible.lab {
    tls internal
    reverse_proxy 127.0.0.1:8000
}

itdrills.com {
    tls internal
    handle /js/script.js {
        rewrite * /js/$ID.js
        reverse_proxy 127.0.0.1:8000 {
            header_up Host plausible.lab
        }
    }
    handle /api/event {
        reverse_proxy 127.0.0.1:8000 {
            header_up Host plausible.lab
        }
    }
    handle {
        reverse_proxy 127.0.0.1:8080
    }
}
EOF
docker run -d --name caddy --restart unless-stopped --network host \
  -v ~/plausible/proxy:/etc/caddy:ro \
  -v caddy_data:/data -v caddy_config:/config caddy:2.11.9-alpine >/dev/null
sleep 5
docker logs caddy 2>&1 | grep -c 'certificate obtained successfully'

Expected:

2

Two certificates, one per name. --network host lets Caddy listen directly on the VM’s ports 80/443 and reach 127.0.0.1:8000 and :8080. The certificates live in the volume caddy_data and survive a restart. Indentation in the Caddyfile uses tabs.

What is mounted is the folder ~/plausible/proxy, not the single file. Tools like sed -i or mv replace a file with a new one; with a file mounted on its own, the container would keep seeing the old one – a bug that shows up at the first caddy reload that has no effect.

Save your progress? Sign up for free

Drill 2: Trust the lab CA

curl does not know the lab CA yet. Its root certificate is inside the container.

# on argo
curl -s -o /dev/null https://plausible.lab/; echo "rc=$?"
docker cp caddy:/data/caddy/pki/authorities/local/root.crt /tmp/caddy-root.crt
sudo cp /tmp/caddy-root.crt /usr/local/share/ca-certificates/caddy-lab.crt
sudo update-ca-certificates 2>&1 | tail -1
openssl x509 -in /tmp/caddy-root.crt -noout -subject
curl -s -o /dev/null -w '%{http_code}\n' https://plausible.lab/api/health
curl -s https://itdrills.com/books/ | grep -o '<h1>.*</h1>'

Expected:

rc=60
done.
subject=CN = Caddy Local Authority - 2026 ECC Root
200
<h1>Books</h1>

rc=60 is curl’s code for “certificate not trusted”. Afterwards the lab CA is in the VM’s trust store. For the browser on your machine, copy /tmp/caddy-root.crt over and import it (macOS: Keychain Access, “System” keychain, set the certificate to “Always Trust”; Windows: certmgr.msc, “Trusted Root Certification Authorities”) – or click away the warning once in the lab.

Save your progress? Sign up for free

Drill 3: Plausible behind the proxy

BASE_URL becomes HTTPS without a port; port 8000 is only published on 127.0.0.1.

# on argo
cd ~/plausible/ce
sed -i 's#^BASE_URL=.*#BASE_URL=https://plausible.lab#' .env
sed -i 's#- 8000:8000#- 127.0.0.1:8000:8000#' compose.override.yml
docker compose up -d 2>&1 | tail -1
until curl -sf https://plausible.lab/api/health >/dev/null; do sleep 5; done
docker compose port plausible 8000
curl -s -m 3 -o /dev/null http://172.16.0.182:8000/; echo "von außen rc=$?"

Expected:

 Container ce-plausible-1 Started
127.0.0.1:8000
von außen rc=7

von außen means “from outside”. rc=7: connection refused – nobody gets to Plausible past Caddy any more.

Save your progress? Sign up for free

Drill 4: Script and events through your own domain

Two changes to the snippet: the script comes from /js/script.js, and init gets the endpoint /api/event – both relative to the page, so itdrills.com.

# on argo
cd ~/plausible/site
for f in $(find . -name index.html); do
  sed -i -e 's#src="http://plausible.lab:8000/js/pa-[^"]*"#src="/js/script.js"#' \
    -e 's#plausible.init()#plausible.init({endpoint: "/api/event"})#' "$f"
done
grep -ho 'src="[^"]*"\|plausible.init([^)]*)' index.html

Expected:

src="/js/script.js"
plausible.init({endpoint: "/api/event"})

The visit script needs two additions: it should accept the lab CA (ignoreHTTPSErrors) and be able to simulate an ad blocker (BLOCK, Drill 6). The new version replaces the old one:

# on argo
cd ~/plausible/pw
cat > besuch.js <<'EOF'
// Tag 4: wie Tag 3, dazu HTTPS mit Labor-CA und BLOCK (simulierter Werbeblocker)
const { chromium } = require('playwright');
(async () => {
  const args = process.env.ECHT
    ? ['--disable-blink-features=AutomationControlled'] : [];
  const b = await chromium.launch({ args });
  const opt = { ignoreHTTPSErrors: true };
  if (process.env.UA) opt.userAgent = process.env.UA;
  const p = await (await b.newContext(opt)).newPage();
  if (process.env.IP) await p.route('**/api/event', r => r.continue(
    { headers: { ...r.request().headers(), 'x-forwarded-for': process.env.IP } }));
  if (process.env.BLOCK) await p.route(u => u.hostname.includes(process.env.BLOCK),
    r => { console.log('blockiert', r.request().url()); return r.abort(); });
  p.on('request', r => {
    if (r.url().endsWith('/api/event')) console.log('->', r.postData());
  });
  p.on('response', r => {
    if (!r.url().endsWith('/api/event')) return;
    const weg = r.headers()['x-plausible-dropped'];
    console.log('<-', r.status(), weg ? 'verworfen' : 'gezählt');
  });
  p.on('console', m => { if (m.text().startsWith('Ignoring')) console.log(m.text()); });
  for (const a of process.argv.slice(2)) {
    if (a.startsWith('klick:')) await p.click(a.slice(6));
    else if (a === 'ignorieren')
      await p.evaluate(() => localStorage.plausible_ignore = 'true');
    else await p.goto(a, { referer: process.env.REFERER });
    await p.waitForTimeout(1500);
  }
  await b.close();
})();
EOF
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
UA="$UA Chrome/140.0.0.0 Safari/537.36"
docker run --rm --network host --user "$(id -u):$(id -g)" -e HOME=/tmp -e ECHT=1 \
  -e UA="$UA" -v ~/plausible/pw:/pw -w /pw mcr.microsoft.com/playwright:v1.65.0-noble \
  node besuch.js https://itdrills.com/coming-soon/

Expected:

-> {"n":"pageview","v":33,"u":"https://itdrills.com/coming-soon/","d":"itdrills.com",…
<- 202 gezählt

(abridged)

The new comment reads “Day 4: like Day 3, plus HTTPS with lab CA and BLOCK (simulated ad blocker)”; blocked requests are printed as blockiert (“blocked”). In the browser it looks as if the page only talks to itself. header_up Host plausible.lab makes sure Plausible assigns the request to its own name.

Save your progress? Sign up for free

Drill 5: All visitors are one

Break it: Three visitors with three different IPs (IP sets X-Forwarded-For, Day 3) – like three people behind an upstream proxy.

# on argo
cd ~/plausible/pw
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
UA="$UA Chrome/140.0.0.0 Safari/537.36"
T=$(date -u '+%F %T'); echo "$T" > ~/plausible/tag04-start
I=mcr.microsoft.com/playwright:v1.65.0-noble
for ip in 203.0.113.30 203.0.113.31 203.0.113.32; do
  docker run --rm --network host --user "$(id -u):$(id -g)" -e HOME=/tmp -e ECHT=1 \
    -e UA="$UA" -e IP=$ip -v ~/plausible/pw:/pw -w /pw \
    $I node besuch.js https://itdrills.com/books/ | tail -1
done
sleep 5

Expected:

<- 202 gezählt
<- 202 gezählt
<- 202 gezählt

Symptom:

# on argo
cd ~/plausible/ce
T=$(cat ~/plausible/tag04-start)
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select count(), uniqExact(user_id) from events_v2
           where name = 'pageview' and timestamp >= '$T'"

Expected:

3   1

Three pageviews, one visitor.

Cause: Caddy only trusts an incoming X-Forwarded-For if the request comes from a trusted proxy. Otherwise it replaces the value with the address it sees itself – here always the VM. That is correct (otherwise any visitor could invent an IP), but behind Cloudflare or a load balancer it means: all visitors come from the proxy.

Fix: Tell Caddy whom it may trust. In the lab the “upstream proxy” is the VM itself; behind Cloudflare, its address ranges would go here (Enterprise Note).

# on argo
cd ~/plausible/proxy
{ printf '{\n\tservers {\n\t\ttrusted_proxies static 127.0.0.1/8 ::1\n\t}\n}\n\n'
  cat Caddyfile; } > Caddyfile.neu
mv Caddyfile.neu Caddyfile
docker exec caddy caddy validate --config /etc/caddy/Caddyfile 2>&1 | tail -1
docker exec caddy caddy reload --config /etc/caddy/Caddyfile 2>/dev/null; echo "rc=$?"

Expected:

Valid configuration
rc=0

Verify:

# on argo
cd ~/plausible/pw
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
UA="$UA Chrome/140.0.0.0 Safari/537.36"
T=$(date -u '+%F %T')
I=mcr.microsoft.com/playwright:v1.65.0-noble
for ip in 203.0.113.30 203.0.113.31 203.0.113.32; do
  docker run --rm --network host --user "$(id -u):$(id -g)" -e HOME=/tmp -e ECHT=1 \
    -e UA="$UA" -e IP=$ip -v ~/plausible/pw:/pw -w /pw \
    $I node besuch.js https://itdrills.com/books/ >/dev/null
done
sleep 5
cd ~/plausible/ce
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select count(), uniqExact(user_id) from events_v2
           where name = 'pageview' and timestamp >= '$T'"

Expected:

3   3

Save your progress? Sign up for free

Drill 6: The ad blocker

A test page embeds Plausible directly, as before this day – with the snippet from Day 3, just with the HTTPS address. BLOCK=plausible simulates a block list that blocks everything with “plausible” in the host name.

# on argo
mkdir -p ~/plausible/site/direkt
{ printf '<!doctype html>\n<html lang="en"><head><meta charset="utf-8">'
  printf '<title>Direkt</title>\n'
  sed 's#http://plausible.lab:8000#https://plausible.lab#' ~/plausible/site/snippet.html
  printf '</head>\n<body><h1>Direkt</h1></body></html>\n'
} > ~/plausible/site/direkt/index.html
grep -o 'src="[^"]*"' ~/plausible/site/direkt/index.html | cut -c1-40
cd ~/plausible/pw
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
UA="$UA Chrome/140.0.0.0 Safari/537.36"
for u in https://itdrills.com/direkt/ https://itdrills.com/coming-soon/; do
  echo "== $u"
  docker run --rm --network host --user "$(id -u):$(id -g)" -e HOME=/tmp -e ECHT=1 \
    -e UA="$UA" -e BLOCK=plausible -v ~/plausible/pw:/pw -w /pw \
    mcr.microsoft.com/playwright:v1.65.0-noble node besuch.js "$u" | cut -c1-60
done

Expected:

src="https://plausible.lab/js/pa--JsuJY
== https://itdrills.com/direkt/
blockiert https://plausible.lab/js/pa--JsuJYgBCST1JOUHGfKkS.
== https://itdrills.com/coming-soon/
-> {"n":"pageview","v":33,"u":"https://itdrills.com/coming
<- 202 gezählt

Embedded directly (direkt), the script never even arrives. Through your own domain there is nothing to block. Real block lists (EasyPrivacy, the uBlock Origin lists) know plausible.io and typical paths; the path /js/script.js was chosen on purpose to be inconspicuous.

Tip: Whether you should bypass ad blockers is a matter of principle. Plausible sets no cookies and stores no IPs; many site operators therefore consider the proxy acceptable. If you see it differently, keep the script embedded directly.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Show which names Caddy holds certificates for. Target time: 30 s
  2. Show that port 80 redirects to HTTPS. Target time: 20 s
  3. Show until when the certificate of itdrills.com is valid. Target time: 30 s

Solution

# on argo
docker exec caddy ls /data/caddy/certificates/local
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' http://itdrills.com/books/
echo | openssl s_client -connect itdrills.com:443 -servername itdrills.com 2>/dev/null \
  | openssl x509 -noout -enddate

Expected (date differs):

itdrills.com
plausible.lab
308 https://itdrills.com/books/
notAfter=Nov 21 09:40:46 2026 GMT

Certificates from the lab CA are only valid for twelve hours – Caddy renews them on its own.

Save your progress? Sign up for free

Checkpoint

# on argo
curl -s https://plausible.lab/api/health; echo
curl -s -o /dev/null -w '%{http_code}\n' https://itdrills.com/js/script.js
grep -c 'trusted_proxies' ~/plausible/proxy/Caddyfile
grep BASE_URL ~/plausible/ce/.env

Expected:

{"sessions":"ok","postgres":"ok","clickhouse":"ok","sites_cache":"ok"}
200
1
BASE_URL=https://plausible.lab

Challenge

Only the admin needs the dashboard at plausible.lab. Since script and events run through itdrills.com, plausible.lab can be locked for everyone else. In the Caddyfile, only let requests from 127.0.0.1 and ::1 through; everyone else gets 403. Check it with a request via the VM’s LAN address.

Hint: Caddy has the matcher remote_ip; with --resolve, curl sends the name to a specific address.

Solution

# on argo
cd ~/plausible/proxy
R='\t@fremd not remote_ip 127.0.0.1/8 ::1\n\trespond @fremd 403'
sed -i "0,/^\ttls internal$/s##&\n$R#" Caddyfile
sed -n '/^plausible.lab/,/^}/p' Caddyfile
docker exec caddy caddy reload --config /etc/caddy/Caddyfile 2>/dev/null; echo "rc=$?"
curl -s -o /dev/null -w 'lokal %{http_code}\n' https://plausible.lab/api/health
curl -s -o /dev/null -w 'LAN %{http_code}\n' --resolve plausible.lab:443:172.16.0.182 \
  https://plausible.lab/api/health

Expected:

plausible.lab {
    tls internal
    @fremd not remote_ip 127.0.0.1/8 ::1
    respond @fremd 403
    reverse_proxy 127.0.0.1:8000
}
rc=0
lokal 200
LAN 403

@fremd (“foreign”) is just the matcher’s name, lokal means “local”. remote_ip checks the address of the direct connection, not X-Forwarded-For – a visitor cannot sneak in that way. This rule now also keeps out your own browser; for the exercises of the following days, remove the lines again or add your machine’s address (Cleanup).

Save your progress? Sign up for free

Cleanup

The test page and the dashboard lock go away again; Caddy, the lab CA and trusted_proxies stay.

# on argo
rm -r ~/plausible/site/direkt ~/plausible/tag04-start
cd ~/plausible/proxy
sed -i -e '/@fremd not remote_ip/d' -e '/respond @fremd 403/d' Caddyfile
docker exec caddy caddy reload --config /etc/caddy/Caddyfile 2>/dev/null; echo "rc=$?"
curl -s -o /dev/null -w 'LAN %{http_code}\n' --resolve plausible.lab:443:172.16.0.182 \
  https://plausible.lab/api/health

Expected:

rc=0
LAN 200

Interview Note

  • Why does Plausible behind Cloudflare count all visitors as one? The reverse proxy only accepts X-Forwarded-For from trusted proxies; without trusted_proxies (or set_real_ip_from in nginx) Plausible only sees Cloudflare.
  • How do you get around ad blockers with Plausible? Serve script and events from your own domain and pass them on via a reverse proxy.
  • Why bind Plausible to 127.0.0.1? So that nobody gets in past the proxy – and Docker ports bypass the host firewall.

Enterprise Note

  • On the internet: drop tls internal, point DNS at the server, and Caddy fetches Let’s Encrypt certificates itself. Plausible can do that on its own too (HTTP_PORT=80, HTTPS_PORT=443 in .env) – but with your own proxy you keep the script path, access rules and other sites in one hand.
  • Behind Cloudflare: trusted_proxies with the published Cloudflare ranges (there is a Caddy module that loads them automatically) and client_ip_headers CF-Connecting-IP.
  • The nginx counterpart: real_ip_header X-Forwarded-For; set_real_ip_from <range>; and proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; when passing requests on.
  • Content Security Policy: with the proxy, 'self' is enough for script-src and connect-src – an advantage that often weighs more than the ad blocker.

Contents

More