Proxy di Flowsery Analytics con Nginx
Instrada Flowsery Analytics attraverso Nginx per evitare il blocco degli adblocker e raccogliere dati precisi sui visitatori. Questa guida vale sia per Nginx standalone sia per Nginx usato come reverse proxy davanti a un application server.
La configurazione ha due elementi essenziali: un proxy_pass che rende gli eventi first-party (al sicuro dagli adblocker) e un proxy_set_header x-flowsery-real-ip che porta l'IP reale del visitatore oltre il passaggio nel proxy, così Flowsery calcola la geolocalizzazione dal visitatore e non dal tuo server.
1. Configurazione di base di Nginx
Aggiungi questi blocchi location in /etc/nginx/sites-available/your-site o in /etc/nginx/conf.d/your-site.conf:
# Proxy the analytics script
location /js/main.js {
proxy_pass https://cdn.flowsery.com/main.js;
proxy_set_header Host cdn.flowsery.com;
# Cache the script for 1 year
proxy_cache_valid 200 1y;
add_header Cache-Control "public, max-age=31536000";
expires 1y;
}
# Proxy the event collection endpoint + forward real visitor IP
location /api/track {
proxy_pass https://analytics.flowsery.com/analytics/events;
proxy_set_header Host analytics.flowsery.com;
# CRITICAL: forwards the real visitor IP to the analytics backend.
# Without this, every visitor resolves to your server's location.
proxy_set_header x-flowsery-real-ip $remote_addr;
# Standard forwarding (kept for compatibility with other consumers)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
# Allow POST requests
proxy_pass_request_body on;
}Se Nginx è dietro Cloudflare (o un altro edge), $remote_addr è l'IP dell'edge della CDN, non del visitatore. Usa invece l'header con l'IP reale fornito dalla CDN:
# Behind Cloudflare: the visitor IP is in $http_cf_connecting_ip
proxy_set_header x-flowsery-real-ip $http_cf_connecting_ip;
# Behind Vercel / Fly.io / a generic L7 proxy
# proxy_set_header x-flowsery-real-ip $http_x_real_ip;Nota: se il tuo server serve già /api/track, scegli un altro percorso e indicalo al tracker con data-api="/flowsery-events".
2. Facoltativo: abilita la cache per lo script
# At the top of your nginx.conf (http {} block)
proxy_cache_path /var/cache/nginx/flowsery_cache levels=1:2 keys_zone=flowsery_cache:10m max_size=10g inactive=60m use_temp_path=off;
# In your server {} block, augment the /js/main.js location
location /js/main.js {
proxy_cache flowsery_cache;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_valid 200 1y;
proxy_cache_bypass $http_pragma;
proxy_cache_revalidate on;
# ...rest of the config from step 1
}3. Modifica il tag script
Sostituisci lo snippet di Flowsery Analytics con la versione che passa dal proxy:
<script defer data-fl-website-id="flid_******" src="/js/main.js"></script>4. Testa e ricarica Nginx
sudo nginx -t # validate config
sudo systemctl reload nginxVerifica che funzioni
- Apri il tuo sito in una normale scheda del browser.
- DevTools → Network, filtra per
/api/track. Verifica che le richieste vadano al tuo dominio. - Nella dashboard di Flowsery, controlla che i paesi dei visitatori rispecchino il tuo pubblico e non la regione del tuo hosting.
Risoluzione dei problemi
Tutti i visitatori risultano dalla stessa località
Il proxy inoltra gli eventi ma non l'IP reale del visitatore. x-flowsery-real-ip manca oppure contiene l'IP del tuo server invece di quello del visitatore.
- Verifica che la riga
proxy_set_header x-flowsery-real-ip <value>sia presente su/api/tracke che il valore usi la variabile giusta per la tua configurazione edge ($remote_addrper il traffico diretto,$http_cf_connecting_ipper Cloudflare,$http_x_real_ipper un edge L7 generico). - Se Nginx si trova dietro un altro proxy, non usare
$remote_addr: è il proxy a monte, non il visitatore. Usa l'header che il proxy a monte aggiunge.