Proxy setups
Caddy reverse proxy for the Loogika script
Caddy can serve Loogika from your own domain with one handle_path block in the Caddyfile. The block forwards /stats/ to https://app.loogika.com/api/ and adds the visitor's IP address. Afterwards you change the script tag and switch on First-Party Proxy in Site Settings.
Last updated .
What do you need before you start?
You need Caddy version 2.7 or later, because the configuration uses the {client_ip} placeholder. Your domain must point to the server. Caddy gets and renews the HTTPS certificate from Let's Encrypt by itself.
You also need your Loogika site ID from Site Settings, under the Tracking Script tab. The proxy guide explains the idea behind the setup.
How do you configure the Caddy proxy?
Add a handle_path block to the site block in your Caddyfile. The directive removes /stats from the address, and the rewrite line puts /api in front of what is left.
# /etc/caddy/Caddyfile
yourdomain.com {
# Your regular site configuration
# Loogika proxy
handle_path /stats/* {
reverse_proxy https://app.loogika.com {
header_up Host app.loogika.com
header_up X-Real-IP {client_ip}
header_up X-Forwarded-For {client_ip}
}
rewrite * /api{uri}
}
}
The block covers every path the script uses, including session replay and identify(). The generic setup page lists the paths one by one.
How do you apply the Caddyfile?
Validate the file first, then reload Caddy. The first command must answer with Valid configuration.
caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
Without systemd, run sudo caddy reload --config /etc/caddy/Caddyfile instead of the second command.
How do you change the script tag?
Load the script from /stats/script.js on your own domain. Keep the file name script.js, because the script uses it to find the address for its events.
<script src="https://yourdomain.com/stats/script.js?siteId=YOUR_SITE_ID" defer></script>
Then open Site Settings in Loogika, go to General and turn on First-Party Proxy. The switch tells Loogika to read the visitor's IP address from the headers Caddy sets.
What if Caddy runs behind a CDN?
Tell Caddy which addresses belong to the CDN. The placeholder {client_ip} holds the address that connected to Caddy. Behind a CDN such as Cloudflare, that address is the CDN's server, and Loogika would place every visitor there.
List the CDN's published IP ranges under trusted_proxies in the global options at the top of the Caddyfile. Caddy then fills {client_ip} with the visitor's own address. The ranges below are placeholders.
{
servers {
trusted_proxies static 198.51.100.0/24 2001:db8::/32
}
}
How do you check that the Caddy proxy works?
Request the script through your domain with curl. The response should have status 200 and the content type application/javascript.
curl -I "https://yourdomain.com/stats/script.js?siteId=YOUR_SITE_ID"
Then open your site with the browser's developer tools on the Network tab. The script and the POST requests to /stats/track should go to your own domain and return status 200. Your visit shows up in the realtime view in Loogika.
How do you use a subdomain instead of a path?
Give the subdomain its own site block and send every request to /api. The script address is then https://stats.yourdomain.com/script.js?siteId=YOUR_SITE_ID.
stats.yourdomain.com {
reverse_proxy https://app.loogika.com {
header_up Host app.loogika.com
header_up X-Real-IP {client_ip}
header_up X-Forwarded-For {client_ip}
}
rewrite * /api{uri}
}
Why does the Caddy proxy not work?
| Symptom | Cause and fix |
|---|---|
| Caddy cannot get a certificate | Check that the domain points to your server, that ports 80 and 443 are open and that no other process uses port 443. Read the log with journalctl -u caddy -f |
| 502 Bad Gateway | Read the last log lines with journalctl -u caddy -n 50 |
| Requests reach the wrong Loogika path | Check the rewrite line. Each request must end up under /api/ |
| Every visitor has the same location | Keep both header_up lines for the IP address and switch on First-Party Proxy in Site Settings. Behind a CDN, add trusted_proxies as well |
The proxy troubleshooting page covers problems that are not specific to Caddy.