This guide shows you how to layer ngrok’s Global Server Load Balancing (GSLB) on top of a VM-based deployment on DigitalOcean. GSLB improves performance and resiliency by distributing traffic to the nearest Point of Presence (PoP) and upstream service. Unlike a traditional GSLB deployment, ngrok routes traffic automatically without new infrastructure, IP provisioning, or DNS changes. You configure your ngrok agents for high availability, horizontal scaling, A/B testing, and more. ngrok’s GSLB works on top of DigitalOcean’s internal load balancers for additional global resiliency and lower latency.Documentation Index
Fetch the complete documentation index at: https://ngrok.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
What you’ll need
- An ngrok account.
- A DigitalOcean account.
- Three Ubuntu 24.04 virtual machines (VMs) in three globally distributed regions (for example, New York, Sydney, and Frankfurt).
- Hostnames should be unique, ideally using the datacenter location (for example,
nyc,sydney, andfrankfurt). - Docker and Docker Compose installed on each VM.
- Hostnames should be unique, ideally using the datacenter location (for example,
1. Reserve your ngrok domain
First, generate a new API key in the ngrok dashboard. Save the API key before closing the modal; it won’t be displayed again. To simplify authenticating your account with the ngrok API, export the API key on your local workstation.YOUR_COMPANY-digitalocean-gslb.ngrok.app would work great.
You can reserve your domain in one of two ways: with the ngrok API, or in the ngrok dashboard.
With the ngrok API, reserve your domain on the /reserved_domains endpoint using the NGROK_API_KEY and NGROK_DOMAIN variables you exported.
Export a variable for your new domain, which will be used in following API calls.
2. Create a Cloud Endpoint
Create apolicy.yaml file on your local workstation with the Traffic Policy for your endpoint.
This policy forwards all incoming requests to https://gslb-demo.internal, a shared internal domain that all three of your VMs’ ngrok agents will connect to.
ngrok automatically routes each request to the nearest connected agent, providing global load balancing without additional configuration.
jq to safely embed the Traffic Policy YAML in the JSON payload and create your Cloud Endpoint:
id from the response for use in optional step 5.
https://<YOUR_NGROK_DOMAIN> now, you’ll see an error from ngrok: the domain and endpoint configuration exist, but there are no ngrok agents connected yet.
You can go to the Endpoints dashboard to see your endpoint.
3. Install the ngrok Agent and an example workload on each VM
To get some agents online and quickly see how ngrok’s GSLB works, you’ll use an example API deployment. This demo deployment has four parts:- A straightforward Go-based API with a single endpoint at
/api, which returns a randomly generated UUID and the machine’shostname, which should reflect the regions of your VMs. - A
Dockerfilefor containerizing said Go-based API. - A
docker-compose.ymlfile for starting the API container and a containerized edition of the ngrok agent on the same network. - A
ngrok.ymlagent configuration file to connect the agent to your ngrok account and the shared internal domain.
-
Clone the demo repository.
-
Edit the
ngrok.ymlngrok agent configuration file with your<YOUR_NGROK_AUTHTOKEN>(find it in the ngrok dashboard); it’s different from the API key you created in the first step. All three VMs use the sameurlvalue—ngrok automatically routes each incoming request to the nearest connected agent. -
Build and start the containerized API deployment, passing the hostname of the VM to the hostname of the Docker container.
4. Test out ngrok’s Global Server Load Balancing
ngrok is now load-balancing your single API endpoint across all three distributed VMs. You can now ping your demo API at<YOUR_NGROK_DOMAIN>/api to see which agent responds.
curl requests.
skip-validation
Step 5 (optional): Extend with Traffic Policy
Your demo API is globally load-balanced with equal distribution across your three VMs. You can extend your endpoint’s Traffic Policy in two ways: adding API gateway features like header injection, or configuring custom weights for fine-grained traffic control.Add API gateway features
Update yourpolicy.yaml to include an on_http_response phase that adds headers to every response:
country (with the appropriate code for your request origin) and is-ngrok headers.
Configure weighted distribution
To route traffic by explicit weight across specific regions—for example, to run A/B tests or shift capacity—update each VM’sngrok.yml to use a unique per-region internal domain instead of the shared one:
- NYC VM:
url: https://nyc.internal - Sydney VM:
url: https://sydney.internal - Frankfurt VM:
url: https://frankfurt.internal
policy-weighted.yaml with a Traffic Policy that uses set-vars to select a backend by weight on each request.
Adjust the weight values as needed; the cumulative_weights list must be recomputed to match (each entry is the sum of all weights up to and including that index).
What’s next?
You now have a globally load-balanced API deployment using three DigitalOcean VMs, three ngrok agents, three secure tunnels, and a single convenient endpoint for your users. From here, you have many options for extending your use of ngrok’s GSLB:- Spread the load further by deploying additional VMs in more regions and connecting each agent to
https://gslb-demo.internal—ngrok will automatically include them in load balancing. - Add more VMs in an existing region and connect them to the same internal domain; ngrok will equally distribute traffic among all connected agents for that domain.
- Provision a Kubernetes cluster with the same workload and the ngrok Kubernetes Operator to load-balance between VM- and Kubernetes-based deployments of the same API or application.
- Use the weighted distribution Traffic Policy for A/B tests by adjusting weights between regions.