文档故障排查网络超时与代理配置

网络超时与代理配置

企业网络环境中 Claude Code 连接超时和代理配置问题的完整解决方案。

问题现象

常见错误信息

# 连接超时
Error: connect ETIMEDOUT 35.171.144.152:443
Error: Request timeout after 30000ms
 
# 代理相关错误
Error: Proxy connection failed
Error: ECONNREFUSED 127.0.0.1:8080
Error: tunnel socket could not be established
 
# DNS 解析失败
Error: getaddrinfo ENOTFOUND api.anthropic.com
Error: getaddrinfo ENOTFOUND proxy.company.com

症状表现

  • Claude Code 启动后无响应或长时间等待
  • 出现”正在连接…”后超时失败
  • 间歇性连接成功,但经常中断
  • 某些功能正常,MCP服务器连接失败

快速自检

1. 基础网络检测

# 检查互联网连接
ping google.com
ping api.anthropic.com
 
# 检查DNS解析
nslookup api.anthropic.com
dig api.anthropic.com
 
# 检查端口连通性
telnet api.anthropic.com 443
nc -zv api.anthropic.com 443

2. 代理环境检测

# 查看代理环境变量
echo $HTTP_PROXY
echo $HTTPS_PROXY
echo $NO_PROXY
env | grep -i proxy
 
# 检查系统代理设置
# macOS
scutil --proxy
 
# Windows
netsh winhttp show proxy
 
# Linux
cat /etc/environment | grep -i proxy

3. Claude Code 连接测试

# 详细模式启动
claude --verbose
 
# 测试API连接
curl -v https://api.anthropic.com/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: your-api-key"

解决方案

企业代理配置

方案一:环境变量配置(推荐)

# 设置代理环境变量
export HTTP_PROXY=http://proxy.company.com:8080
export HTTPS_PROXY=https://proxy.company.com:8080
export NO_PROXY=localhost,127.0.0.1,*.company.com
 
# 如果代理需要认证
export HTTP_PROXY=http://username:password@proxy.company.com:8080
export HTTPS_PROXY=https://username:password@proxy.company.com:8080
 
# 持久化配置(添加到shell配置文件)
echo 'export HTTPS_PROXY=http://proxy.company.com:8080' >> ~/.bashrc
echo 'export HTTPS_PROXY=http://proxy.company.com:8080' >> ~/.zshrc

方案二:npm 代理配置

# 配置npm代理(如果通过npm安装Claude Code)
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy https://proxy.company.com:8080
npm config set registry https://registry.npmjs.org/
 
# 查看npm配置
npm config list

方案三:系统级代理配置

macOS 系统代理设置:

# 通过网络偏好设置
系统偏好设置 > 网络 > 高级 > 代理
 
# 或使用命令行
networksetup -setwebproxy "Wi-Fi" proxy.company.com 8080
networksetup -setsecurewebproxy "Wi-Fi" proxy.company.com 8080

Windows 系统代理设置:

# 设置系统代理
netsh winhttp set proxy proxy.company.com:8080
 
# 或通过注册表
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d "proxy.company.com:8080" /f

连接超时优化

增加超时时间

# 通过环境变量设置
export CLAUDE_TIMEOUT=60000  # 60秒
export REQUEST_TIMEOUT=45000 # 45秒
 
# 在Claude Code启动时设置
claude --timeout 60000

网络优化配置

# 优化TCP连接参数(Linux)
echo 'net.ipv4.tcp_keepalive_time = 600' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_keepalive_intvl = 60' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_keepalive_probes = 3' >> /etc/sysctl.conf
 
# 刷新配置
sudo sysctl -p

SSL/TLS 问题解决

证书验证问题

# 临时跳过SSL验证(不推荐生产环境)
export NODE_TLS_REJECT_UNAUTHORIZED=0
 
# 添加企业根证书
export NODE_EXTRA_CA_CERTS=/path/to/corporate-root-ca.crt
 
# 使用系统证书库
export SSL_CERT_DIR=/etc/ssl/certs
export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

企业防火墙证书

# 导出企业证书
# 从浏览器中导出.crt文件
 
# 添加到系统证书库
# macOS
sudo security add-trusted-cert -d root -r trustRoot -k /Library/Keychains/System.keychain corporate-cert.crt
 
