Railway 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 deploy a PostHog reverse proxy on Railway.
How it works
Railway provides a one-click deployment template that runs an nginx-based reverse proxy. When deployed, Railway gives you a domain that routes all requests to PostHog's servers.
Here's the request flow:
- User triggers an event in your app
- Request goes to your Railway domain (e.g.,
your-project.up.railway.app) - The nginx proxy running on Railway forwards the request to PostHog
- PostHog processes the request and returns a response
- The proxy returns PostHog's response to the user
This works because ad blockers see requests going to Railway's domain (or your custom domain), not PostHog's.
Prerequisites
This guide requires a Railway account (free tier works).
Setup
- 1
- 2
Configure your region
During deployment, Railway prompts you to set the
POSTHOG_CLOUD_REGIONenvironment variable:- Set to
usfor US region - Set to
eufor EU region
This tells the proxy which PostHog servers to forward requests to. Make sure this matches your PostHog project's region.
- Set to
- 3
Wait for deployment
Railway automatically builds and deploys your proxy. This usually takes 1-2 minutes.
Once deployed, Railway provides you with a domain like
your-project-name.up.railway.app. You can find this in your project's Settings → Networking section. - 4
Update your PostHog SDK
In your application code, update your PostHog initialization to use your Railway domain:
Replace
your-project-name.up.railway.appwith your actual Railway domain. - 5
Add a custom domain
OptionalFor better blocking resistance, use your own domain instead of Railway's default:
- Go to your Railway project's Settings → Networking
- Click Add Domain
- Follow Railway's custom domains guide to configure DNS
Then update your SDK to use your custom domain:
Verify your setup
CheckpointConfirm events are flowing through your proxy:
Test the proxy directly:
TerminalYou should see a
200 OKresponse.Open your browser's developer tools and go to the Network tab
Trigger an event in your app
Look for requests to your Railway domain
Verify the response status is
200 OKCheck the PostHog app to confirm events appear
If you see errors, check troubleshooting below.
Troubleshooting
Proxy stops working periodically
If your proxy stops responding intermittently:
- Check sleep settings: Free tier services may sleep after inactivity. Go to Settings → Service and check if sleep mode is enabled.
- Monitor resource usage: Check the Metrics tab for memory or CPU spikes that might cause restarts.
- View logs: Look for error messages in the Deployments tab.
- Consider upgrading: Paid plans include better uptime guarantees and disable sleep mode.
If issues persist, check Railway's status page.
Deployment failed
If the initial deployment fails:
- Check the build logs for error messages
- Verify you set the
POSTHOG_CLOUD_REGIONvariable correctly (must beusoreu) - Try redeploying from the Railway dashboard
502 Bad Gateway errors
If the proxy returns 502 errors:
- Verify the
POSTHOG_CLOUD_REGIONvariable matches your PostHog project's region - Check that the Railway service is running in the dashboard
- View the service logs for connection errors