Page Rules - Conditions

Page rule condition configuration allows the defining of multiple conditions with a logical “AND” relationship. This means the page rule will only be triggered when all conditions are met, and the actions defined by the rule will be executed. These actions can be page rule actions, Web Application Firewall (WAF), proxy, cache, etc.

Please note that although conditions are not required in page rule configuration, at least one action needs to be defined.

Parameter Description

ParameterData TypeRequiredDescription
varstring or arrayYesVariable name, examples include uri or [‘req-header’, ‘Referer’] etc.
opstringNoOperator
valsstringNoComparison value for the condition, can have multiple values with an “OR” relationship between them

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

  • uri: The requested URI, e.g., /hello.html.
  • sorted-query-string: Sorted query parameters.
  • query-string: Query parameters.
  • host: Hostname.
  • user-agent: User agent.
  • scheme: Protocol type, can be http or https.
  • client-country: Client’s country or region code, e.g., US for United States, UK for United Kingdom, 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, e.g., 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, e.g., 2.2.2.2 in 1.1.1.1,2.2.2.2.
  • http-version: HTTP version number, can be 2.0, 1.1, 1.0, 0.9.

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

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

Common “operators (op)” include:

  • String comparison operators: eq (equal to), ne (not equal to), 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 to), str-len-ne (length not equal to), 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 not match), suffix (suffix match), !suffix (suffix not match), contains (contains), !contains (does not contain).
  • IP address comparison operators: ~~ (IP match), !~~ (IP not match).

Examples

General 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 requested 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 in the request header has a suffix matching openresty.com, the action will be executed, printing out Hello World.

Using IP List

- enable_rule: true
  order: 5
  conditions:
  - var: client-addr
    op: ~~
    vals:
    - val: global-ip-list1
      type: ip-list
    - val: app-ip-list1
      type: app-ip-list
  actions:
  - set-uri:
      uri: /hello

In this example, if the client address of the request is in the global IP list named global-ip-lists1 or the application IP list named app-ip-list1, the request URI will be set to /hello.

Using User Variables

- enable_rule: true
  order: 6
  conditions:
  - user_var: hello
    op: prefix
    val: hello
  - global_var: world
    op: prefix
    val: world
  actions:
  - set-uri:
      uri: /anything

In this example, if the value of the application user variable hello has a prefix matching hello and the value of the global user variable world has a prefix matching world, the request URI will be set to /anything.