目录
- time_redirect 是什么
- 为什么重定向耗时要单独监控
- 实战1:基础用法——输出重定向耗时
- 实战2:配合 -L 追踪多跳耗时
- 实战3:写入文件做性能日志
- 实战4:批量检测多个域名的跳转耗时
- 实战5:结合其他时间变量做完整分析
- 常见问题排查
time_redirect 是什么
curl -w 的 time_redirect 变量(单位是秒,浮点数)记录的是:从发起请求到完成所有重定向所消耗的时间,不包括最后一次请求的数据传输时间。
换句话说,它告诉你"跳转本身花了多少时间",而不是整个请求的总时间。
curl -s -o /dev/null -w "time_redirect: %{time_redirect}s\n" https://example.com
为什么重定向耗时要单独监控
一个 HTTP 请求的总耗时 = DNS解析 + 连接建立 + TLS握手 + 重定向 + 数据传输
重定向往往是性能瓶颈的隐藏点:
- 301/302 跳转链太长(A→B→C→D)
- 某跳的服务器响应慢
- CDN 回源导致额外跳转
- HSTS 预加载引发的额外跳转
只盯着 time_total 看,你永远不知道时间耗在哪一步。
实战1:基础用法——输出重定向耗时
curl -s -o /dev/null \
-w "DNS: %{time_namelookup}s | 重定向: %{time_redirect}s | 总耗时: %{time_total}s\n" \
https://www.youres.cn
输出示例:
DNS: 0.012s | 重定向: 0.085s | 总耗时: 0.312s
如果 time_redirect 接近 time_total,说明绝大部分时间都耗在跳转上,重点排查重定向链。
实战2:配合 -L 追踪多跳耗时
curl -s -o /dev/null -L \
-w "跳转次数: %{num_redirects} | 重定向耗时: %{time_redirect}s | 最终URL: %{url_effective}\n" \
https://t.co/xxxxxx
短链分析场景特别有用:跳转次数多不多、每跳花了多少时间,一目了然。
实战3:写入文件做性能日志
curl -s -o /dev/null -L \
-w "%{url_effective},%{num_redirects},%{time_redirect},%{time_total}\n" \
https://example.com >> redirect_perf.csv
配合 cron 定时执行,可以建立重定向性能的历史趋势,发现"某天开始变慢"的问题。
实战4:批量检测多个域名的跳转耗时
#!/bin/bash
urls=(
"https://www.youres.cn"
"https://baidu.com"
"https://qq.com"
)
for url in ""; do
result=
echo ""
done
输出 CSV 格式,直接导入 Excel 做分析。
实战5:结合其他时间变量做完整分析
| 变量 | 含义 |
|---|---|
| time_namelookup | DNS 解析耗时 |
| time_connect | TCP 连接耗时 |
| time_appconnect | TLS 握手耗时 |
| time_redirect | 重定向总耗时 |
| time_starttransfer | 首字节耗时 |
| time_total | 总耗时 |
curl -s -o /dev/null -L \
-w "DNS: %{time_namelookup} | TCP: %{time_connect} | TLS: %{time_appconnect} | 重定向: %{time_redirect} | 首字节: %{time_starttransfer} | 总耗时: %{time_total}\n" \
https://example.com
常见问题排查
Q:time_redirect 输出始终是 0?
A:目标 URL 没有发生重定向。用 -L 参数让 curl 跟随跳转,或者确认目标确实返回 301/302。
Q:time_redirect 很大,但 num_redirects 只有 1?
A:那一跳的服务器响应慢,不是跳转链的问题。重点看那一跳的服务器性能或网络延迟。
Q:Windows PowerShell 怎么用?
A:PowerShell 的 Invoke-WebRequest 不直接支持 time_redirect,建议用 WSL 或 Git Bash 里的 curl。
相关文章:
curl -w格式化输出重定向信息:5个实战技巧让你精准诊断跳转链路
curl -L参数跟随重定向详解:3个实战场景让你彻底搞懂这个常用参数
curl -w num_redirects检测重定向次数:精准诊断跳转链路的实战指南
版权声明
本文仅代表个人观点。
本文系AI辅助作者原创,未经许可,转载请保留原文链接。

发表评论