跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

DNS 最大 TTL

最后更新 查看 MarkdownAgent 设置

为 Gateway 返回的 DNS 响应设置最大生存时间(TTL)。如果上游 DNS 记录的 TTL 超过配置的最大值,Gateway 会将其限制为指定值。较低的值能让 DNS 策略变更(例如封锁新发现的恶意域名)更快地在所有客户端生效,但缓存时间缩短会增加查询量。

最大 TTL 限制仅适用于允许的查询从上游获取的 DNS 答案。Gateway 生成的响应(封锁、覆盖和安全搜索答案)已有较短的默认 TTL,因此不受影响。

工作原理

Gateway 按层级应用 TTL 设置。最具体的设置优先:

  1. 如果 DNS 位置设置了单独的覆盖值,就使用该值。
  2. 如果位置继承设置,就使用账户级最大 TTL。
  3. 如果所有层级都未配置最大 TTL,上游 TTL 值将原样传递。

最大 TTL 的有效范围为 60 至 36,000 秒(1 分钟至 10 小时)。

配置账户级最大 TTL

账户级设置适用于所有未单独设置覆盖值的 DNS 位置。

  1. 在 Zero Trust ↗ 中,前往 Traffic Policies > Traffic Settings。
  2. 在 Proxy and inspection 下找到 Configure time-to-live for DNS resolution 部分。
  3. 输入以秒为单位的值(60 至 36,000)。
  4. 选择 Save(保存)。
curl --request PUT \
  https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/configuration \
  --header "Authorization: Bearer {api_token}" \
  --header "Content-Type: application/json" \
  --data '{
    "settings": {
      "max_ttl_secs": 3600
    }
  }'

要移除账户级限制(允许上游 TTL 原样传递),请省略 max_ttl_secs 字段或将其设为 null。

配置位置级最大 TTL

每个 DNS 位置都可以覆盖账户级设置。位置级设置支持三种模式:

模式 行为
遵循账户级设置(inherit) 使用账户级配置的值。新建位置默认采用此模式。
不设置最大值(disabled) 即使账户级设置了最大 TTL,也对该位置禁用此限制。上游 TTL 值将原样传递。
自定义(override) 设置覆盖账户级数值的位置专属最大 TTL。需要将 ttl_secs 设为 60 至 36,000 之间的值。
  1. 在 Zero Trust ↗ 中,前往 Traffic controls(流量控制) > Resolver endpoints(解析器端点)。
  2. 选择一个位置,或创建新位置。
  3. 在 DNS Endpoints 选项卡中,找到 Configure time-to-live for this location 部分。
  4. 选择以下选项之一:
    • Respect account-level setting - 继承账户级 TTL 限制。
    • Do not set max value - 对该位置禁用 TTL 限制。
    • Custom - 输入以秒为单位的 TTL 值。
  5. 选择 Save(保存)。
# Inherit account-level setting (default)
curl --request PUT \
  https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/locations/{location_id} \
  --header "Authorization: Bearer {api_token}" \
  --header "Content-Type: application/json" \
  --data '{
    "max_ttl": {
      "mode": "inherit"
    }
  }'
# Disable max TTL for this location
curl --request PUT \
  https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/locations/{location_id} \
  --header "Authorization: Bearer {api_token}" \
  --header "Content-Type: application/json" \
  --data '{
    "max_ttl": {
      "mode": "disabled"
    }
  }'
# Override with a custom value
curl --request PUT \
  https://api.cloudflare.com/client/v4/accounts/{account_id}/gateway/locations/{location_id} \
  --header "Authorization: Bearer {api_token}" \
  --header "Content-Type: application/json" \
  --data '{
    "max_ttl": {
      "mode": "override",
      "ttl_secs": 3600
    }
  }'

DNS 日志字段

启用最大 TTL 后,Gateway DNS 日志中会出现两个额外字段:

字段 描述
upstream_record_ttls 上游 DNS 响应的原始 TTL 值,尚未应用任何限制。
applied_max_ttl Gateway 对响应应用的最大 TTL 值。如果未应用限制,此字段不存在。

这些字段位于仪表板中 DNS 日志列选择器的 DNS Response Details 分组,也会出现在 Logpush 数据集中。

这篇文档对您有帮助吗?