页边剪报

curl 分阶段耗时模板与 --resolve 免改 hosts 验证证书

· 来源备忘:curl(1) 手册页与 Cloudflare 博客排障示例 #curl#networking

接口偶发变慢时,光看总耗时分不清是卡在 DNS 解析、TCP 握手、TLS 协商还是后端首字节处理(TTFB)。在机器上放一小段 curl -w 格式串,一眼就能定位在哪一环:

curl -sS -o /dev/null -w '\
    namelookup:  %{time_namelookup}s\n\
       connect:  %{time_connect}s\n\
    appconnect:  %{time_appconnect}s\n\
  starttransfer: %{time_starttransfer}s\n\
         total:  %{time_total}s (HTTP %{http_code}, v%{http_version})\n' \
  https://example.org/healthz

另外切 DNS 之前想测某台新机上的证书和虚拟主机配对不对,千万别直接 curl -H "Host: a.com" https://1.2.3.4/——那样 TLS 握手里的 SNI 还是空的,拿不到对的证书。正确姿势是用 --resolve:

curl -v --resolve a.example.org:443:198.51.100.12 https://a.example.org/