docs(guide): 添加502错误排查指南并移除端口修改指南

- 添加502 Bad Gateway错误排查指南文档
- 移除旧的端口修改指南文档
- 提供完整的错误排查步骤和解决方案
- 包含Nginx配置检查和后端服务验证流程
- 添加快速修复步骤和常见问题处理
- 移除过时的端口变更操作说明
This commit is contained in:
2025-12-26 14:11:13 +08:00
parent 56ae5a5892
commit bbf235362c
8 changed files with 6948 additions and 23910 deletions

View File

@@ -0,0 +1,220 @@
# 502 Bad Gateway 错误排查指南
## 错误说明
`502 Bad Gateway` 表示 Nginx 无法连接到后端服务。通常是因为:
1. **后端服务未启动**
2. **Nginx配置中的端口不正确**(最常见)
3. **后端服务启动失败**
4. **防火墙阻止连接**
## 排查步骤
### 1. 检查后端服务是否启动
在服务器上执行:
```bash
# 检查18082端口是否在监听
netstat -tlnp | grep 18082
# 或
ss -tlnp | grep 18082
# 检查Java进程
ps aux | grep java
# 或
jps -l
```
**如果端口没有监听:**
- 后端服务没有启动
- 需要启动后端服务
### 2. 检查Nginx配置最重要
在宝塔面板中:
1. 网站 → 设置 → 配置文件
2. 找到 `location /api/` 配置块
3. **确认 `proxy_pass` 端口是 `18082`**
**正确的配置:**
```nginx
location /api/ {
proxy_pass http://127.0.0.1:18082; # ← 必须是18082不是8082
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
```
**错误的配置会导致502**
```nginx
location /api/ {
proxy_pass http://127.0.0.1:8082; # ← 错误端口还是8082
...
}
```
**修改后必须:**
1. 点击"保存"
2. 点击"重载配置"或"重启Nginx"
### 3. 测试后端服务是否正常
在服务器上执行:
```bash
# 测试后端API是否可访问
curl http://127.0.0.1:18082/api/health
# 或
curl http://127.0.0.1:18082/api/erp/user/login -X POST -H "Content-Type: application/json" -d '{"username":"test","password":"test"}'
```
**如果返回连接拒绝:**
- 后端服务没有启动
- 检查启动日志
**如果返回正常响应:**
- 后端服务正常
- 问题在Nginx配置
### 4. 检查后端启动日志
查看后端启动日志,确认:
- 服务是否成功启动
- 是否监听在 `18082` 端口
- 是否有错误信息
```bash
# 如果使用nohup启动
tail -f app.log
# 或查看Spring Boot启动日志
# 应该看到类似信息:
# Tomcat started on port(s): 18082 (http)
```
### 5. 检查防火墙
在宝塔面板中:
1. 安全 → 防火墙
2. 确认端口 `18082` 已开放(如果后端需要外部访问)
3. **注意:** 如果后端只监听 `127.0.0.1`,不需要开放防火墙
### 6. 检查后端监听地址
确认后端配置:
**application-dev.yml:**
```yaml
server:
port: 18082
# 如果没有配置 address默认监听 0.0.0.0(所有网卡)
# 如果配置了 address: 127.0.0.1,只监听本地
```
**如果后端只监听 127.0.0.1**
- Nginx的 `proxy_pass` 必须使用 `http://127.0.0.1:18082`
- 不能使用 `http://175.178.252.59:18082`
## 快速修复步骤
### 步骤1确认后端服务已启动
```bash
# 在服务器上执行
java -jar mt-startup-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
# 或后台运行
nohup java -jar mt-startup-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev > app.log 2>&1 &
```
### 步骤2修改Nginx配置
在宝塔面板中修改配置文件:
```nginx
location /api/ {
proxy_pass http://127.0.0.1:18082; # 确保是18082
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 添加超时配置(可选)
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
```
### 步骤3重载Nginx
在宝塔面板中:
1. 保存配置
2. 重载配置
### 步骤4验证
```bash
# 在服务器上测试
curl http://127.0.0.1/api/erp/user/login -X POST -H "Content-Type: application/json" -d '{"username":"test","password":"test"}'
```
## 常见问题
### Q1: 修改了Nginx配置但还是502
**A:** 检查是否:
- 保存了配置
- 重载了Nginx不是只保存
- 确认端口是 `18082`,不是 `8082`
### Q2: 后端服务启动了但Nginx还是502
**A:** 检查:
- Nginx配置中的端口是否正确
- 后端是否真的在 `18082` 端口监听
- Nginx错误日志`/www/wwwlogs/175.178.252.59.error.log`
### Q3: 如何查看Nginx错误日志
**A:** 在宝塔面板中:
- 网站 → 日志 → 错误日志
- 或直接查看:`/www/wwwlogs/175.178.252.59.error.log`
查看是否有类似错误:
```
connect() failed (111: Connection refused) while connecting to upstream
```
这表示无法连接到后端。
## 验证清单
- [ ] 后端服务已启动
- [ ] 后端监听在 `18082` 端口
- [ ] Nginx配置中 `proxy_pass` 端口是 `18082`
- [ ] Nginx配置已保存并重载
- [ ] 防火墙已开放(如果需要)
- [ ] 后端启动日志无错误
## 如果还是无法解决
1. **查看Nginx错误日志**
```bash
tail -f /www/wwwlogs/175.178.252.59.error.log
```
2. **查看后端启动日志**
```bash
tail -f app.log
```
3. **检查端口占用**
```bash
netstat -tlnp | grep 18082
```
4. **测试后端直接访问**
```bash
curl http://127.0.0.1:18082/api/health
```