跳转到内容

WordPress 如何修复「cURL error 28 Connection timed out after 10007 milliseconds」错误

Published: at 01:13

最近网站突然提示以下错误:

在测试 REST API 时,发生了一个错误:

REST API 端点:https://www.sample.cn/wp-json/wp/v2/types/post?context=edit

REST API 响应:(http_request_failed) cURL error 28: Connection timed out after 10007 milliseconds

以下为ChatGPT给出的解决方案:

错误含义详解:

这个错误大多发生在 WordPress 本地回环请求(loopback)失败 时,也就是 WordPress 尝试通过 curl 或 wp_remote_get() 访问自己的 REST API 端点(例如用于 Gutenberg、Site Health、Jetpack、Heartbeat API 等),但连接被阻断或延迟过长。

这个错误说明你在访问 WordPress 的 REST API 时,服务器尝试发起一个 HTTP 请求(可能是自己请求自己),结果发生了 cURL 超时错误:

cURL error 28: Connection timed out after 10007 milliseconds

🔍 错误含义详解:

这个错误大多发生在 WordPress 本地回环请求(loopback)失败 时,也就是 WordPress 尝试通过 curl 或 wp_remote_get() 访问自己的 REST API 端点(例如用于 Gutenberg、Site Health、Jetpack、Heartbeat API 等),但连接被阻断或延迟过长。

🧪 问题判断

在服务器终端中运行下列命令:

curl -ILk https://127.0.0.1/wp-json/wp/v2/types/post?context=edit
curl -ILk https://www.sample.cn/wp-json/wp/v2/types/post?context=edit

测试结果

本地请求 127.0.0.1 能正常访问 REST API(返回 401 是正常行为)

本地 curl 无法访问自己的域名

✅ 问题核心:

当 WordPress 发起 https://www.sample.cn/wp-json/… 这样的请求时,其实就是:

服务器自己通过 Cloudflare CDN 去访问自己。

但 Cloudflare 会对这种访问行为进行安全拦截(比如:UA 检查、Bot 防护、WAF、Rate Limit),或请求根本不会路由回源服务器(造成超时)。


🎯 解决方案

修改 /etc/hosts 文件让域名解析为 127.0.0.1(最简单且推荐)

将请求绕过 Cloudflare,直接在本机解析为 localhost,解决 99% 问题。

sudo nano /etc/hosts

添加:

127.0.0.1 www.sample.cn

保存后再运行测试:

curl -ILk https://www.sample.cn/wp-json/wp/v2/types/post?context=edit

如果成功(返回 401 或 200),问题就解决了。