Nuxt reverse proxy
Contents
- If you use a self-hosted proxy, PostHog can't help troubleshoot. Use our managed reverse proxy if you want support.
- Use domains matching your PostHog region:
us.i.posthog.comfor US,eu.i.posthog.comfor EU. - Don't use obvious path names like
/analytics,/tracking,/telemetry, or/posthog. Blockers will catch them. Use something unique to your app instead.
This guide shows you how to use Nuxt server routes as a reverse proxy for PostHog.
How it works
Nuxt server routes run on the server and can intercept requests before they reach the client. When a request matches your proxy path, the server route fetches the response from PostHog and returns it under your domain.
Here's the request flow:
- User triggers an event in your app
- Request goes to your domain (e.g.,
yourdomain.com/ph/e) - Nuxt's server middleware intercepts requests matching your proxy path
- The server route fetches the response from PostHog's servers
- PostHog's response is returned to the user under your domain
This works because the proxy runs server-side, so the browser only sees requests to your domain. Ad blockers that filter by domain won't block these requests.
Prerequisites
This guide requires a Nuxt 3 project with server-side rendering enabled (the default).
Setup
- 1
Create the server route
Create a file at
server/routes/ph/[...path].ts:The
[...path]in the filename is a Nuxt catch-all route that matches any path after/ph/. This handles all PostHog endpoints like/ph/e,/ph/decide, and/ph/static/array.js.Here's what the code does:
- Routes
/static/*requests to PostHog's asset server and everything else to the main API - Sets the
hostheader so PostHog can route the request correctly - Forwards the client's IP address for accurate geolocation
- Strips content encoding headers to avoid issues with compressed responses
- Routes
- 2
Update your PostHog SDK
In your Nuxt app, update your PostHog initialization to use your proxy path:
The
api_hosttells the SDK where to send events. Using a relative path ensures requests go to your domain. Theui_hostmust point to PostHog's actual domain so features like the toolbar link correctly. - 3
Deploy your changes
Commit and push your changes. The server route will be active once deployed.
In development, restart your dev server after creating the server route.
Verify your setup
CheckpointConfirm events are flowing through your proxy:
- Open your browser's developer tools and go to the Network tab
- Navigate to your site or trigger an event
- Look for requests to your domain with your proxy path (e.g.,
yourdomain.com/ph) - Verify the response status is
200 OK - Check the PostHog app to confirm events appear in your activity feed
If you see errors, check troubleshooting below.
Troubleshooting
Server route not matching
If requests to your proxy path return 404:
- Verify the file is at
server/routes/ph/[...path].ts(notserver/api/) - Check the file extension is
.tsnot.jsif you're using TypeScript - Restart your dev server after creating the file
Static site generation
If you're using nuxt generate for static site generation, server routes won't be available. Use a CDN-level proxy like Cloudflare Workers or a platform-specific option like Netlify redirects or Vercel rewrites instead.
All users show same location
If geolocation data is wrong or all users appear in the same location, verify the x-forwarded-for header is being set correctly. If you're behind multiple proxies, you may need to adjust which header value is forwarded.