Recipe

Route traffic to different endpoints by geography

How to use this recipe

1. Create two internal agent endpoints

On a server designated for US traffic, start an ngrok agent.

ngrok http 8080 --url https://us-service.internal

On a server designated for Canadian traffic, start an ngrok agent.

ngrok http 8080 --url https://ca-service.internal

2. Reserve a domain

Navigate to the Domains section of the ngrok dashboard and click New + to reserve a domain like https://your-api.ngrok.app.

3. Create a cloud endpoint

Navigate to the Endpoints section of the ngrok dashboard, then click New + and Cloud Endpoint to create a new cloud endpoint with the URL https://your-api.ngrok.app.

4. Apply Traffic Policy to your cloud endpoint

Apply this chain of Traffic Policy rules to your public cloud endpoint.

on_http_request:
  - expressions:
      # Check if the client is from the US
      - "conn.geo.country_code == 'US'"
    actions:
      # Forward the request to the US internal service
      - type: forward-internal
        config:
          url: http://us-service.internal
  - expressions:
      # Check if the client is from Canada
      - "conn.geo.country_code == 'CA'"
    actions:
      # Forward the request to the CA internal service
      - type: forward-internal
        config:
          url: http://ca-service.internal

What you get from this recipe

If ngrok identifies a request as coming from the US country code, it is directed to the US-based internal agent endpoint. If the country code is CA, then it’s forwarded to your Canadian internal agent endpoint.

What's next?