Files
lucioandClaude Opus 5.5 e48a1eacf4 Keep hotlink 403s out of CDN and browser caches
The tile hotlink refusal is sent with Cache-Control: no-store from
LuPhoto, Caddy and nginx instead of inheriting the one-year immutable
header. cloudflare.md explains that edge-cached tiles bypass the
origin Referer check, recommends a WAF rule for /t/, and notes that
tile URLs depend only on the photo, the settings and the site key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 22:49:45 -08:00

3.2 KiB

Cloudflare

Run the site behind a Cloudflare Tunnel (cloudflared.yml in this folder). In serve mode the tunnel points at LuPhoto on 127.0.0.1:8080. In export mode it points at Caddy or nginx, and LuPhoto itself listens on server.listen (127.0.0.1:8080 by default) for the key endpoint /k, so give the web server a different plain HTTP listener for the tunnel, for example http://photos.luciochen.com:8081 in the Caddyfile, or listen 127.0.0.1:8081; in nginx, and point the tunnel there.

Client addresses

LuPhoto limits photo keys per visitor address, so it must see the real one. Through the tunnel every request reaches the origin from cloudflared on loopback, and Cloudflare puts the visitor's address in CF-Connecting-IP. In /etc/luphoto/config.toml:

[server]
trusted_proxies  = ["127.0.0.1/32", "::1/128"]
client_ip_header = "CF-Connecting-IP"

trusted_proxies must list the address the request comes from when it reaches LuPhoto: loopback for cloudflared, Caddy or nginx on the same machine (they pass CF-Connecting-IP through unchanged). If cloudflared runs on another host, add that host's address. Never trust a range the public can connect from directly, or anyone can choose their own address. Without the tunnel, behind Caddy or nginx only, keep the default "X-Forwarded-For".

Cache rules

Caching → Cache Rules, in this order:

  1. Key endpoint: when the URI path equals /k, Bypass cache. It is a per-visitor POST answered with Cache-Control: no-store; it must never be cached or served from the edge.
  2. Tiles, OG images and assets: when the URI path starts with /t/, /og/ or /assets/, set Eligible for cache, Edge TTL 1 month (or "use cache-control header"), Browser TTL "respect origin". Their names are content-hashed, so they never change. A tile URL depends only on the photo's bytes, the processing settings and the site key in /var/lib/luphoto/state/site.key, so it stays the same across builds and restarts; back up that key, because a new one gives every tile a new URL and empties the edge cache.
  3. Everything else (HTML, /a/* including the per-photo pool files under /a/<slug>/p/, index.<build>.json, robots.txt): Edge TTL 1 minute or "respect origin" (max-age=60). Manifests and pool files are named after the build, so a new build needs no purge. Decoy tiles under /t/ never change their bytes either, so rule 2 holds for them.

Keep "Cache by device type" and query-string variations off. Once a tile is in the edge cache, Cloudflare serves it without asking the origin, so the Referer check in LuPhoto, Caddy or nginx only sees cache misses (its 403 is sent with Cache-Control: no-store, so a refused request is never cached for real visitors). To block hotlinking of cached tiles too, add a WAF custom rule: when the URI path starts with /t/, the Referer header is not empty and it does not start with https://photos.luciochen.com/, Block. Cloudflare's Scrape Shield Hotlink Protection works as well, but it only covers image extensions it knows. /k checks Origin itself. Bot Fight Mode and similar challenges must not be applied to /k, because they would answer the browser's fetch with an HTML page.