Install with NGINX
View as MarkdownAdd the Reveata snippet to pages NGINX serves, and proxy the script and events through your own domain with the visitor’s address and your proxy secret.
On this page
If you can edit the HTML or templates NGINX serves, paste the snippet before </head> there: see Install the script. When you can’t (for example a static build you don’t control, or an app behind NGINX), NGINX can add the snippet to every HTML page on its way out.
Copy the snippet from your Reveata dashboard (it contains your website’s tracking id):
<script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js"></script>Add it with sub_filter
This uses NGINX’s ngx_http_sub_module. Most packaged builds include it; check with nginx -V 2>&1 | grep -o with-http_sub_module.
- In the
serverblock of your site, add the twosub_filterlines to thelocationthat serves your pages:
server {
server_name example.com;
location / {
# … your existing root or proxy_pass settings …
sub_filter '</head>' '<script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js"></script></head>';
sub_filter_once on;
}
}- If that
locationusesproxy_passand your app compresses its responses, also addproxy_set_header Accept-Encoding "";. NGINX can only replace text in responses it receives uncompressed. It can still compress them for visitors withgzip on;. - Test the configuration with
sudo nginx -t, then reload withsudo nginx -s reload(orsudo systemctl reload nginx).
sub_filter changes only text/html responses by default, so images, scripts and API responses are left alone.
Check it works
Open a page in a private window and view its source: the snippet should be right before </head>. Then check that the visit appears in your Reveata dashboard. If it doesn’t, follow Verify your installation.
Proxy through NGINX
NGINX can serve the Reveata script from your own domain and pass events on, so fewer visits are lost to ad blockers. It must send Reveata the visitor’s address with your website’s proxy secret; otherwise every visit looks like it came from your server.
- Create a proxy secret. In Reveata, open your website’s Settings → General, find Proxy secret in Tracking code, and choose Create proxy secret. Copy it (it starts with
dt_proxy_and is shown once). - Store it for NGINX. NGINX doesn’t read environment variables in its config, so put the secret in a file at the
httplevel, for example/etc/nginx/conf.d/reveata.conf, readable only by root (sudo chmod 600 /etc/nginx/conf.d/reveata.conf):
map $host $reveata_proxy_secret {
default "dt_proxy_your_secret";
}With the official Docker image, save the same block as /etc/nginx/templates/reveata.conf.template with default "${REVEATA_PROXY_SECRET}"; instead, and the image writes the file from the environment variable at startup.
- Add the two locations inside the
serverblock of your site:
location = /dt/script.js {
proxy_pass https://reveata.com/js/script.js;
proxy_ssl_server_name on;
proxy_set_header Cookie "";
}
location = /dt/event {
proxy_pass https://reveata.com/api/collect;
proxy_ssl_server_name on;
proxy_set_header X-Reveata-Proxy-Secret $reveata_proxy_secret;
proxy_set_header X-Reveata-Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie "";
}- Point the snippet at them, in your pages or in the
sub_filterline above:
<script defer data-website-id="ws_your_tracking_id" data-api="/dt/event" src="/dt/script.js"></script>- Test with
sudo nginx -t, then reload withsudo systemctl reload nginx.
NGINX passes the browser’s other headers on and sends Reveata’s host name upstream, which is what Reveata needs. The paths can be anything; avoid words like analytics, which some blockers look for.
Behind a load balancer or CDN (Cloudflare, an AWS load balancer), $remote_addr is the load balancer’s address. Use the realip module so it becomes the visitor’s, for example for Cloudflare real_ip_header CF-Connecting-IP; with a set_real_ip_from line for each Cloudflare range.
No server access? The managed proxy does the same with one DNS record and no NGINX change. More platforms: Proxy through your own server.
Revenue
NGINX only serves pages; payments are recorded by your provider or your app. Connect your provider in Reveata (Connect a payment provider), and pass the visitor id with each checkout from your app: see the guide for your framework, for example Django, Laravel, Express or PHP.
Check the proxy works
- Open
https://your-site/dt/script.js: you should see the script. - Visit your site, open the developer tools’ Network tab, and find the request to
/dt/event. Its response has anx-reveata-proxyheader:trustedmeans the secret was accepted. - In Reveata’s Realtime, your visit shows your own city, not your server’s.
proxy_secret_invalid means the secret is missing, mistyped or rotated. No header at all means NGINX didn’t send it: check that reveata.conf is included at the http level.
Common pitfalls
- Forwarding without the secret. A plain
proxy_passto Reveata makes every visit come from your server’s address: countries, IP exclusions and per-visitor limits stop working, and in cookieless mode all visitors look alike. - Compressed upstream responses.
sub_filtercan’t change compressed responses; addproxy_set_header Accept-Encoding "";to thelocationthat proxies your app. - Caching
/dt/event. Never cache it. The script itself may be cached; Reveata sends the rightCache-Control. - A Content Security Policy. With the proxy, the script and events are on your own domain, so
'self'covers them. Without it, see Content Security Policy.