# Install with NGINX

> Add 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.

If you can edit the HTML or templates NGINX serves, paste the snippet before `</head>` there: see [Install the script](https://reveata.com/docs/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):

```html
<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`.

1. In the `server` block of your site, add the two `sub_filter` lines to the `location` that serves your pages:

```nginx
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;
    }
}
```

2. If that `location` uses `proxy_pass` and your app compresses its responses, also add `proxy_set_header Accept-Encoding "";`. NGINX can only replace text in responses it receives uncompressed. It can still compress them for visitors with `gzip on;`.
3. Test the configuration with `sudo nginx -t`, then reload with `sudo nginx -s reload` (or `sudo 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](https://reveata.com/docs/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.

1. **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).
2. **Store it for NGINX.** NGINX doesn’t read environment variables in its config, so put the secret in a file at the `http` level, for example `/etc/nginx/conf.d/reveata.conf`, readable only by root (`sudo chmod 600 /etc/nginx/conf.d/reveata.conf`):

```nginx
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.

3. **Add the two locations** inside the `server` block of your site:

```nginx
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 "";
}
```

4. **Point the snippet at them**, in your pages or in the `sub_filter` line above:

```html
<script defer data-website-id="ws_your_tracking_id" data-api="/dt/event" src="/dt/script.js"></script>
```

5. Test with `sudo nginx -t`, then reload with `sudo 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](https://reveata.com/docs/managed-proxy) does the same with one DNS record and no NGINX change. More platforms: [Proxy through your own server](https://reveata.com/docs/self-hosted-proxy).

## Revenue

NGINX only serves pages; payments are recorded by your provider or your app. Connect your provider in Reveata ([Connect a payment provider](https://reveata.com/docs/connect-a-payment-provider)), and pass the visitor id with each checkout from your app: see the guide for your framework, for example [Django](https://reveata.com/docs/django#revenue), [Laravel](https://reveata.com/docs/laravel#revenue), [Express](https://reveata.com/docs/express#revenue) or [PHP](https://reveata.com/docs/php#revenue).

## Check the proxy works

1. Open `https://your-site/dt/script.js`: you should see the script.
2. Visit your site, open the developer tools’ **Network** tab, and find the request to `/dt/event`. Its response has an `x-reveata-proxy` header: `trusted` means the secret was accepted.
3. 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_pass` to 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_filter` can’t change compressed responses; add `proxy_set_header Accept-Encoding "";` to the `location` that proxies your app.
- **Caching `/dt/event`.** Never cache it. The script itself may be cached; Reveata sends the right `Cache-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](https://reveata.com/docs/content-security-policy).
