Note: If you are using the EU cloud then use
eu
instead ofus
in all domains (e.g.us.i.posthog.com
->eu.i.posthog.com
)
If you are using Next.js, you can take advantage of rewrites to behave like a reverse proxy. To do so, add a rewrites()
function to your next.config.js
file:
// next.config.jsconst nextConfig = {async rewrites() {return [{source: "/ingest/static/:path*",destination: "https://us-assets.i.posthog.com/static/:path*",},{source: "/ingest/:path*",destination: "https://us.i.posthog.com/:path*",},];},}module.exports = nextConfig
Then configure the PostHog client to send requests via your rewrite.
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {api_host: "https://your-host.com/ingest"ui_host: 'https://app.posthog.com' // or 'https://eu.posthog.com' if your PostHog is hosted in Europe})
If this isn't working for you (returning
503
errors), it may be an issue with how your hosting service (such as Heroku) handles rewrites. You can write Next.js middleware to proxy requests instead.