Composer连接失败主因是代理配置错误或不可用,需检查环境变量、单独配置代理、临时禁用代理,并处理no_proxy及证书问题。
Composer连接失败常因系统或终端设置了代理,而该代理不可用、配置错误或未被Composer识别。解决核心是确认代理状态、正确配置或临时禁用代理。
在终端运行以下命令,查看是否有代理环境变量被设置:
env | grep -i proxy,关注 http_proxy、https_proxy、no_proxy
set proxy;PowerShell 中用 Get-ChildItem Env: | Where-Object Name -like "*proxy*"
若输出中出现类似 http_proxy=http://127.0.0.1:8080 的内容,说明代理已启用——需确认该地址是否真实可用(如本地代理软件是否正在运行)。
Composer支持独立的代理设置,优先级高于系统环境变量,更安全可控:
composer config -g http-proxy http://user:pass@proxy-host:portcomposer config -g https-proxy http://user:pass@proxy-host:port
user:pass@;若使用SOCKS5,写成 socks5://host:port
composer config -g --unset http-proxycomposer config -g --unset https-proxy
不修改任何配置,仅本次生效,适合调试或偶尔使用:
http_proxy= https_proxy= composer install
set http_proxy=& set https_proxy=& composer install
$env:http_proxy=""; $env:https_proxy=""; composer install
即使代理可用,也可能因忽略内网或自签名域名导致失败:
no_proxy(逗号分隔,不带协议):export no_proxy="packagist.org,github.com,api.github.com"
composer config -g secure-http false
composer config -g cafile /path/to
/cacert.pem