Route to Endpoints by Geography
To route traffic to specific regions, you can use ngrok’s Cloud Endpoints, internal Agent Endpoints, and Traffic Policy engine to match incoming traffic based on the client's IP geolocation data using ISO 3166 country codes, then forward requests to the appropriate regional endpoints.
With this setup, you can:
- Showcase user content tailored to their region, like country-specific pricing.
- Comply with regulations like GDPR.
- Ensure your customers' traffic is routed to the nearest possible endpoint for the best latency.
This also implements the 'front door' pattern, which creates a loose coupling between the traffic management logic at your gateway and your services.
1. Start endpoints for your services
On a server designated for US traffic, start an internal Agent Endpoint, replacing $PORT
based on where your service listens.
You can also use one of our SDKs or the Kubernetes Operator.
Loading…
On a server designated for Canadian traffic, start a second endpoint.
Loading…
2. Reserve a domain
Navigate to the Domains section of the ngrok dashboard and click New + to reserve a free static domain like https://your-service.ngrok.app
or a custom domain you already own.
3. Create a Cloud Endpoint
Navigate to the Endpoints section of the ngrok dashboard, then click New + and Cloud Endpoint.
In the URL field, enter the domain you just reserved to finish creating your Cloud Endpoint.
4. Add routing to your services with Traffic Policy
While still viewing your new cloud endpoint in the dashboard, copy and paste the policy below into the Traffic Policy editor.
Loading…
What's happening here? This policy checks checks every HTTP request for where it originated from. If that's the US or Canada, ngrok forwards the traffic to its respective endpoint. If the traffic comes from any other country, ngrok forwards it to a "default" service of your choosing.
5. Try out your endpoint
Visit the domain you reserved either in the browser or in the terminal using a tool like curl
.
You should see the app or service at the port connected to your internal Agent Endpoint.
What's next?
- Read more about Traffic Policy and its core concepts.
- View your traffic in Traffic Inspector.
- Check out Endpoint Pools for dead-simple load balancing.