example.com.
For example, app.example.com can serve a static web app while api.example.com serves a JSON API using entirely different services and infrastructure.
With ngrok Cloud Endpoints and Traffic Policy, you can dynamically determine where your traffic is forwarded to.
ngrok simplifies multiplexing by letting you:
- Put a URL online to accept internet traffic to a Cloud Endpoint, which allows you to control how that traffic accesses the internal resources where your internal Agent Endpoints are running. This is called the “front door” pattern.
- Use Traffic Policy to create dynamic routing topologies based on subdomains or headers.
- Add new services behind your gateway without service discovery, manual registration, certificate management, split-horizon DNS zones, or any other complex networking configurations.
- Remove the need to manage multiple domains or subdomains for each service, which can be cumbersome and error-prone.
Create endpoints for your services
Start internal Agent Endpoints, replacing Make sure the hostname of the internal endpoint matches the subdomain or header value you want to use for routing.
$PORT based on where your service listens, for each of your services on the systems where they run.
You can also use one of the SDKs or the Kubernetes Operator.https://foo.example.com→https://foo.internalx-service: bar→https://bar.internal
Reserve a domain
Navigate to the Domains section of the ngrok dashboard and click New + to reserve a wildcard domain like
*.example.com.
You’ll then need to set up CNAME records with your DNS provider.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.
Add multiplexing with Traffic Policy
While still viewing your new Cloud Endpoint in the dashboard, copy one of the three policies below and paste it into the Traffic Policy editor.
The choice depends on whether you want to route by hostname/subdomain, header value, or with global variables that are accessed at runtime.What’s happening here?
This policy forwards every HTTP request to one of your internal Agent Endpoints, which in turn forward traffic to your internal services.
The
- By subdomain
- By header
- By allowed subdomains
split macro dynamically routes traffic based on the incoming hostname so that the policy forwards requests to the foo subdomain to https://foo.internal, and so on.What’s next?
- Explore more routing options, like routing by cookie, query parameter, geographic location, IP intelligence, or device type.
- Add authentication like OAuth, OIDC, or JWT validation to some or all of your multiplexed services, or even combine multiple methods to let users authenticate with one
ORanother method. - View your traffic in Traffic Inspector to ensure multiplexing works as expected or to find potentially abusive patterns you can block using IP Intelligence or the OWASP action.