# Proxy on DigitalOcean

> Forward the Reveata script and events on DigitalOcean App Platform with a small service component, or on a Droplet with Nginx.

How you proxy depends on where your site runs on DigitalOcean.

- **A Droplet** with Nginx, Caddy or Apache in front of your site: add the configuration for your web server from [Proxy through your own server](https://reveata.com/docs/self-hosted-proxy#nginx). Nothing here is specific to DigitalOcean.
- **App Platform**: a static site component can’t run code per request, so add a small service component that forwards the two paths, as below. If your app already has a service (Express, Flask, FastAPI, Laravel), add the routes there instead: see [Flask](https://reveata.com/docs/proxy-flask), [FastAPI](https://reveata.com/docs/proxy-fastapi) or [Proxy through your own server](https://reveata.com/docs/self-hosted-proxy#express).

## Before you start

In Reveata, open your website, then **Settings → General → Tracking code**, and choose **Create proxy secret** under **Proxy secret** (admins and the owner can). Copy it: it starts with `dt_proxy_` and is shown once.

## The service

A Node.js service with no dependencies (Node 18 or later). Put it in its own folder of your repository, for example `dt-proxy/server.js`, with a `package.json` whose `start` script is `node server.js`:

```js
const http = require('node:http');

const UPSTREAM = 'https://reveata.com';
const PASS = [
  'user-agent',
  'accept-language',
  'origin',
  'referer',
  'sec-fetch-dest',
  'sec-fetch-mode',
  'sec-fetch-site',
];

function visitorIp(req) {
  // App Platform puts the visitor's address in do-connecting-ip.
  const direct = req.headers['do-connecting-ip'];
  if (direct) return String(direct).trim();
  return String(req.headers['x-forwarded-for'] ?? '').split(',')[0].trim();
}

http
  .createServer(async (req, res) => {
    try {
      if (req.method === 'GET' && req.url === '/dt/script.js') {
        const r = await fetch(`${UPSTREAM}/js/script.js`);
        res.writeHead(r.status, {
          'content-type': 'text/javascript; charset=utf-8',
          'cache-control': r.headers.get('cache-control') ?? 'public, max-age=86400',
        });
        res.end(Buffer.from(await r.arrayBuffer()));
        return;
      }
      if (req.method === 'POST' && req.url === '/dt/event') {
        const chunks = [];
        let size = 0;
        for await (const chunk of req) {
          size += chunk.length;
          if (size > 16 * 1024) {
            res.writeHead(413).end();
            return;
          }
          chunks.push(chunk);
        }
        const headers = {
          'content-type': 'text/plain',
          'x-reveata-proxy-secret': process.env.REVEATA_PROXY_SECRET ?? '',
        };
        const ip = visitorIp(req);
        if (ip) headers['x-reveata-client-ip'] = ip;
        for (const name of PASS) if (req.headers[name]) headers[name] = String(req.headers[name]);
        const r = await fetch(`${UPSTREAM}/api/collect`, {
          method: 'POST',
          headers,
          body: Buffer.concat(chunks),
        });
        const out = { 'cache-control': 'no-store' };
        const diagnostic = r.headers.get('x-reveata-proxy');
        if (diagnostic) out['x-reveata-proxy'] = diagnostic;
        res.writeHead(r.status, out).end();
        return;
      }
      res.writeHead(404).end();
    } catch {
      res.writeHead(502).end();
    }
  })
  .listen(Number(process.env.PORT ?? 8080));
```

It forwards the body unchanged, with the visitor's browser headers, and never your cookies.

## Route `/dt` to it

In the app spec (**Settings → App Spec** in the control panel, or `.do/app.yaml`), add the service with the secret as an encrypted runtime variable, and an ingress rule that sends `/dt` to it with the path kept:

```yaml
services:
  - name: dt-proxy
    source_dir: dt-proxy
    environment_slug: node-js
    run_command: npm start
    http_port: 8080
    instance_count: 1
    envs:
      - key: REVEATA_PROXY_SECRET
        scope: RUN_TIME
        type: SECRET
        value: dt_proxy_your_secret
ingress:
  rules:
    - component:
        name: dt-proxy
        preserve_path_prefix: true
      match:
        path:
          prefix: /dt
    # …your existing rules, for example the static site on /
```

App Platform encrypts a `SECRET` value once you save the spec. Keep your existing rules for the rest of the site below this one. Then point the snippet at the two paths:

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

## Check it works

Open `/dt/script.js` on your site: you should see the script. Visit a page, then open **Realtime** in Reveata: your visit should appear within seconds, in your own city. If it shows a data-centre location instead, read the `x-reveata-proxy` response header of `/dt/event` in your browser's network tab (the service passes it on), and see [Check that it works](https://reveata.com/docs/self-hosted-proxy#check-that-it-works) for what each value means. A `trusted` value with the wrong city means the address forwarded isn’t the visitor’s: log the `do-connecting-ip` and `x-forwarded-for` headers once to see what your app receives.
