Proxy setups
Nginx proxy for the Loogika script
Nginx can serve Loogika from your own domain with one location block. The block forwards /stats/ to https://app.loogika.com/api/ with proxy_pass 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 nginx with an HTTPS certificate for your domain and your Loogika site ID. The site ID is in Site Settings in Loogika, under the Tracking Script tab. The proxy guide explains the idea behind the setup.
How do you configure the nginx proxy?
Add one location block to the server block of your site. The trailing slashes matter. With them, nginx swaps /stats/ for /api/ and keeps the rest of the address.
# /etc/nginx/sites-available/yourdomain.com
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
location / {
# Your regular site configuration
}
# Loogika proxy
location /stats/ {
proxy_pass https://app.loogika.com/api/;
proxy_set_header Host app.loogika.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ssl_server_name on;
# Session replay uploads are larger than events
client_max_body_size 10M;
}
}
The block covers every path the script uses, including session replay and identify(). Nginx passes the browser's other request headers through unchanged, which Loogika needs for bot detection. The generic setup page lists the paths and headers one by one.
What do the directives do?
| Directive | Purpose |
|---|---|
proxy_set_header Host | Tells Loogika which host the request is for |
X-Real-IP and X-Forwarded-For | Carry the visitor's IP address. Without them every visit seems to come from your server |
proxy_ssl_server_name on | Sends the host name during the TLS handshake (SNI). Leaving it out causes SSL errors in the nginx log |
client_max_body_size 10M | Lets session replay data through |
How do you apply the configuration?
Test the configuration first, then reload nginx. The test must report that the configuration file test is successful.
sudo nginx -t
sudo systemctl reload nginx
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 nginx sets.
How do you check that the nginx 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.
Can nginx cache the script?
Yes, with a cache zone. The directive proxy_cache_valid does nothing on its own, so define the zone in the http block first:
# /etc/nginx/nginx.conf (http block)
proxy_cache_path /var/cache/nginx/loogika
levels=1:2
keys_zone=loogika_cache:10m
max_size=100m
inactive=60m
use_temp_path=off;
Create the folder and give it to the user nginx runs as. The example uses www-data.
sudo mkdir -p /var/cache/nginx/loogika
sudo chown www-data:www-data /var/cache/nginx/loogika
Then add an exact-match block for the script next to the /stats/ block. Nginx prefers an exact match, so the script uses the cache and all other requests still use the general block.
location = /stats/script.js {
proxy_pass https://app.loogika.com/api/script.js;
proxy_set_header Host app.loogika.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_cache loogika_cache;
proxy_cache_valid 200 1h;
proxy_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache-Status $upstream_cache_status;
}
Run the curl command twice. The header x-cache-status reads MISS on the first request and HIT on the second. Never add cache directives to the POST paths such as /stats/track.
How do you use a subdomain instead of a path?
Give the subdomain its own server block and forward everything to /api/. The script address is then https://stats.yourdomain.com/script.js?siteId=YOUR_SITE_ID.
server {
listen 443 ssl http2;
server_name stats.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/stats.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/stats.yourdomain.com/privkey.pem;
location / {
proxy_pass https://app.loogika.com/api/;
proxy_set_header Host app.loogika.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
client_max_body_size 10M;
}
}
Why does the nginx proxy not work?
| Symptom | Cause and fix |
|---|---|
| 502 Bad Gateway | Check that proxy_ssl_server_name on is set. Then read the log with sudo tail -f /var/log/nginx/error.log |
| The log says no resolver is defined | Nginx found a variable, for example $1, in a proxy_pass line. Take the variable out, or add a resolver directive |
nginx -t says proxy_pass cannot have a URI part | A regex block (location ~) uses a proxy_pass with a fixed path. Use a prefix block such as location /stats/ instead |
| SSL errors in the nginx log | Add proxy_ssl_server_name on to the block |
| Every visitor has the same location | Keep both IP header lines in the block and switch on First-Party Proxy in Site Settings |
x-cache-status always reads MISS | Check that the cache folder exists and is writable, and that proxy_cache uses the zone name from proxy_cache_path |
The proxy troubleshooting page covers problems that are not specific to nginx.