欢迎来到「财富猿」—— 一个专注于技术开发与财富创造的博客!

如何在 Docker + OpenResty 环境下修复 Google Site Kit “无法访问 WordPress REST API” 的问题

111次阅读
没有评论

如何在 Docker + OpenResty 环境下修复 Google Site Kit “无法访问 WordPress REST API” 的问题

在使用 Google Site Kit 连接 Google Analytics 或 Search Console 时,你可能会遇到以下错误:

Site Kit cannot access the WordPress REST API. Please ensure it is enabled on your site.

当你的 WordPress 部署在 Docker 容器中,并且使用 1Panel-openresty(基于 OpenResty/Nginx 的 Web 服务器)作为反向代理时,Google Site Kit 可能会因为容器内部网络问题导致 REST API 访问失败。

本教程将详细介绍 如何修复 Site Kit 无法访问 WordPress REST API 的问题,并适用于所有基于 Docker + Nginx(OpenResty) 反向代理的 WordPress 站点。


🛠 1. 问题分析

在 Docker 容器环境下,Google Site Kit 插件需要访问 WordPress 的 REST API (/wp-json),但由于以下原因可能导致访问失败:

WordPress REST API 其实是正常的,你可以在浏览器访问:

https://你的域名/wp-json

容器内部 访问自身 API 时却失败,原因可能是:

  • 容器尝试访问 HTTPS 端口,但容器内部没有监听 443 端口,导致连接失败。
  • Nginx/OpenResty 反向代理了 HTTPS 端口,WordPress 仅监听 HTTP(80 端口)
  • 容器内部 DNS 解析问题,导致 curl -I https://你的域名/wp-json 无法连接自身域名。

📌 2. 关键步骤

目标:让容器内部能够正确访问 WordPress REST API,从而让 Google Site Kit 正常工作。

🔎 1. 检查 REST API 是否正常

首先,进入 WordPress 容器:

docker exec -it wordpress bash

然后执行:

curl -I http://localhost/wp-json
  • 如果返回 200 OK401 Unauthorized,说明 REST API 正常运行 ✅。
  • 如果返回 curl: (7) Failed to connect,说明容器内部访问有问题,继续执行后续步骤修复。

🔧 2. 修改 wp-config.php 让容器内部访问 HTTP

由于 OpenResty 处理了 HTTPS,容器内部应该使用 HTTP 而非 HTTPS 访问 WordPress。

执行以下命令编辑 wp-config.php

vim wp-config.php

wp-config.php 末尾添加以下内容

if (strpos($_SERVER['REMOTE_ADDR'], '172.') === 0) {
    define('WP_HOME', 'http://localhost');
    define('WP_SITEURL', 'http://localhost');
} else {
    define('WP_HOME', 'https://你的域名');
    define('WP_SITEURL', 'https://你的域名');
}

// 告诉 WordPress 站点是 HTTPS,避免插件误判
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
    $_SERVER['HTTPS'] = 'on';
}

🔹 解释

  • 如果请求来自 Docker 容器内部(172.* 内网 IP 段),则使用 HTTP 访问 localhost,避免 HTTPS 端口问题。
  • 其他情况仍然使用 HTTPS,确保外部访问 WordPress 是安全的。
  • $_SERVER['HTTPS'] = 'on'; 让 WordPress 识别 OpenResty 代理过来的 HTTPS 请求,避免插件因协议问题报错。

保存退出 (:wq)


📡 3. 修改 OpenResty/Nginx 配置,确保代理正确

在 OpenResty (Nginx) 配置中,确保代理请求时传递 X-Forwarded-Proto,这样 WordPress 知道自己在 HTTPS 环境下。

打开 1Panel/OpenResty 站点配置文件,例如:

vim /etc/nginx/sites-enabled/your-site.conf

确保以下内容正确:

server {
    listen 80;
    listen 443 ssl http2;
    server_name 你的域名 www.你的域名;

    ssl_certificate /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8580;  # 这里是你的 WordPress 运行的端口
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;  # 重要,告诉 WordPress 这是 HTTPS 请求
    }

    if ($scheme = http) {
        return 301 https://$host$request_uri;  # 确保外部 HTTP 访问自动跳转到 HTTPS
    }
}

然后重启 OpenResty

systemctl restart openresty

🧪 4. 测试是否修复成功

WordPress 容器内,再次运行:

curl -I http://localhost/wp-json

如果返回:

HTTP/1.1 401 Unauthorized

🎉 恭喜!Google Site Kit 现在可以正常访问 REST API 了!


🎯 3. 最终总结

你可能会遇到 Google Site Kit 无法访问 REST API 的问题,原因大多数是:

  1. WordPress 容器内访问自身 HTTPS 失败(因为 OpenResty/Nginx 代理了 HTTPS)。
  2. WordPress 没有正确识别 HTTPS 代理,导致插件误判 API 无法访问。
  3. 解决方案
    • 修改 wp-config.php 让容器内请求 http://localhost 而不是 https://你的域名
    • 确保 OpenResty/Nginx 配置了 X-Forwarded-Proto,让 WordPress 识别 HTTPS 代理。
    • 容器内测试 curl -I http://localhost/wp-json,返回 200 或 401 即表示成功

📌 4. 参考代码(完整修复方案)

如果你想直接复制粘贴使用,按以下步骤执行:

1️⃣ 进入 WordPress 容器

docker exec -it wordpress bash

2️⃣ 重新创建 sources.list(如果 apt update 过慢)

echo "deb http://mirrors.aliyun.com/debian bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
apt update && apt install vim nano curl -y

3️⃣ 修改 wp-config.php

vim wp-config.php

添加以下代码:

if (strpos($_SERVER['REMOTE_ADDR'], '172.') === 0) {
    define('WP_HOME', 'http://localhost');
    define('WP_SITEURL', 'http://localhost');
} else {
    define('WP_HOME', 'https://你的域名');
    define('WP_SITEURL', 'https://你的域名');
}

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
    $_SERVER['HTTPS'] = 'on';
}

4️⃣ 重启 OpenResty

systemctl restart openresty

5️⃣ 测试

curl -I http://localhost/wp-json

返回 401 Unauthorized 即修复成功!


🎉 至此,Google Site Kit 终于可以正常连接你的 WordPress 站点了! 🎉

正文完
 0
评论(没有评论)