150 lines
3.7 KiB
Markdown
150 lines
3.7 KiB
Markdown
|
|
# 检查 Nginx 配置的关键步骤
|
|||
|
|
|
|||
|
|
## 🔍 问题分析
|
|||
|
|
|
|||
|
|
根据您的反馈:
|
|||
|
|
- 修改 nginx 配置后,所有接口都返回 404
|
|||
|
|
- 只有登录接口正常(`/api/government/auth/login`)
|
|||
|
|
- 重复路径可以正常工作(`/api/government/government/departments`)
|
|||
|
|
|
|||
|
|
这说明:
|
|||
|
|
1. Nginx 已经重启并读取了新配置
|
|||
|
|
2. `/api/government/auth/` 规则工作正常
|
|||
|
|
3. `/api/government/` 规则可能有问题
|
|||
|
|
|
|||
|
|
## 🎯 关键问题
|
|||
|
|
|
|||
|
|
**重复路径能工作说明了什么?**
|
|||
|
|
|
|||
|
|
如果 `/api/government/government/departments` 能返回 200,说明:
|
|||
|
|
- 请求到达了 nginx
|
|||
|
|
- Nginx 将请求代理到了后端
|
|||
|
|
- 后端处理了 `/api/government/departments` 路径(去掉了一个 government)
|
|||
|
|
|
|||
|
|
这意味着当前的 proxy_pass 配置可能是:
|
|||
|
|
```nginx
|
|||
|
|
proxy_pass http://localhost:5352/api/government/;
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这会导致:
|
|||
|
|
- 请求:`/api/government/government/departments`
|
|||
|
|
- Nginx 匹配:`location ^~ /api/government/`
|
|||
|
|
- 代理到:`http://localhost:5352/api/government/ + government/departments`
|
|||
|
|
- 实际请求:`http://localhost:5352/api/government/government/departments`
|
|||
|
|
|
|||
|
|
但是后端路由是 `/api/government/departments`,所以单个 government 的路径会 404。
|
|||
|
|
|
|||
|
|
## 🔧 正确的解决方案
|
|||
|
|
|
|||
|
|
需要修改 `proxy_pass` 配置,去掉路径重写:
|
|||
|
|
|
|||
|
|
```nginx
|
|||
|
|
# 错误的配置(当前)
|
|||
|
|
location ^~ /api/government/ {
|
|||
|
|
proxy_pass http://localhost:5352/api/government/;
|
|||
|
|
# 这会导致路径重复:/api/government/departments → http://localhost:5352/api/government/departments
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 正确的配置
|
|||
|
|
location ^~ /api/government/ {
|
|||
|
|
proxy_pass http://localhost:5352;
|
|||
|
|
# 这会保持原始路径:/api/government/departments → http://localhost:5352/api/government/departments
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 📝 修改步骤
|
|||
|
|
|
|||
|
|
### 1. 修改 nginx 配置文件
|
|||
|
|
|
|||
|
|
找到 `_etc_nginx_conf.d_ningmuyun_one.conf` 文件中的这一段:
|
|||
|
|
|
|||
|
|
```nginx
|
|||
|
|
# 政府API代理 - 处理其他政府相关接口
|
|||
|
|
location ^~ /api/government/ {
|
|||
|
|
proxy_pass http://localhost:5352/api/government/; # ❌ 错误
|
|||
|
|
# ... 其他配置
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
修改为:
|
|||
|
|
|
|||
|
|
```nginx
|
|||
|
|
# 政府API代理 - 处理其他政府相关接口
|
|||
|
|
location ^~ /api/government/ {
|
|||
|
|
proxy_pass http://localhost:5352; # ✅ 正确
|
|||
|
|
# ... 其他配置
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 重启 nginx
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo nginx -t
|
|||
|
|
sudo systemctl reload nginx
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 验证修复
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 应该返回 200
|
|||
|
|
curl -X GET https://ad.ningmuyun.com/api/government/departments
|
|||
|
|
|
|||
|
|
# 应该返回 404(因为路径不再重复)
|
|||
|
|
curl -X GET https://ad.ningmuyun.com/api/government/government/departments
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 🎯 proxy_pass 尾部斜杠的区别
|
|||
|
|
|
|||
|
|
这是一个非常重要的 nginx 配置细节:
|
|||
|
|
|
|||
|
|
### 有尾部斜杠
|
|||
|
|
```nginx
|
|||
|
|
location ^~ /api/government/ {
|
|||
|
|
proxy_pass http://localhost:5352/api/government/;
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
- 请求:`/api/government/departments`
|
|||
|
|
- 匹配部分:`/api/government/`
|
|||
|
|
- 剩余部分:`departments`
|
|||
|
|
- 代理到:`http://localhost:5352/api/government/departments`
|
|||
|
|
|
|||
|
|
### 无尾部斜杠
|
|||
|
|
```nginx
|
|||
|
|
location ^~ /api/government/ {
|
|||
|
|
proxy_pass http://localhost:5352;
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
- 请求:`/api/government/departments`
|
|||
|
|
- 代理到:`http://localhost:5352/api/government/departments`(保持完整路径)
|
|||
|
|
|
|||
|
|
## 📊 当前配置应该是
|
|||
|
|
|
|||
|
|
```nginx
|
|||
|
|
# 政府认证API代理
|
|||
|
|
location ^~ /api/government/auth/ {
|
|||
|
|
proxy_pass http://localhost:5352/api/auth/; # ✅ 正确(需要去掉 government)
|
|||
|
|
# ... 其他配置
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 政府API代理
|
|||
|
|
location ^~ /api/government/ {
|
|||
|
|
proxy_pass http://localhost:5352; # ✅ 正确(保持完整路径)
|
|||
|
|
# ... 其他配置
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 🚨 总结
|
|||
|
|
|
|||
|
|
修改配置文件,将:
|
|||
|
|
```nginx
|
|||
|
|
proxy_pass http://localhost:5352/api/government/;
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
改为:
|
|||
|
|
```nginx
|
|||
|
|
proxy_pass http://localhost:5352;
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
然后重启 nginx,问题应该就能解决了。
|
|||
|
|
|