Network Issues and Mirror Setup - langningchen/miniapp GitHub Wiki
网络问题和换源指南
本指南专门解决国内网络环境下的各种网络问题,提供详细的换源配置方案,确保开发过程顺畅。
🌐 网络问题概览
国内开发者常遇到的网络问题:
- 🐌 下载缓慢 - 国外源访问速度慢
- ❌ 连接超时 - 网络连接不稳定
- 🔒 访问受限 - 某些域名无法访问
- 📦 包管理器慢 - npm、apt 等下载慢
- 🛠️ 工具链下载失败 - 编译工具获取困难
📋 问题诊断
网络连通性测试
# 测试基本网络连接
ping -c 4 8.8.8.8
# 测试 DNS 解析
nslookup google.com
nslookup github.com
# 测试 HTTP 连接
curl -I https://www.google.com
curl -I https://github.com
# 检查网络速度
curl -o /dev/null -s -w "下载速度: %{speed_download} bytes/sec\n" https://www.baidu.com
代理环境检测
# 检查环境变量
echo $http_proxy
echo $https_proxy
echo $all_proxy
# 检查系统代理设置
env | grep -i proxy
# 测试代理连接
curl --proxy socks5://127.0.0.1:1080 -I https://www.google.com
🔧 系统级换源配置
Ubuntu APT 换源
备份原始源列表:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
sudo cp -r /etc/apt/sources.list.d /etc/apt/sources.list.d.backup
自动换源脚本:
#!/bin/bash
# 自动选择最快的 APT 源
echo "正在测试各大镜像源速度..."
# 镜像源列表
declare -A MIRRORS=(
["阿里云"]="mirrors.aliyun.com"
["清华大学"]="mirrors.tuna.tsinghua.edu.cn"
["中科大"]="mirrors.ustc.edu.cn"
["华为云"]="mirrors.huaweicloud.com"
["网易"]="mirrors.163.com"
)
FASTEST_MIRROR=""
FASTEST_TIME=999
for name in "${!MIRRORS[@]}"; do
url="${MIRRORS[$name]}"
echo "测试 $name ($url)..."
time_taken=$(curl -o /dev/null -s -w '%{time_total}' "https://$url" 2>/dev/null || echo "999")
if (( $(echo "$time_taken < $FASTEST_TIME" | bc -l) )); then
FASTEST_TIME=$time_taken
FASTEST_MIRROR=$url
FASTEST_NAME=$name
fi
echo "响应时间: ${time_taken}s"
done
if [ -n "$FASTEST_MIRROR" ]; then
echo "最快的镜像源: $FASTEST_NAME ($FASTEST_MIRROR)"
# 应用最快的镜像源
sudo sed -i.bak "s|http://archive.ubuntu.com/ubuntu/|https://$FASTEST_MIRROR/ubuntu/|g" /etc/apt/sources.list
sudo sed -i "s|http://security.ubuntu.com/ubuntu/|https://$FASTEST_MIRROR/ubuntu/|g" /etc/apt/sources.list
echo "APT 源已更新为 $FASTEST_NAME"
sudo apt update
else
echo "所有镜像源都无法访问,请检查网络连接"
fi
手动配置推荐源:
# 阿里云源(推荐)
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
EOF
# 更新源
sudo apt update
📦 开发工具换源
NodeJS/NPM 换源
npm 换源配置:
# 查看当前源
npm config get registry
# 测试各大源的速度
npm_sources=(
"https://registry.npmjs.org/"
"https://registry.npmmirror.com/"
"https://mirrors.huaweicloud.com/repository/npm/"
"https://mirrors.cloud.tencent.com/npm/"
)
echo "测试 npm 源速度..."
for source in "${npm_sources[@]}"; do
echo "测试: $source"
time=$(curl -o /dev/null -s -w '%{time_total}' "$source" 2>/dev/null || echo "超时")
echo "响应时间: ${time}s"
echo "---"
done
# 设置为淘宝源(推荐)
npm config set registry https://registry.npmmirror.com
# 验证设置
npm config get registry
pnpm 换源配置:
# 设置 pnpm 镜像源
pnpm config set registry https://registry.npmmirror.com
# 设置其他镜像配置
pnpm config set shamefully-hoist true
pnpm config set strict-peer-dependencies false
# 验证配置
pnpm config list
使用 nrm 管理源:
# 安装 nrm
npm install -g nrm
# 查看可用源
nrm ls
# 测试所有源速度
nrm test
# 切换到最快的源
nrm use taobao
Git 换源配置
GitHub 换源:
# 设置 Git 代理(如果有)
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 或使用 SSH
git config --global url."[email protected]:".insteadOf "https://github.com/"
# 查看配置
git config --global --list | grep -E "(proxy|url)"
使用 GitHub 镜像:
# 配置 GitHub 镜像
git config --global url."https://ghproxy.com/https://github.com/".insteadOf "https://github.com/"
# 或使用 FastGit
git config --global url."https://hub.fastgit.xyz/".insteadOf "https://github.com/"
# 克隆时使用镜像
git clone https://ghproxy.com/https://github.com/user/repo.git
Python/pip 换源
# 创建 pip 配置目录
mkdir -p ~/.pip
# 配置 pip 镜像源
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
[install]
trusted-host = mirrors.aliyun.com
EOF
# 验证配置
pip config list
🛠️ 项目特定换源
编译工具链换源
创建工具链下载脚本 download_toolchain.sh
:
#!/bin/bash
# 工具链下载脚本,支持多个镜像源
TOOLCHAIN_NAME="armv7-eabihf--glibc--stable-2018.11-1"
TOOLCHAIN_FILE="${TOOLCHAIN_NAME}.tar.bz2"
# 镜像源列表
MIRRORS=(
"https://toolchains.bootlin.com/downloads/releases/toolchains/armv7-eabihf/tarballs/"
"https://mirrors.tuna.tsinghua.edu.cn/toolchains/bootlin/releases/toolchains/armv7-eabihf/tarballs/"
"https://mirror.iscas.ac.cn/toolchains/bootlin/releases/toolchains/armv7-eabihf/tarballs/"
)
cd jsapi/toolchains
for mirror in "${MIRRORS[@]}"; do
echo "尝试从 $mirror 下载..."
if wget --timeout=30 --tries=2 "${mirror}${TOOLCHAIN_FILE}"; then
echo "✅ 下载成功"
break
else
echo "❌ 下载失败,尝试下一个镜像源..."
fi
done
if [ -f "$TOOLCHAIN_FILE" ]; then
echo "解压工具链..."
tar -xjf "$TOOLCHAIN_FILE"
echo "✅ 工具链安装完成"
else
echo "❌ 所有镜像源下载失败"
exit 1
fi
依赖库换源
#!/bin/bash
# 依赖库下载脚本
# 创建下载函数
download_with_fallback() {
local file=$1
local urls=("${@:2}")
for url in "${urls[@]}"; do
echo "尝试下载: $url"
if curl -L --fail --connect-timeout 10 -o "$file" "$url"; then
echo "✅ 下载成功: $file"
return 0
fi
done
echo "❌ 所有源下载失败: $file"
return 1
}
# 下载 CMake
CMAKE_URLS=(
"https://cmake.org/files/v3.27/cmake-3.27.7-linux-x86_64.tar.gz"
"https://mirrors.tuna.tsinghua.edu.cn/cmake/v3.27/cmake-3.27.7-linux-x86_64.tar.gz"
)
download_with_fallback "cmake.tar.gz" "${CMAKE_URLS[@]}"
🚀 网络优化配置
DNS 优化
# 配置更快的 DNS
sudo tee /etc/systemd/resolved.conf << 'EOF'
[Resolve]
DNS=223.5.5.5 119.29.29.29 8.8.8.8
Domains=~.
EOF
# 重启 DNS 服务
sudo systemctl restart systemd-resolved
# 验证 DNS 配置
systemd-resolve --status
网络参数优化
# 优化网络参数
sudo tee -a /etc/sysctl.conf << 'EOF'
# 网络优化
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 65536 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
EOF
# 应用配置
sudo sysctl -p
🔧 代理配置
HTTP/HTTPS 代理
# 设置系统代理
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7890
# 添加到 shell 配置文件
cat >> ~/.bashrc << 'EOF'
# 代理配置
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export all_proxy=socks5://127.0.0.1:7890
EOF
# 重新加载配置
source ~/.bashrc
Git 代理配置
# 为 Git 设置代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# 只为 GitHub 设置代理
git config --global http.https://github.com.proxy http://127.0.0.1:7890
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
curl/wget 代理
# curl 使用代理
curl --proxy http://127.0.0.1:7890 https://www.google.com
# wget 使用代理
wget --proxy-user=user --proxy-password=pass \
--proxy=on -e use_proxy=yes \
-e http_proxy=http://127.0.0.1:7890 \
https://example.com/file.tar.gz
📊 网络监控脚本
网络质量监控
#!/bin/bash
# 网络质量监控脚本
LOG_FILE="network_monitor.log"
monitor_network() {
while true; do
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# 测试延迟
ping_result=$(ping -c 1 -W 2 8.8.8.8 2>/dev/null | grep 'time=' | cut -d'=' -f4)
# 测试下载速度
speed=$(curl -o /dev/null -s -w '%{speed_download}' https://www.baidu.com 2>/dev/null)
speed_kb=$((speed / 1024))
# 记录结果
echo "[$timestamp] 延迟: ${ping_result:-超时}, 速度: ${speed_kb}KB/s" | tee -a $LOG_FILE
sleep 60
done
}
echo "开始网络监控..."
monitor_network
换源效果测试
#!/bin/bash
# 换源效果测试脚本
test_source_speed() {
local name=$1
local url=$2
echo "测试 $name..."
# 测试连接时间
connect_time=$(curl -o /dev/null -s -w '%{time_connect}' "$url" 2>/dev/null || echo "超时")
# 测试下载速度
total_time=$(curl -o /dev/null -s -w '%{time_total}' "$url" 2>/dev/null || echo "超时")
echo "$name: 连接=${connect_time}s, 总时间=${total_time}s"
}
echo "测试各大源的访问速度..."
# 测试 APT 源
test_source_speed "阿里云APT" "https://mirrors.aliyun.com/ubuntu/"
test_source_speed "清华APT" "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
# 测试 NPM 源
test_source_speed "淘宝NPM" "https://registry.npmmirror.com/"
test_source_speed "华为NPM" "https://mirrors.huaweicloud.com/repository/npm/"
# 测试 GitHub
test_source_speed "GitHub" "https://github.com"
test_source_speed "GitHub代理" "https://ghproxy.com/"
✅ 换源验证
验证脚本
#!/bin/bash
# 换源效果验证脚本
echo "🔍 验证换源配置..."
# 验证 APT 源
echo "📦 APT 源测试..."
sudo apt update && echo "✅ APT 源正常" || echo "❌ APT 源异常"
# 验证 NPM 源
echo "📦 NPM 源测试..."
npm config get registry
npm ping && echo "✅ NPM 源正常" || echo "❌ NPM 源异常"
# 验证 pnpm 源
echo "📦 pnpm 源测试..."
pnpm config get registry
# 验证 Git 配置
echo "📦 Git 配置..."
git config --global --get url."https://ghproxy.com/https://github.com/".insteadof
# 测试下载速度
echo "⚡ 速度测试..."
echo "下载测试文件..."
time curl -o /tmp/test.html https://www.baidu.com && rm -f /tmp/test.html
echo "✅ 换源验证完成"
🚀 下一步
网络配置优化后,继续:
❗ 常见网络问题
问题1:换源后仍然很慢
解决方案:
# 测试不同时间段的网络速度
for i in {1..5}; do
echo "第 $i 次测试:"
time curl -o /dev/null -s https://registry.npmmirror.com
sleep 10
done
# 尝试使用不同的镜像源
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
问题2:部分域名无法访问
解决方案:
# 检查 DNS 解析
nslookup github.com
# 尝试使用不同的 DNS
echo "nameserver 223.5.5.5" | sudo tee /etc/resolv.conf
# 使用代理访问
export https_proxy=http://127.0.0.1:7890
问题3:下载中断或失败
解决方案:
# 使用断点续传
wget -c https://example.com/large-file.tar.gz
# 增加重试次数
wget --tries=5 --timeout=30 https://example.com/file.tar.gz
# 使用 aria2 多线程下载
sudo apt install aria2
aria2c -x 10 -s 10 https://example.com/file.tar.gz
问题4:SSL 证书问题
解决方案:
# 更新 CA 证书
sudo apt update
sudo apt install ca-certificates
# 临时跳过 SSL 验证(不推荐)
curl -k https://example.com
wget --no-check-certificate https://example.com
需要帮助? 查看 常见问题解决 获取更多网络和系统配置解决方案。