Posthog reverse proxy to be deployed on you own docker infra.
4.2K
This Docker container provides a secure nginx reverse proxy for PostHog analytics, allowing you to route analytics requests through your own domain to avoid ad blockers and maintain data privacy.
proxy_ssl_server_name to prevent SSL handshake failures/health endpoint for monitoringdocker build -t posthog-proxy .
docker run -d \
-p 8080:8080 \
-e ALLOWED_DOMAIN=yourdomain.com \
-e POSTHOG_CLOUD=us \
posthog-proxy
version: '3.8'
services:
posthog-proxy:
build: .
ports:
- "8080:8080"
environment:
- ALLOWED_DOMAIN=yourdomain.com
- POSTHOG_CLOUD=us
- CACHE_STATIC_DAYS=7
restart: unless-stopped
| Variable | Default | Description |
|---|---|---|
ALLOWED_DOMAIN | google.com | Your domain name (without protocol). Requests with referers from this domain and its subdomains will be allowed. |
POSTHOG_CLOUD | us | PostHog cloud region. Use us for US cloud or eu for EU cloud. |
CACHE_STATIC_DAYS | 7 | Number of days to cache static assets. |
Configure your PostHog client to use your proxy:
posthog.init('YOUR_API_KEY', {
api_host: 'https://yourdomain.com',
ui_host: 'https://app.posthog.com' // or 'https://eu.posthog.com' for EU
});
If you're serving the proxy on a subpath:
posthog.init('YOUR_API_KEY', {
api_host: 'https://yourdomain.com/analytics',
});
https://yourdomain.com/*Referer header:
/static/* paths:
https://[region]-assets.i.posthog.com/static/*https://[region].i.posthog.com/*services:
posthog-proxy:
build: .
environment:
- ALLOWED_DOMAIN=yourdomain.com
- POSTHOG_CLOUD=us
labels:
- "traefik.enable=true"
- "traefik.http.routers.posthog.rule=Host(`analytics.yourdomain.com`)"
- "traefik.http.routers.posthog.tls=true"
- "traefik.http.services.posthog.loadbalancer.server.port=8080"
apiVersion: apps/v1
kind: Deployment
metadata:
name: posthog-proxy
spec:
replicas: 2
selector:
matchLabels:
app: posthog-proxy
template:
metadata:
labels:
app: posthog-proxy
spec:
containers:
- name: nginx
image: your-registry/posthog-proxy:latest
ports:
- containerPort: 8080
env:
- name: ALLOWED_DOMAIN
value: "yourdomain.com"
- name: POSTHOG_CLOUD
value: "us"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
The proxy includes a health endpoint at /health that returns 200 OK when the service is running.
curl http://localhost:8080/health
# Returns: healthy
The proxy logs all requests with detailed information:
[01/Jan/2024:12:00:00 +0000] "GET /static/recorder.js HTTP/1.1" 200 "Referer: https://yourdomain.com" "Host: yourdomain.com" "Cache: HIT"
The X-Cache-Status header indicates cache performance for static assets:
HIT: Served from cacheMISS: Fetched from originUPDATING: Serving stale content while updatingSTALE: Serving stale content due to origin errorIf you see SSL handshake errors in the logs, ensure:
proxy_ssl_server_name on; directive is present (already included)If events work but recordings don't:
/static/recorder.js requests are succeedingThis means the referer check is failing:
ALLOWED_DOMAIN matches your actual domainReferer headerlocalhost are allowedThe proxy is configured to handle PostHog's IP rotation automatically. If you still experience issues:
valid=300s)To modify the proxy configuration:
entrypoint.sh for nginx configuration changesDockerfile for build-time changesdocker build -t test . && docker run -p 8080:8080 testdocker exec <container> nginx -tThis configuration is provided as-is for use with PostHog services.
Content type
Image
Digest
sha256:8b61ea0d1…
Size
6.9 MB
Last updated
about 1 year ago
docker pull hiteshjoshi/posthog_reverse_proxy