跳转到内容
搜索文档

为默认内容类型启用 Zstandard 压缩

创建一条 compression rule,对 Cloudflare 默认应用压缩的响应内容类型启用 Zstandard 压缩。

最后更新 查看 MarkdownAgent 设置

创建一条 compression rule,对 Cloudflare 默认会应用压缩的响应内容类型启用 Zstandard 压缩。

以下示例规则会对 Cloudflare 默认应用压缩 的响应内容类型启用 Zstandard 压缩。如果客户端不支持 Zstandard 压缩,将回退使用 Brotli 或 Gzip 压缩。

当传入请求匹配时

  • 自定义筛选表达式:
    • Media Type is in text/html, text/richtext, text/plain, text/css, text/x-script, text/x-component, text/x-java-source, text/x-markdown, application/javascript, application/x-javascript, text/javascript, text/js, image/x-icon, image/vnd.microsoft.icon, application/x-perl, application/x-httpd-cgi, text/xml, application/xml, application/rss+xml, application/vnd.api+json, application/x-protobuf, application/json, multipart/bag, multipart/mixed, application/xhtml+xml, font/ttf, font/otf, font/x-woff, image/svg+xml, application/vnd.ms-fontobject, application/ttf, application/x-ttf, application/otf, application/x-otf, application/truetype, application/opentype, application/x-opentype, application/font-woff, application/eot, application/font, application/font-sfnt, application/wasm, application/javascript-binast, application/manifest+json, application/ld+json, application/graphql+json, application/geo+json

然后

  • 压缩选项:自定义
  • 为压缩类型定义自定义顺序ZstandardBrotliGzip

以下示例使用 Update a zone ruleset 操作,将现有 entry point ruleset(ID 为 {ruleset_id})在 http_response_compression 阶段的规则设置为单条 compression rule:

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 rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"ref": "use_zstd_compression",
						"expression": "(http.response.content_type.media_type in {\"text/html\" \"text/richtext\" \"text/plain\" \"text/css\" \"text/x-script\" \"text/x-component\" \"text/x-java-source\" \"text/x-markdown\" \"application/javascript\" \"application/x-javascript\" \"text/javascript\" \"text/js\" \"image/x-icon\" \"image/vnd.microsoft.icon\" \"application/x-perl\" \"application/x-httpd-cgi\" \"text/xml\" \"application/xml\" \"application/rss+xml\" \"application/vnd.api+json\" \"application/x-protobuf\" \"application/json\" \"multipart/bag\" \"multipart/mixed\" \"application/xhtml+xml\" \"font/ttf\" \"font/otf\" \"font/x-woff\" \"image/svg+xml\" \"application/vnd.ms-fontobject\" \"application/ttf\" \"application/x-ttf\" \"application/otf\" \"application/x-otf\" \"application/truetype\" \"application/opentype\" \"application/x-opentype\" \"application/font-woff\" \"application/eot\" \"application/font\" \"application/font-sfnt\" \"application/wasm\" \"application/javascript-binast\" \"application/manifest+json\" \"application/ld+json\" \"application/graphql+json\" \"application/geo+json\"})",
						"action": "compress_response",
						"action_parameters": {
								"algorithms": [
										{
												"name": "zstd"
										},
										{
												"name": "brotli"
										},
										{
												"name": "gzip"
										}
								]
						}
				}
		]
	}'

使用 Terraform 时,通过 ref 字段可在更新后保持稳定的规则 ID。添加该字段可防止 Terraform 在变更时重建规则。更多信息请参阅 故障排除(Terraform 文档)。

这篇文档对您有帮助吗?