Load Balancing expressions
Overview
When working with Load Balancing rules, you will encounter two kinds of expression:
Simple expressions compare a value from an HTTP request to a value defined in the expression. You can identify a simple expression by the presence of a comparison operator (equals or less than, for example).
Compound expressions combine two or more simple expressions into a single expression. You can identify a compound expression by the presence of a logical operator (and, or, for example). With compound expressions you can tailor rules to specific use cases with a high degree of accuracy and precision.
The Create Custom Rule dialog in the Traffic app provides two interfaces for creating expressions: the Expression Builder and the Expression Editor.
Simple expressions
Simple expressions are composed of three elements:
- A field that represents a property of an HTTP request.
- A representative value for that field, which Cloudflare compares with the actual value from the request.
- A comparison operator, which specifies how the value defined in the expression must relate to the actual value from the request for the operator to return
true
.
When the comparison operator returns true
, the request matches the expression.
This example expression returns true when a request URI path contains /content
:
(http.request.uri.path contains "/content")
In general, simple expressions use this pattern:
<field> <operator> <value>
For more, see Supported fields and operators.
Compound expressions
A compound expression uses a logical operator (and, or, for example) to combine two or more expressions. Compound expressions are powerful because they allow you to build complex statements within a single expression.
This example expression returns true when both the HTTP request URI path contains /content
and the query string contains stream1
:
(http.request.uri.path contains "/content") and (http.request.uri.query contains "webserver")
In general, compound expressions use this pattern:
<expression> <logical operator> <expression>
Note that a compound expression can itself be one of the operands of a logical operator. In this way, you can use multiple operators to construct a compound expression from many individual expressions.
For more, see Supported fields and operators.
Working with expressions
The Expression Builder’s visual interface allows you to build expressions without worrying about field names and syntax.
By comparison, the Expression Editor is text only, but it supports advanced features not available in the builder.
Expression Builder
Complex expressions are easier to scan when displayed in the Expression Builder’s visual interface:
Notice how the Expression Preview displays the expression in text:
(http.request.uri.path contains "/content") and (http.request.uri.query contains "webserver")
The Expression Preview is a great reference for learning to write more advanced expressions.
For a walkthrough, see Creating Load Balancing rules.
Expression Editor
The Expression Editor is a text-only interface for creating Load Balancing expressions. Although it lacks the visual simplicity of the Expression Builder, the Expression Editor editor supports advanced features, such as support for grouping symbols (parentheses).
To access the Expression Editor in the Traffic app, click Edit expression in the Create Custom Rule dialog:
The Expression Editor displays:
To return to the builder, click Use expression builder.
Rules lists
Rules lists allow you to create a group of IP addresses and refer to them collectively, by name, in your Load Balancing expressions.
For example, you might create a list of known office IP addresses and use it in a Load Balancing rule that directs requests from that office to a specific origin pool.
Cloudflare stores your lists at the account level, so you can view, manage, and incorporate them into Load Balancing rules for any of your zones.
For more, see Rules lists in the Firewall Rules documentation.