Composer 默认不信任自托管 GitLab 域名,需在 composer.json 的 config 下显式配置 gitlab-domains 数组(仅 host 和可选 port),并清除缓存;若仍报 SSL 错误,须配置 COMPOSER_CAFILE 或更新系统 CA 证书。

Composer 默认不信任自托管 GitLab 域名,直接 composer install 会报 Could not fetch https://gitlab.example.com/... 或 SSL certificate problem —— 根本原因不是证书本身,而是 Composer 的域名白名单机制在起作用。
为什么 gitlab-domains 配置必须显式声明?
Composer 从 2.2 开始默认启用安全策略:只允许访问官方 Packagist、GitHub、GitLab.com 等预设域名。自托管 GitLab(如 gitlab.example.com)不在其中,即使 HTTPS 可用、证书有效,也会被拦截。
这不是 SSL 错误,也不是网络问题,是 Composer 主动拒绝发起请求。
- 不配
gitlab-domains→ 请求根本不会发出,curl或浏览器能打开也不管用 - 配错格式(比如漏了协议、多加斜杠)→ 配置无效,行为不变
- 该配置只影响 GitLab API 调用(如获取 package info、下载 dist),不影响
git clone行为
怎么在 composer.json 里正确添加 gitlab-domains?
必须写在根级 config 对象下,且值是字符串数组。路径、协议、端口都要精确匹配你实际使用的 GitLab 地址。
例如你用的是 https://gitlab.example.com,就填 "gitlab.example.com";如果用了非标端口如 https://gitlab.example.com:8443,就得写 "gitlab.example.com:8443"。
{
"config": {
"gitlab-domains": ["gitlab.example.com", "gitlab.internal.company"]
}
}
- 不要写
https://前缀 ——gitlab-domains只认 host(和可选 port) - 多个域名用逗号分隔,每个都是独立字符串
- 修改后需运行
composer update --lock或至少composer clear-cache才生效
遇到 SSL certificate problem 还要额外处理吗?
加了 gitlab-domains 后仍报 SSL 错误,说明证书链不完整或系统 CA 不信任。这时不能关 SSL 验证(secure-http: false 已废弃且危险),而应让 Composer 信任你的 CA。
- 把 GitLab 使用的根证书(PEM 格式)追加到系统 CA 包,或
- 设置环境变量
COMPOSER_CAFILE=/path/to/your-ca.crt - 验证方式:
openssl s_client -connect gitlab.example.com:443 -servername gitlab.example.com看是否提示Verify return code: 0 (ok)
真正卡住人的地方往往不是“要不要配”,而是配了却没生效——要么域名写错格式,要么缓存没清,要么以为加了域名就自动解决证书问题。三者缺一不可。


















