Page Rule Conditions

Page rule condition configuration allows the definition of multiple conditions, which are logically combined with an AND relationship. This means the page rule will only be triggered, and the defined actions will be performed only when all conditions are met. These actions could include page rule actions, Web Application Firewall (WAF), proxy, caching, etc.

although conditions are not required in the page rule configuration, at least one action must be defined.

Parameter Explanation

Parameter NameData TypeRequiredDescription
varstring or arrayYesVariable name, examples include uri or [‘req-header’, ‘Referer’]
opstringNoOperator
valsstringNoComparison values for the condition can be multiple, which are related by “OR”

Common string type “variable names (var)” include:

  • uri: The request’s URI, for example, /hello.html.
  • sorted-query-string: Sorted query parameters.
  • query-string: Query parameters.
  • host: Hostname.
  • user-agent: User-agent.
  • scheme: Protocol type, could be http or https.
  • client-country: Client’s country or region code, such as US for the United States, UK for the United Kingdom, and CN for China.
  • client-addr: Client IP address.
  • server-addr: Server IP address.
  • server-port: Server port number.
  • req-method: HTTP request method, such as CONNECT, DELETE, GET, HEAD, POST, PUT, TRACE, OPTIONS, PATCH, etc.
  • first-x-forwarded-addr: The first IP address in the X-Forwarded-For request header, for example, 1.1.1.1 in 1.1.1.1,2.2.2.2.
  • last-x-forwarded-addr: The last IP address in the X-Forwarded-For request header, for example, 2.2.2.2 in 1.1.1.1,2.2.2.2.
  • http-version: HTTP version number could be 2.0, 1.1, 1.0, 0.9.

Common array type “variable names (var)” include:

  • uri-arg: Parameters in the URI, for example, username in “hello.html?username=openresty”.
  • req-header: Request header information, for example, Host in “Host: openresty.com”.
  • req-cookie: Cookies in the request, for example, token in “Cookie: token=tokenvalue”.

Common “operators (op)” include:

  • String comparison operators: eq (equal), ne (not equal), lt (less than), le (less than or equal to), gt (greater than), ge (greater than or equal to).
  • String length comparison operators: str-len-eq (length equal), str-len-ne (length not equal), str-len-lt (length less than), str-len-le (length less than or equal to), str-len-gt (length greater than), str-len-ge (length greater than or equal to).
  • Regular expression matching operators: prefix (prefix match), !prefix (prefix does not match), suffix (suffix match), !suffix (suffix does not match), contains (contains), !contains (does not contain).
  • IP address comparison operators: ~~ (IP match), !~~ (IP does not match).

Examples

- enable_rule: true
  conditions:
  - var: uri
    op: eq
    vals:
      - /robots.txt

  actions:
    print:
      content_type: text/html
      msg: 'Hello World'

In this example, if the request’s URI equals /robots.txt, the action will be executed, printing out Hello World.

- enable_rule: true
  conditions:
  - var:
    - req-header
    - Host
    op: suffix
    vals:
      - openresty.com

  actions:
    print:
      content_type: text/html
      msg: 'Hello World'

In this example, if the Host suffix in the request header matches openresty.com, then the action will be executed, printing out Hello World.