Skip to main content

Traffic Policy

warning

Traffic Policy is currently in preview. Breaking changes may occur at any time with no notice, including changes to the structure of policy documents, the behaviors of policies, and the pricing of this feature.

Overview

This module enables you to assign a policy to your endpoints by defining a set of rules for three traffic management phases: on_tcp_connect, on_http_request, and on_http_response. These rules allow you to influence and control traffic to and from your upstream service.

Traffic Policy rules are composed of expressions that filter the traffic on which they are applicable and actions that should take effect.

Example Usage

ngrok http 80 --traffic-policy-file /path/to/policy.yml
policy.yml
on_http_request:
- name: FooBarParamNotFound
expressions:
- "'bar' in getQueryParam('foo')"
actions:
- type: "custom-response"
config:
status_code: 404
content: not found
headers:
content-type: text/plain
- name: "BazCookieForLargeRequests"
expressions:
- "!hasReqCookie('baz')"
- "req.content_length > 5000"
actions:
- type: deny
on_http_response:
- name: "LogUnsuccessfulRequests"
expressions:
- "res.status_code < 200 && res.status_code >= 300"
actions:
- type: log
config:
metadata:
hostport: example.com:443
success: false

Behavior

Traffic Policy rules are evaluated sequentially in the order they are configured with on_tcp_connect and on_http_request rules taking effect before the upstream server is reached and on_http_response rules taking effect after the upstream server responds. Whether or not the configured actions are performed is determined at runtime by the expressions.

Expression Evaluation

Traffic Policy expressions are written using the Common Expression Language (CEL). Traffic Policy expressions must evaluate to true in order for policy actions to take effect. There is no behavioral difference between adding multiple expressions to a single policy rule and having one single expression with multiple statements logically conjoined together (i.e. ["1 == 1 && 2 == 2"] is the same s ["1 == 1", "2 == 2"]).

If no expressions are specified on a policy rule, its actions will always take effect.

See expressions for variable and macro definitions.

Action Execution

If the expressions of a traffic policy rule evaluate to true, the policy's actions will be executed. If multiple actions are defined on a traffic policy, the actions will execute sequentially.

See actions for all available actions.

Reference

Configuration

ParameterDescription
nameTraffic Policy rules can optionally be given a name for convenience.
expressionsA list of CEL expressions that filter which traffic a policy rule will apply to.
actionsA list of actions that will execute sequentially if the associated policy rule's expressions all match on the traffic.
typeThe type of action.
configThe configuration details of how an action should execute. Each action has its own configuration structure.

Edges

Traffic Policy is an HTTPS Edge module which can be applied to Routes.

The Traffic Policy module can be configured via the ngrok dashboard or API.