Docs·Proxy guide
Proxy with nginx
Two location blocks in your site's server block serve Spreelytics from /sp/. This guide uses /sp/. Any path works if nothing else on your site uses it and it doesn't contain words like "analytics" or "tracking", which blockers look for.
Add the locations
Add these to your site's server block, then reload nginx:
# Looks Spreelytics' hosts up again every 5 minutes, since their IP addresses change.
resolver 1.1.1.1 8.8.8.8 valid=300s ipv6=off;
location /sp/js/ {
set $spreelytics_cdn cdn.spreelytics.com;
rewrite ^/sp/js/(.*)$ /cdn/sdk/$1 break;
proxy_pass https://$spreelytics_cdn;
proxy_ssl_server_name on;
proxy_set_header Host cdn.spreelytics.com;
proxy_set_header Cookie "";
}
location /sp/ {
set $spreelytics_api sdk.spreelytics.com;
rewrite ^/sp/(.*)$ /1.0/$1 break;
proxy_pass https://$spreelytics_api;
proxy_ssl_server_name on;
proxy_set_header Host sdk.spreelytics.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie "";
}
Point the script at your proxy
In your Spreelytics snippet, load the script through /sp/js/ and add api_host:
<script src="/sp/js/spreelytics-loader.js"></script>
<script>
Spreelytics.init({
client_key: "YOUR_CLIENT_KEY",
api_host: "https://www.example.com/sp",
});
</script>
Check it works
Open your site with the browser's Network tab open and filtered by /sp/. The script files and
requests like track/page should come from your own domain, with status 200. In Spreelytics,
Settings → Tracking script says "Last seen just now" and Realtime shows you in your own country.
- Wrong country, or
200and nothing arrives: the proxy isn't passing the visitor's IP address inX-Forwarded-For. See The visitor's IP address below. 401: theOriginandRefererheaders didn't arrive, or your domain isn't in Allowed domains.
The visitor's IP address
$proxy_add_x_forwarded_for adds the address nginx sees to any X-Forwarded-For already on the request, and Spreelytics reads the first public one. Behind Cloudflare or a load balancer, that is still the visitor.
Related
- Proxy overview: the other ways to set it up.
- Filter traffic: which traffic Spreelytics drops.