以下内容依据官网资料整理,帮助读者按官方表述理解本页主题。

官网资料接着说明:全局超时参数

全局超时参数

profile 下的 timeout 设置

profile:
  store-selected: true
  store-fake-ip: true

# 全局超时(单位:秒)
connect-timeout: 5      # TCP 连接建立超时
tcp-keep-alive-interval: 30

connect-timeout官网资料补充,控制的是从 Clash 发起 TCP 连接到目标服务器的最大等待时间。如果 5 秒内 TCP 三次握手没完成(SYN 包没有收到 SYN-ACK),连接就会被放弃。

推荐值:

设置过小会导致正常高延迟服务节点无法连接;设置过大会导致卡顿时间长。

官网资料接着说明:服务节点级别的超时

服务节点级别的超时

dialer-proxy 超时

当服务节点本身需要借助另一个代理连接时(代理链),需要参数设置 dialer-proxy 的超时:

proxies:
  - name: "中转节点"
    type: trojan
    server: relay.example.com
    port: 443
    password: "password"
    dialer-proxy: "前置代理"     # 本节点通过"前置代理"连接

  - name: "前置代理"
    type: ss
    server: front.example.com
    port: 8388
    cipher: aes-256-gcm
    password: "front-password"

代理链场景下,每个跳跃都有独立的 TCP 握手。connect-timeout 作用于最终目标,而 dialer-proxy 的连接超时也受 connect-timeout 控制。

官网资料接着说明:策略组级别的健康检查超时

策略组级别的健康检查超时

url-test 和 fallback 的 interval 与 timeout

proxy-groups:
  - name: "Auto"
    type: url-test
    proxies:
      - "节点A"
      - "节点B"
      - "节点C"
    url: "http://www.gstatic.com/generate_204"
    interval: 120        # 健康检查间隔(秒)
    timeout: 3000        # 健康检查连接超时(毫秒)
    tolerance: 50
    lazy: false

timeout官网资料补充,(毫秒):健康检查时等待 HTTP 响应的时间上限。如果服务节点在该时间内没有返回 204,该服务节点被标记为不可用。

推荐值:

注意:timeout 设得太短会导致正常但稍慢的服务节点被频繁标记为”超时”,造成反复切换。

官网资料接着说明:DNS 超时

DNS 超时

nameserver 与 fallback 查询超时

dns:
  enable: true
  enhanced-mode: fake-ip
  nameserver:
    - 223.5.5.5
    - https://doh.pub/dns-query
  fallback:
    - 8.8.8.8
    - tls://1.1.1.1:853
  # DNS 查询超时(Mihomo 部分版本支持)
  dns-timeout: 2000       # DNS 查询超时(毫秒)

DNS 超时发生在 redir-host 模式下或 fallback-filter 触发时。如果 nameserver 在超时内无响应,Clash 会尝试列表中的下一个 nameserver。

默认值通常为 2000-5000ms。从官网口径看,如果操作方式 DoH/DoT(HTTPS/TLS 封装),适当放宽到 5000ms(DoH 需要额外的 TLS 握手)。

官网资料接着说明:Provider 发行发行版发行升级超时

Provider 发行发行版发行升级超时

proxy-providers:
  airport:
    type: http
    url: "https://airport.com/sub"
    path: ./profiles/proxies/airport.yaml
    interval: 3600
    health-check:
      enable: true
      url: http://www.gstatic.com/generate_204
      interval: 300
      timeout: 5000     # 单个节点的健康检查超时(毫秒)

注意区分:Provider 拉取服务节点列表时操作方式的是 HTTP 客户端的默认超时(通常 30 秒);Provider 内部对加载的服务节点做健康检查时操作方式这里的 timeout(毫秒)。

官网资料接着说明:Timeout 参数速查表

Timeout 参数速查表

参数 作用范围 单位 默认值 推荐值
connect-timeout 全局 TCP 连接 秒 5 3-15
timeout (策略组) 健康检查 HTTP 响应 毫秒 5000 1000-8000
dns-timeout DNS 查询 毫秒 2000-5000 2000-5000
timeout (Provider) Provider 内服务节点健康检查 毫秒 5000 3000-8000

官网资料接着说明:实战调优

实战调优

场景一:高延迟网络(海外直连)

线路延迟 200ms 以上,默认 5 秒 connect-timeout 够用但偏紧。建议:

