Source: https://datafa.st/docs/caddy-proxy
Markdown source: https://datafa.st/docs/caddy-proxy.md
Description: How to proxy DataFast analytics through Caddy

# Proxy DataFast with Caddy

Learn how to proxy DataFast analytics through Caddy to bypass adblockers and improve accuracy. Caddy's simple configuration makes it easy to set up.

## 1. Basic Caddy Configuration

Add the following to your `Caddyfile`:

```caddy
your_domain.com {
    # Proxy the analytics script
    handle /js/script.js {
        reverse_proxy https://datafa.st {
            header_up Host {upstream_hostport}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}
        }
        
        # Cache the script for 1 year
        header Cache-Control "public, max-age=31536000"
        header Expires "1y"
    }

    # Proxy the events endpoint
    handle /api/events {
        reverse_proxy https://datafa.st {
            header_up Host {upstream_hostport}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}
            header_up x-datafast-real-ip {remote_host}
        }
    }

    # Your other site configurations...
}
```

> **Note:** If you already have an `/api/events` API endpoint, add `data-api-url` to the DataFast script tag to send events to your own API endpoint. For example, `data-api-url="/datafast-events"` will send events to `/datafast-events` instead of `/api/events`. Read more [here](/docs/script-configuration#api-url-data-api-url-optional)

> **Important:** If you notice all visitors showing from the same location in your analytics, make sure you're sending the `x-datafast-real-ip` header with the actual visitor IP address (not your proxy server IP) when forwarding events to DataFast's `/api/events` endpoint.

## 2. Optional: Add Caching Configuration

For better performance, you can add caching:

```caddy
your_domain.com {
    # Cache configuration
    cache {
        path /var/cache/caddy
        ttl 1y
        capacity 10GB
    }

    handle /js/script.js {
        cache
        reverse_proxy https://datafa.st {
            header_up Host {upstream_hostport}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}
        }
        
        header Cache-Control "public, max-age=31536000"
        header Expires "1y"
    }

    # ... rest of the configuration
}
```

## 3. Update Your Script Tag

Replace your existing DataFast script with the proxied version:

```html
<script
  defer
  data-website-id="dfid_******"
  data-domain="your_domain.com"
  src="/js/script.js"
></script>
```

## 4. Reload Caddy

```bash
# Test the configuration
caddy validate

# If the test is successful, reload Caddy
caddy reload
```

## Verification

To verify the proxy is working:
1. Visit your website
2. Open the network tab in your browser's developer tools
3. Check that analytics requests are going through your domain instead of datafa.st

> **info:** Caddy automatically handles HTTPS certificates and HTTP/2, making it a great choice for proxying analytics.

> **warning:** Make sure your Caddy server has enough disk space allocated for caching if you're using the cache configuration.

## Troubleshooting

#### *All visitors showing from the same location*

If your analytics show all visitors from a single location (usually your server's location), your proxy isn't forwarding visitor IPs.

**To fix:**
1. Make sure your proxy sends the `x-datafast-real-ip` header with the actual visitor IP address (not your proxy server IP) when forwarding events to DataFast's `/api/events` endpoint
