Skip to content
16 changes: 16 additions & 0 deletions docs/http/traffic-policy/gallery.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Deny,
DeprecateVersion,
EnforceTLS,
ForwardInternalFull,
JWTsRateLimiting,
LimitSize,
LogUnsuccessful,
Expand Down Expand Up @@ -156,3 +157,18 @@ Prevent obsolete and potentially vulnerable browsers, SDKs, or CLI tools like `c
Connect your API to ngrok's [event logging system](/docs/obs/index.mdx) for smarter troubleshooting of your API gateway and upstream services.

<LogUnsuccessful />

### Using forwardInternal and URL Rewrite Actions to route requests to different ports based on the request path

This example provides a full Agent [configuration file](/docs/agent/config/) that uses [Internal Endpoints](/docs/network-edge/internal-endpoints/) plus the [forwardInternal](/docs/http/traffic-policy/actions/forward-internal/) and
[URL Rewrite](/docs/http/traffic-policy/actions/url-rewrite/) Traffic Policy Actions to route requests to differnt upstream ports based on the URL path of the request.
This allows us to use one domain to route to multiple services (ports) based on the request path sent in each request.

https://mypublic.ngrok.app/... --> http://localhost:80/...
https://mypublic.ngrok.app/service1/... --> http://localhost:81/...

In this example, we are creating a "public" endpoint using an ngrok reserved domain "mypublic.ngrok.app", an "internal" endpoint (only accessible inside your acccount) "service1.internal",
then using the forwardInternal action to forward requests that have "/service1" in the path to a different port.
Finally, we remove the added path from the request before sending it to the upstream service using the URL Rewrite action. This path then does not have to exist on the upstream service, creating a "virtual" "service1" path on the public ngrok endpoint.

<ForwardInternalFull />
61 changes: 61 additions & 0 deletions traffic-policy/gallery.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,65 @@ export const LimitSize = () => (
/>
);

export const ForwardInternalFull = () => (
<ConfigExample
config={{
version: "3",
agent: {
authtoken: 4nq9771bPxe8ctg7LKr_2ClH7Y15Zqe4bWLWF9p,
},
endpoints: [
{
name: httpexample,
url: https://mypublic.ngrok.app,
upstream: {
url: localhost:80
},
traffic_policy: {
on_http_request: [
{
expressions: [
"req.url.path.startsWith('/service1')",
],
actions: [
{
type: forward-internal,
config: {
url: https://service1.internal:443,
},
},
],
},
],
},
{
name: example-internal-1,
url: service1.internal,
upstream: {
url: localhost:81,
}
traffic_policy: {
on_http_request: [
{
expressions: [
"req.url.path.startsWith('/service1')",
],
actions: [
{
type: "url-rewrite",
config: {
from: "/service1/?([.*]+)?",
to: "/$1",
},
},
],
},
],
},
},
],
}}
/>
);

;
Loading