Connection refused 与“认证失败”不是一类问题。它表示 TCP 连接到目标主机和端口时,没有服务接受连接,或网络设备主动拒绝。先确认 Magento 实际连接的 host/port,再从运行 PHP 的同一网络环境测试,不能只在 OpenSearch 服务器本机 curl。

读取 Magento 当前有效配置

php bin/magento config:show catalog/search/engine
php bin/magento config:show catalog/search/opensearch_server_hostname
php bin/magento config:show catalog/search/opensearch_server_port
php bin/magento config:show catalog/search/opensearch_enable_auth

配置可能被 app/etc/env.php 或环境变量锁定。后台显示值与 CLI 不一致时,以当前运行实例读取到的值为准。

在 PHP 容器或 Web 节点内测试网络

getent hosts opensearch
nc -vz opensearch 9200
curl -v --connect-timeout 3 http://opensearch:9200/

getent 失败是 DNS/容器服务名问题;DNS 正常但 nc refused,查目标监听;连接建立后返回 401/403,网络已经通了,转去查认证。不要把 401 当作端口问题。

OpenSearch 服务器实际监听在哪里

systemctl status opensearch --no-pager
journalctl -u opensearch -n 120 --no-pager
ss -lntp | grep 9200
curl -sS http://127.0.0.1:9200/

只监听 127.0.0.1 时,远程 Magento 无法连接。修改 network.host 前先确认防火墙、TLS 和访问控制,不能为了连接成功直接暴露 0.0.0.0:9200 到公网。

容器与主机名的常见误区

在 Docker/Kubernetes 中,localhost 指当前 PHP 容器,不是 OpenSearch 容器。Magento 应使用 service name 和容器端口;宿主机映射端口只供宿主机或外部访问。检查:

docker ps --format 'table {{.Names}}\t{{.Ports}}'
docker network inspect app-network
kubectl get svc,endpoints -n commerce

TLS 和协议不匹配

若服务只接受 HTTPS,而 Magento 配成 HTTP,常见表现可能是连接重置或协议错误。分别测试:

curl -vk https://opensearch:9200/
openssl s_client -connect opensearch:9200 -servername opensearch </dev/null

证书验证失败应安装正确 CA 或修证书 SAN,不要长期使用跳过验证。账号密码也不要出现在命令历史中。

连接恢复后的应用验证

php bin/magento indexer:reindex catalogsearch_fulltext
php bin/magento indexer:status catalogsearch_fulltext
curl -s 'http://opensearch:9200/_cluster/health?pretty'

集群至少要能正常分配目标索引,前台搜索常用词能返回商品。然后重启一台 PHP 节点或消费者确认配置仍有效,避免只修好了当前 shell 所在容器。