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

# Proxy DataFast with Nginx

Learn how to proxy DataFast analytics through Nginx to bypass adblockers and improve accuracy. This guide works for both standalone Nginx and Nginx as a reverse proxy.

## 1. Basic Nginx Configuration

Add the following to your Nginx configuration (usually in `/etc/nginx/sites-available/your-site` or `/etc/nginx/conf.d/your-site.conf`):

```nginx
# Proxy the analytics script
location /js/script.js {
    proxy_pass https://datafa.st/js/script.js;
    proxy_set_header Host datafa.st;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    
    # Cache the script for 1 year
    proxy_cache_valid 200 1y;
    add_header Cache-Control "public, max-age=31536000";
    expires 1y;
}

# Proxy the events endpoint
location /api/events {
    proxy_pass https://datafa.st/api/events;
    proxy_set_header Host datafa.st;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header x-datafast-real-ip $remote_addr;
    
    # Allow POST requests
    proxy_method POST;
    proxy_pass_request_body on;
}
```

> **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 a caching configuration:

```nginx
# Define cache zone
proxy_cache_path /var/cache/nginx/datafast_cache levels=1:2 keys_zone=datafast_cache:10m max_size=10g inactive=60m use_temp_path=off;

# In your server block
location /js/script.js {
    proxy_cache datafast_cache;
    proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    proxy_cache_valid 200 1y;
    proxy_cache_bypass $http_pragma;
    proxy_cache_revalidate on;
    
    # ... rest of the configuration from step 1
}
```

## 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. Test and Reload Nginx

```bash
# Test the configuration
sudo nginx -t

# If the test is successful, reload Nginx
sudo systemctl reload nginx
```

## 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

## 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
