Using Netlify redirects as a reverse proxy

Last updated:

|Edit this page

Note: If you are using the EU cloud then use eu instead of us in all domains (e.g. us.i.posthog.com -> eu.i.posthog.com)

Netlify supports redirects and rewrites which we can use as a reverse proxy from an /ingest route. In your netlify.toml file, add a redirect like this:

JavaScript
[[redirects]]
from = "/ingest/static/*"
to = "https://us-assets.i.posthog.com/static/:splat"
host = "us-assets.i.posthog.com"
status = 200
force = true
[[redirects]]
from = "/ingest/*"
to = "https://us.i.posthog.com/:splat"
host = "us.i.posthog.com"
status = 200
force = true

Once done, set the /ingest route of your domain as the API host in your PostHog initialization like this:

JavaScript
posthog.init('<ph_project_api_key>',
{
api_host: 'https://www.your-domain.com/ingest',
ui_host: 'https://us.posthog.com' // or 'https://eu.posthog.com' if your PostHog is hosted in Europe
}
)

Once updated, deploy your changes on Netlify and check that PostHog requests are going to https://www.your-domain.com/ingest by checking the network tab on your domain.

Questions?

Was this page useful?

Next article

Using Next.js rewrites as a reverse proxy

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 and the skipTrailingSlashRedirect option to your next.config.js file: Then configure the PostHog client to send requests via your rewrite. 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. Setup video

Read next article