TL;DRTo use ngrok’s Global Server Load Balancing with DigitalOcean:
- Reserve your ngrok domain
- Create your ngrok Edge
- Create Tunnel Group backends for your VMs
- Create a Weighted Backend and Route on your Edge
- Install the ngrok agent and an example workload on each VM
- Test out ngrok’s Global Server Load Balancing
- (optional) Enable the Traffic Policy module for API gateway features
This how-to guide requires:
- An ngrok account.
- An account with DigitalOcean.
- 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 location of the datacenter (for example,
nyc,sydney, andfrankfurt). - Docker and Docker Compose installed on each VM.
- Hostnames should be unique, ideally using the location of the datacenter (for example,
1. Reserve your ngrok domain
Your first task is to generate a new API key in the ngrok dashboard. Make sure you save the API key before you close the modal, because 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.
Custom domains within ngrokUsing CNAMEs, ngrok can host an endpoint on your domain while also managing the complete TLS certificate lifecycle on your behalf. If you’d prefer to use a custom domain rather than an ngrok-managed one, follow our guide to set up your DNS and replace your custom domain in the
export command above.2. Create your ngrok Edge
With ngrok, you can manage multiple endpoints, such as domains or routes, using a single Edge configuration in the cloud. Edges let you update endpoints without taking them offline, connect multiple ngrok agents to a single endpoint, apply modules like OAuth or IP restrictions, and balance the load between your upstream services. Send aPOST request to the /edges/https endpoint, replacing the description field below as needed.
id in this response, which begins with edghts_..., for future use.
3. Create Tunnel Group backends for your VMs
Next, create a Tunnel Group for each of your globally distributed VMs. A Tunnel Group uses one or more labels to identify which agent-created tunnels it should attach to a given Edge and route, which you’ll create in a moment. You’ll need to run the API request to the/backends/tunnel_group endpoint for each of your VMs, changing the <LOCATION_NAME> with the name of the city or region of the data center where they’re deployed (for example, nyc, sydney, and frankfurt).
id, beginning with bkdtg_..., returned from the ngrok API for each Tunnel Group you create.
4. Create a Weighted Backend and Route on your Edge
With a Weighted Backend, you can specify custom weights for Tunnel Group backends, shaping precisely how to load-balance traffic across the infrastructure you’ve distributed around the globe. Without a Weighted Backend, the ngrok Edge and GSLB will always favor the Point of Presence (PoP) and VM nearest the user making requests. Create a Weighted Backend at the/backends/weighted endpoint using your Tunnel Group backend ids exported from the previous step. The curl command below creates equal weighting for your backends, but you can use integers between 0-10000 to configure precise proportional weights.
id of your new Weighted Backend.
/ using your Weighted Backend using the /edges/https/{EDGE_ID}/routes endpoint.
Make sure you grab the right idThe command starts with two ID values (
edge_id and id), be sure to use id with the value that starts with edghtsrt.https://<YOUR_NGROK_DOMAIN> right now, you’ll see an error message from ngrok. You’ve successfully reserved a domain and created an edge, but you don’t yet have ngrok agents or active tunnels.


5. Install the ngrok agent and an example workload on each VM
To get some tunnels 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 one of the Tunnel Group backends you previously created.
-
Clone the demo repository.
-
Edit the
ngrok.ymlngrok agent configuration file with your<YOUR_NGROK_AUTHTOKEN>, which you can find in the ngrok dashboard—note that it’s different from the API key you created in the first step. Next, edit the<LOCATION_NAME>for that VM, matching the labels you created in step 3 (for example,nyc,sydney, andfrankfurt). -
Build and start the containerized API deployment, passing the hostname of the VM to the hostname of the Docker container.
6. 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 backend responds.
curl requests.
skip-validation

Step 7 (optional): Enable the Traffic Policy module for API gateway features
Your demo API is already globally load-balanced, but if you want to extend your ngrok usage even further, you can enable one or more Traffic Policy modules on your Edge to control traffic or establish standards for how your upstream services are accessed. You have two options:-
Using
curlwith the ngrok API’s/edges/https/{edge_id}/routes/{id}/policyendpoint. For this step, you will need to have exported theROUTE_IDenvironment variable in step #4. -
In the Traffic Policy module section of your Edge in the ngrok dashboard, click the Edit Traffic Policy button and then the YAML button and add the YAML below.
country (with the appropriate code for where you’re generating said request) and is-ngrok headers.
What’s next?
You now have a globally load-balanced API deployment using three DigitalOcean VMs, three ngrok agents, three secure tunnels, one ngrok Edge, 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 from user requests further by creating additional deployments in more regions, adding a new Tunnel Group backend for each, and patching your Weighted Backend configuration.
- Add more VMs to a region with existing deployments and add them to the relevant Tunnel Group backend. ngrok will then load-balance between those specific VMs equally after weighting requests on the Backend level.
- 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 your Weighted Backend for A/B tests by changing your deployments between different regions and VMs.