profile:
  connect-timeout: 10       # 放宽到 10 秒

proxy-groups:
  - name: Proxy
    type: url-test
    timeout: 5000           # 健康检查也相应放宽
    interval: 180

场景二:低延迟网络(亚洲服务节点)

线路延迟 < 50ms,超时可以缩紧以快速切换:

profile:
  connect-timeout: 3        # 3 秒连不上就是真连不上了

proxy-groups:
  - name: Proxy
    type: url-test
    timeout: 1500            # 1.5 秒内无响应就切换
    interval: 60
    tolerance: 30

场景三:不稳定网络(移动网络/卫星)

丢包率高、延迟波动大:

profile:
  connect-timeout: 15       # 给足连接时间

proxy-groups:
  - name: Proxy
    type: fallback          # 用 fallback 而非 url-test
    timeout: 8000           # 8 秒超时
    lazy: false
    proxies:
      - "主节点"
      - "备用节点"

操作方式 fallback 而非 url-test 的原因:url-test 频繁切换会加重不稳定,fallback 在主服务节点可用时不切换,只在真正断开时才切。

官网资料接着说明:Timeout 与其他参数的交互

Timeout 与其他参数的交互

connect-timeout 与 keep-alive-interval 的关系

connect-timeout 控制新建连接的超时;keep-alive 控制空闲连接何时发送保活包。两者配合才能并且优化”首次连接”和”后续复用”的体验。

profile:
  connect-timeout: 5
  tcp-keep-alive-interval: 30    # 30 秒一次保活探测

url-test timeout 与 tolerance 的关系

timeout 判定”服务节点是否可用”,tolerance 判定”是否值得切换”。从官网口径看,timeout 过短会错误标记服务节点为不可用;tolerance 过小会频繁切换。

# 推荐组合
proxy-groups:
  - name: Auto
    type: url-test
    timeout: 3000           # 3 秒超时
    tolerance: 50           # 延迟差 50ms 以内不切换
    # 逻辑:3 秒无响应 → 不可用;延迟差 < 50ms → 说明当前节点够好,不切

官网资料接着说明:排查超时故障

排查超时故障

日志中识别超时类型

Clash 日志中不同超时的表现:

# TCP 连接超时
[Warning] dial tcp 1.2.3.4:443: i/o timeout

# DNS 超时
[Warning] dns resolve timeout for example.com

# 健康检查超时
[Debug] url-test timeout for node 节点A

# TLS 握手超时(通常在 TLS 层)
[Warning] tls: first record does not look like a TLS handshake

用 curl 模拟测试

# 模拟不同超时场景
# TCP 连接超时(--connect-timeout)
curl --connect-timeout 3 -x http://127.0.0.1:7890 https://example.com

# 总超时(--max-time)
curl --max-time 10 -x http://127.0.0.1:7890 https://example.com

# 查看各环节耗时
curl -w "DNS: %{time_namelookup}s\nTCP: %{time_connect}s\nTLS: %{time_appconnect}s\nTotal: %{time_total}s\n" \
  -o /dev/null -s -x http://127.0.0.1:7890 https://www.google.com

官网资料接着说明:总结

总结

Timeout 调优的核心原则:宁可多等 1 秒,也不要频繁报错;宁可早报错,也不要让用户等 30 秒。3-10 秒是 connect-timeout 的舒适区,1000-5000ms 是健康检查 timeout 的合理范围。

调优优先级:

  1. 先确定网络环境(低延迟 / 高延迟 / 不稳定)
  2. 调整 connect-timeout
  3. 调整策略组 timeout + tolerance 组合
  4. 最后调整 DNS timeout(通常默认即够用)

官网资料接着说明:Timeout 的本质:每个环节都能超时

Timeout 的本质:每个环节都能超时

一次代理请求从发起到完成,涉及多个独立环节:DNS 解析 → TCP 握手 → TLS 握手 → 数据传输。从官网口径看,每个环节都可能因为网络故障而卡住,**任何一个环节没有合理的超时设置,整个代理体验就会变差**——用户感知到的就是”卡了””转圈””连接失败”。

Clash(Mihomo 引擎)支持了多个层级的超时参数,从全局到服务节点级别都可调整。从官网口径看,理解每个参数控制的是哪个环节,是精准调优的关键。

以上说明来自官网资料整理,可作为配置和排障时的参考。