Files
onion-dmp/502错误解决方案.md
2026-04-08 14:52:09 +08:00

223 lines
4.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 🔧 502 Bad Gateway 错误解决方案
## ❓ 问题描述
访问 https://dmp.ink1ing.tech 时出现:
```
Bad gateway Error code 502
```
## <20><> 根本原因
**Node.js 服务器进程崩溃或停止了**
Cloudflare Tunnel 正常运行但本地服务器localhost:3456无法访问导致 Tunnel 无法转发请求。
---
## ✅ 已解决
服务已重新启动并恢复正常:
- ✅ Node.js 服务器: 运行中 (PID: 93335)
- ✅ Cloudflare Tunnel: 已连接
- ✅ 公网访问: https://dmp.ink1ing.tech 正常
---
## 🔍 为什么会挂?
### 可能的原因
1. **内存不足**
- Node.js 进程占用内存过多被系统杀掉
2. **代码错误**
- 重新生成数据库时,旧进程可能因为数据库被删除而崩溃
3. **手动停止**
- 可能在某个操作中意外停止了进程
4. **系统睡眠**
- macOS 进入睡眠后某些进程可能被挂起
---
## 🛠️ 快速修复方法
### 方法 1一键重启推荐
```bash
cd /Users/inkling/Desktop/dmp
./start-daemon.sh
```
### 方法 2完全重启
```bash
cd /Users/inkling/Desktop/dmp
./stop-services.sh
./start-daemon.sh
```
### 方法 3健康检查
```bash
cd /Users/inkling/Desktop/dmp
./health-check.sh
```
如果发现服务未运行执行方法1重启。
---
## 🚨 预防措施
### 1. 进程监控脚本(已创建)
我创建了 `watchdog.sh` 脚本,可以:
- 自动检测服务是否崩溃
- 自动重启失败的服务
- 记录监控日志
**使用方法**
```bash
# 手动运行一次检查
./watchdog.sh
# 或设置 cron 定时任务每5分钟检查一次
# 打开 crontab 编辑器
crontab -e
# 添加以下行
*/5 * * * * /Users/inkling/Desktop/dmp/watchdog.sh
```
### 2. 开机自启动
创建 macOS LaunchAgent
```bash
cat > ~/Library/LaunchAgents/com.dmp.service.plist << 'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.dmp.service</string>
<key>ProgramArguments</key>
<array>
<string>/Users/inkling/Desktop/dmp/start-daemon.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>StandardOutPath</key>
<string>/Users/inkling/Desktop/dmp/logs/launchd.log</string>
<key>StandardErrorPath</key>
<string>/Users/inkling/Desktop/dmp/logs/launchd.error.log</string>
</dict>
</plist>
PLIST
# 加载服务
launchctl load ~/Library/LaunchAgents/com.dmp.service.plist
```
### 3. 服务器优化
如果频繁崩溃,可以优化 `server.js`
```javascript
// 添加错误处理
process.on('uncaughtException', (err) => {
console.error('未捕获的异常:', err);
// 不退出进程
});
process.on('unhandledRejection', (reason, promise) => {
console.error('未处理的 Promise 拒绝:', reason);
});
```
---
## 📊 监控和日志
### 查看日志
```bash
# 服务器日志
tail -f logs/server.log
# Tunnel 日志
tail -f logs/tunnel.log
# 监控日志
tail -f logs/watchdog.log
# 监控日志(如果设置了)
tail -f logs/monitor.log
```
### 检查进程
```bash
# 查看所有相关进程
ps aux | grep -E "node server|cloudflared tunnel" | grep -v grep
# 查看进程资源使用
top -pid $(pgrep -f "node server.js")
```
---
## 🔔 告警通知(高级)
可以配置 watchdog 脚本在服务崩溃时发送通知:
```bash
# 在 watchdog.sh 中添加
if ! pgrep -f "node server.js" > /dev/null; then
# macOS 通知
osascript -e 'display notification "DMP 服务已崩溃并重启" with title "服务监控"'
# 或发送邮件/企业微信/钉钉等
fi
```
---
## ✅ 验证修复
访问以下地址确认服务正常:
- 本地: http://localhost:3456
- 公网: https://dmp.ink1ing.tech
运行健康检查:
```bash
./health-check.sh
```
应该看到所有检查项都是 ✅
---
## 📝 故障排查清单
如果仍然出现 502
1. ✅ 检查本地服务:`curl http://localhost:3456`
2. ✅ 检查进程:`ps aux | grep "node server"`
3. ✅ 检查 Tunnel`cloudflared tunnel info dmp-tunnel`
4. ✅ 查看日志:`tail -f logs/*.log`
5. ✅ 重启服务:`./start-daemon.sh`
6. ✅ 清除缓存:浏览器 Ctrl+Shift+R 强制刷新
---
生成时间: $(date)