Skip to content

Example custom error rules

Last updated View as MarkdownAgent setup

The provided examples use the following fields in their rule expressions:

  • http.response.code (Response Status Code): Represents the HTTP status code returned to the client, either set by a Cloudflare product or returned by the origin server. Use this field to customize the response for error codes returned by the origin server or by a Cloudflare product such as a Worker.

  • cf.response.1xxx_code: Contains the specific error code for Cloudflare-generated errors. This field will only work for Cloudflare-generated errors such as 52X and 1XXX.

Custom JSON response for all 5XX errors

This example configures a custom JSON error response for all 5XX errors (500-599) in a zone. The HTTP status code of the custom error response will be set to 530.

Custom error rule configuration:

  • Name: Custom JSON response for all 5XX errors

  • If incoming requests match > Custom filter expression:

    Field Operator Value Logic
    Response Status Code greater than or equal to 500 And
    Response Status Code less than or equal to 599

    If using the Expression Editor:
    (http.response.code ge 500 and http.response.code le 599)

  • Response type: JSON response

  • Response code: 530

  • JSON response: {"message": "A server error occurred."}

Required API token permissions

At least one of the following token permissions is required:
  • Response Compression Write
  • Config Settings Write
  • Dynamic URL Redirects Write
  • Cache Settings Write
  • Custom Errors Write
  • Origin Write
  • Managed headers Write
  • Zone Transform Rules Write
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • HTTP DDoS Managed Ruleset Write
  • Sanitize Write
  • Transform Rules Write
  • Select Configuration Write
  • Bot Management Write
  • Zone WAF Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
  • Logs Write
Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/