# 生产环境连接超时排查指南 ## 问题现象 - **症状**: 生产环境无法访问,连接超时 - **发生时间**: 刚刚发生 - **影响范围**: novalon.cn, git.f.novalon.cn, ci.f.novalon.cn - **服务器IP**: 139.155.109.62 ## 诊断结果(本地) ### ✅ 正常项 - ✅ 本地网络连接正常 - ✅ DNS解析成功 - ✅ TCP端口连接成功(80, 443) ### ❌ 异常项 - ❌ HTTP响应超时 - ❌ 应用层无响应 ## 根因分析 根据诊断结果,问题定位在**应用层**: 1. **网络层正常**: DNS解析、TCP连接都正常 2. **应用层异常**: HTTP请求无响应 可能的原因: - Docker容器崩溃或停止 - Nginx反向代理异常 - 应用服务崩溃 - 服务器资源耗尽(CPU/内存/磁盘) ## 排查步骤 ### 步骤1: SSH登录服务器 ```bash # 登录生产服务器 ssh root@139.155.109.62 # 或 ssh user@139.155.109.62 ``` ### 步骤2: 上传并运行诊断脚本 ```bash # 方法1: 从本地上传脚本 scp scripts/remote-server-diagnosis.sh root@139.155.109.62:/tmp/ # 方法2: 直接在服务器上创建脚本 # 复制 remote-server-diagnosis.sh 的内容到服务器 # 运行诊断脚本 chmod +x /tmp/remote-server-diagnosis.sh /tmp/remote-server-diagnosis.sh --full ``` ### 步骤3: 手动排查(如果脚本无法运行) #### 3.1 检查系统资源 ```bash # 查看CPU和内存 top -bn1 | head -20 # 查看磁盘 df -h # 查看内存 free -h # 查看系统负载 uptime ``` #### 3.2 检查Docker容器 ```bash # 查看容器状态 docker ps -a # 查看容器日志 docker logs # 查看容器资源使用 docker stats --no-stream # 重启容器 docker restart ``` #### 3.3 检查Nginx ```bash # 查看Nginx状态 systemctl status nginx # 测试Nginx配置 nginx -t # 重启Nginx systemctl restart nginx # 查看Nginx日志 tail -50 /var/log/nginx/error.log ``` #### 3.4 检查应用服务 ```bash # 查看Node.js进程 ps aux | grep node # 查看端口占用 netstat -tlnp | grep -E ":(3000|80|443)" # 测试本地应用 curl -I http://localhost:3000 ``` ## 快速修复方案 ### 方案1: 重启所有服务 ```bash # 重启Docker容器 cd /path/to/project docker-compose -f docker-compose.prod.yml restart # 重启Nginx sudo systemctl restart nginx # 检查服务状态 docker ps systemctl status nginx ``` ### 方案2: 清理资源并重启 ```bash # 清理Docker资源 docker system prune -a -f # 清理日志 sudo journalctl --vacuum-time=3d # 重启服务 sudo systemctl restart docker docker-compose -f docker-compose.prod.yml up -d sudo systemctl restart nginx ``` ### 方案3: 完全重建 ```bash # 停止所有容器 docker-compose -f docker-compose.prod.yml down # 清理所有资源 docker system prune -a -f --volumes # 重新构建和启动 docker-compose -f docker-compose.prod.yml build --no-cache docker-compose -f docker-compose.prod.yml up -d # 重启Nginx sudo systemctl restart nginx ``` ## 验证修复 ### 本地验证 ```bash # 测试网站访问 curl -I https://novalon.cn # 测试Git服务器 git ls-remote https://git.f.novalon.cn/novalon/novalon-website.git # 测试CI服务器 curl -I https://ci.f.novalon.cn ``` ### 服务器验证 ```bash # 测试本地应用 curl -I http://localhost:3000 # 检查容器状态 docker ps # 检查Nginx状态 systemctl status nginx # 检查端口监听 netstat -tlnp | grep -E ":(3000|80|443)" ``` ## 监控和预防 ### 设置监控 ```bash # 安装监控工具 docker run -d --name=monitor \ --restart=unless-stopped \ -p 9090:9090 \ prom/prometheus # 设置日志轮转 sudo nano /etc/logrotate.d/nginx ``` ### 定期清理 ```bash # 创建定时清理脚本 cat > /etc/cron.daily/docker-cleanup << 'EOF' #!/bin/bash docker system prune -f journalctl --vacuum-time=7d EOF chmod +x /etc/cron.daily/docker-cleanup ``` ## 紧急联系 如果以上方法都无法解决问题,请: 1. 保存诊断日志: ```bash /tmp/remote-server-diagnosis.sh --full > /tmp/diagnosis-report.log ``` 2. 联系服务器提供商检查网络和硬件 3. 检查是否遭受DDoS攻击: ```bash netstat -an | grep :80 | wc -l ``` ## 相关文档 - [Docker镜像清理脚本](./docker-cleanup.sh) - [网络诊断脚本](./network-diagnosis.sh) - [生产环境诊断脚本](./production-diagnosis.sh) - [远程服务器诊断脚本](./remote-server-diagnosis.sh)