# Reverse proxy templates

Proxy the Screeb tag and its endpoints through your own domain with nginx, Cloudflare Workers or Vercel.

Use these templates to serve Screeb from your own domain, then configure the tag as described in [Custom collector URL](https://developers.screeb.app/sdk-js/custom-collector-url). Replace `analytics.acme.com` with your proxy hostname.

The proxy gets a host of its own. The tag must sit at the root of that host: it loads its bundles from `/v<version>/` at the origin of `tag.js`, whatever the path `tag.js` was served from. So the four endpoint prefixes go to their Screeb service, and every other path goes to the tag origin.

| Path on your proxy                          | Forwarded to                  |
| ------------------------------------------- | ----------------------------- |
| `/rpc/`                                     | `https://rpc.screeb.app/rpc/` |
| `/static/`                                  | `https://static.screeb.app/`  |
| `/report/`                                  | `https://r.screeb.app/rpc/`   |
| `/hosted-page/`                             | `https://survey.screeb.app/`  |
| Everything else (`/tag.js`, `/v<version>/`) | `https://t.screeb.app/`       |

Forward the request headers unchanged, the browser's `Origin` header in particular: the allowed domains check reads it.

## nginx

```nginx
server {
    listen 443 ssl;
    server_name analytics.acme.com;

    # Add your certificate configuration here.

    location /rpc/ {
        proxy_pass https://rpc.screeb.app/rpc/;
        proxy_set_header Host rpc.screeb.app;
        proxy_ssl_server_name on;
    }

    location /static/ {
        proxy_pass https://static.screeb.app/;
        proxy_set_header Host static.screeb.app;
        proxy_ssl_server_name on;
    }

    location /report/ {
        proxy_pass https://r.screeb.app/rpc/;
        proxy_set_header Host r.screeb.app;
        proxy_ssl_server_name on;
    }

    location /hosted-page/ {
        proxy_pass https://survey.screeb.app/;
        proxy_set_header Host survey.screeb.app;
        proxy_ssl_server_name on;
    }

    # The tag: tag.js and its versioned bundles.
    location / {
        proxy_pass https://t.screeb.app;
        proxy_set_header Host t.screeb.app;
        proxy_ssl_server_name on;
    }
}
```

## Cloudflare Workers

Deploy this Worker on the `analytics.acme.com` route:

```js
const ROUTES = [
  ["/rpc/", "https://rpc.screeb.app/rpc/"],
  ["/static/", "https://static.screeb.app/"],
  ["/report/", "https://r.screeb.app/rpc/"],
  ["/hosted-page/", "https://survey.screeb.app/"],
];

export default {
  async fetch(request) {
    const url = new URL(request.url);
    const [prefix, target] =
      ROUTES.find(([prefix]) => url.pathname.startsWith(prefix)) ?? ["/", "https://t.screeb.app/"];
    const upstream = new URL(url.pathname.slice(prefix.length) + url.search, target);
    return fetch(new Request(upstream, request));
  },
};
```

## Vercel

Create a dedicated Vercel project on `analytics.acme.com`, so the catch-all rule for the tag does not take over your app's routes, with this `vercel.json`. Rewrites to an external URL are proxied server side, and the first matching rule wins:

```json
{
  "rewrites": [
    { "source": "/rpc/:path*", "destination": "https://rpc.screeb.app/rpc/:path*" },
    { "source": "/static/:path*", "destination": "https://static.screeb.app/:path*" },
    { "source": "/report/:path*", "destination": "https://r.screeb.app/rpc/:path*" },
    { "source": "/hosted-page/:path*", "destination": "https://survey.screeb.app/:path*" },
    { "source": "/:path*", "destination": "https://t.screeb.app/:path*" }
  ]
}
```

## Check the proxy

Open `https://analytics.acme.com/tag.js` in a browser: it must return the Screeb loader. Then load a page of your site with the tag configured, and check in the network panel of the browser developer tools that the requests go to your proxy and succeed.