# Ubuntu/Debian
sudo cp corporate-cert.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
 
# CentOS/RHEL
sudo cp corporate-cert.crt /etc/pki/ca-trust/source/anchors/
sudo update-ca-trust
⚠️

⚠️ 安全提示:跳过SSL验证会降低安全性,只应在开发环境或临时排查时使用。

高级故障排查

网络抓包分析

# 使用tcpdump抓包
sudo tcpdump -i any -w claude-network.pcap host api.anthropic.com
 
# 使用Wireshark分析
# 查看TCP握手、SSL握手和HTTP请求
 
# curl详细调试
curl -v --trace-ascii trace.txt https://api.anthropic.com/v1/messages

代理链路测试

# 测试代理连通性
curl -v --proxy http://proxy.company.com:8080 https://api.anthropic.com
 
# 测试代理认证
curl -v --proxy-user username:password \
  --proxy http://proxy.company.com:8080 \
  https://api.anthropic.com
 
# 测试SOCKS代理
curl --socks5 proxy.company.com:1080 https://api.anthropic.com

DNS 问题排查

# 更换DNS服务器
# 临时更改
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 1.1.1.1" >> /etc/resolv.conf
 
# 永久配置(Ubuntu)
sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
sudo rm /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
 
# 测试域名解析
dig @8.8.8.8 api.anthropic.com
nslookup api.anthropic.com 1.1.1.1

配置模板

企业环境配置脚本

#!/bin/bash
# claude-proxy-setup.sh
 
# 企业代理配置
export HTTP_PROXY="http://proxy.company.com:8080"
export HTTPS_PROXY="http://proxy.company.com:8080"
export NO_PROXY="localhost,127.0.0.1,*.company.com,*.local"
 
# 超时配置
export CLAUDE_TIMEOUT=60000
export REQUEST_TIMEOUT=45000
 
# SSL配置
export NODE_EXTRA_CA_CERTS="/etc/ssl/certs/corporate-root-ca.crt"
 
# 启动Claude Code
echo "启动Claude Code with proxy configuration..."
claude --verbose

Windows 批处理脚本

@echo off
REM claude-proxy-setup.bat
 
REM 设置代理
set HTTP_PROXY=http://proxy.company.com:8080
set HTTPS_PROXY=http://proxy.company.com:8080
set NO_PROXY=localhost,127.0.0.1,*.company.com
 
REM 设置超时
set CLAUDE_TIMEOUT=60000
 
REM 启动Claude Code
echo Starting Claude Code with proxy...
claude --verbose
 
pause

验证配置

配置验证清单

# 1. 代理连通性测试
curl --proxy $HTTP_PROXY https://httpbin.org/ip
 
# 2. API连接测试
curl --proxy $HTTPS_PROXY https://api.anthropic.com
 
# 3. Claude Code连接测试
claude --version
 
# 4. 完整功能测试
claude -p "Hello, test connection"

性能测试

# 网络延迟测试
ping -c 10 api.anthropic.com
 
# 带宽测试
curl -w "@curl-format.txt" -o /dev/null -s https://api.anthropic.com
 
# 创建curl-format.txt
echo "     time_namelookup:  %{time_namelookup}\n     time_connect:     %{time_connect}\n     time_appconnect:  %{time_appconnect}\n     time_pretransfer: %{time_pretransfer}\n     time_redirect:    %{time_redirect}\n     time_starttransfer: %{time_starttransfer}\n     time_total:       %{time_total}\n" > curl-format.txt

预防措施

监控脚本

#!/bin/bash
# network-monitor.sh
 
while true; do
    if ! curl -s --max-time 5 https://api.anthropic.com > /dev/null; then
        echo "$(date): Connection failed, checking proxy..."
        # 重新设置代理配置
        source /path/to/proxy-config.sh
    else
        echo "$(date): Connection OK"
    fi
    sleep 300  # 5分钟检查一次
done

自动切换配置

# 网络环境自动检测
if curl -s --max-time 5 https://api.anthropic.com > /dev/null; then
    echo "Direct connection available"
    unset HTTP_PROXY HTTPS_PROXY
else
    echo "Using proxy configuration"
    export HTTP_PROXY="http://proxy.company.com:8080"
    export HTTPS_PROXY="http://proxy.company.com:8080"
fi

相关卡片

参考资源

关于我