Cloudflare 中文文档
Rules
Rules
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Create an origin rule via API

Use the Rulesets API to create origin rules via API.

​​ Basic rule settings

When creating an origin rule via API, make sure you:

  • Set the rule action to route.
  • Define the parameters in the action_parameters field according to the type of origin override.
  • Deploy the rule to the http_request_origin phase at the zone level.

​​ Procedure

Follow this workflow to create an origin rule for a given zone via API:

  1. Use the List zone rulesets operation to check if there is already a ruleset for the http_request_origin phase at the zone level.

  2. If the phase ruleset does not exist, create it using the Create a zone ruleset operation. In the new ruleset properties, set the following values:

    • kind: zone
    • phase: http_request_origin
  3. Use the Update a zone ruleset operation to add an origin rule to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.

Make sure your API token has the required permissions to perform the API operations.

​​ Example requests

Example: Add a rule that overrides the HTTP Host header

The following example sets the rules of an existing phase ruleset ({ruleset_id}) to a single origin rule — overriding the HTTP Host header — using the Update a zone ruleset operation:

The response contains the complete definition of the ruleset you updated.

Example: Add a rule that overrides the SNI value of incoming requests

The following example sets the rules of an existing phase ruleset ({ruleset_id}) to a single origin rule — overriding the SNI value of incoming requests addressed at admin.example.com — using the Update a zone ruleset operation:

Example: Add a rule that overrides the resolved DNS record and the Host header of incoming requests

The following example sets the rules of an existing phase ruleset ({ruleset_id}) to a single origin rule — overriding the resolved DNS record and the Host header of incoming requests — using the Update a zone ruleset operation:

The response contains the complete definition of the ruleset you updated.

Example: Add a rule that overrides the port of incoming requests

The following example sets the rules of an existing phase ruleset ({ruleset_id}) to a single origin rule — overriding the port of incoming requests — using the Update a zone ruleset operation:

The response contains the complete definition of the ruleset you updated.


​​ Required API token permissions

The API token used in API requests to manage origin rules must have at least the following permission:

  • Origin > Edit