127 lines
4.3 KiB
Markdown
127 lines
4.3 KiB
Markdown
|
|
# 政府端登录接口404错误修复说明
|
|||
|
|
|
|||
|
|
## 问题描述
|
|||
|
|
- **错误URL**: `https://ad.ningmuyun.com/api/government/auth/login`
|
|||
|
|
- **错误状态**: 404 Not Found
|
|||
|
|
- **影响范围**: 政府端管理系统登录功能
|
|||
|
|
|
|||
|
|
## 问题根因分析
|
|||
|
|
|
|||
|
|
### 1. 前端API调用路径
|
|||
|
|
- **前端调用**: `https://ad.ningmuyun.com/api/government/auth/login`
|
|||
|
|
- **API定义**: `government-admin/src/utils/api.js` 第74行
|
|||
|
|
```javascript
|
|||
|
|
login: (data) => instance.post('/auth/login', data)
|
|||
|
|
```
|
|||
|
|
- **baseURL**: `http://localhost:5352/api` (开发环境)
|
|||
|
|
- **实际请求**: `http://localhost:5352/api/auth/login`
|
|||
|
|
|
|||
|
|
### 2. 后端路由配置 ✅
|
|||
|
|
- **认证路由**: `government-backend/routes/auth.js` 第6行
|
|||
|
|
```javascript
|
|||
|
|
router.post('/login', login)
|
|||
|
|
```
|
|||
|
|
- **应用路由注册**: `government-backend/app.js` 第39行
|
|||
|
|
```javascript
|
|||
|
|
app.use('/api/auth', require('./routes/auth'));
|
|||
|
|
```
|
|||
|
|
- **完整路径**: `/api/auth/login`
|
|||
|
|
|
|||
|
|
### 3. Nginx配置问题 ❌
|
|||
|
|
**问题所在**: nginx配置中缺少政府认证API的路径匹配
|
|||
|
|
|
|||
|
|
**原始配置问题**:
|
|||
|
|
- 只有 `/api/government/` 的代理配置
|
|||
|
|
- 缺少 `/api/government/auth/` 的专门代理配置
|
|||
|
|
- 导致 `/api/government/auth/login` 请求无法正确路由到后端的 `/api/auth/login`
|
|||
|
|
|
|||
|
|
## 解决方案
|
|||
|
|
|
|||
|
|
### 修复nginx配置
|
|||
|
|
在 `government-backend/_etc_nginx_conf.d_ningmuyun_one.conf` 中添加政府认证API代理配置:
|
|||
|
|
|
|||
|
|
```nginx
|
|||
|
|
# 政府认证API代理 - 处理登录等认证接口
|
|||
|
|
location ^~ /api/government/auth/ {
|
|||
|
|
proxy_pass http://localhost:5352/api/auth/; # 政府后端认证服务端口
|
|||
|
|
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_set_header X-Forwarded-Proto $scheme;
|
|||
|
|
|
|||
|
|
# CORS配置
|
|||
|
|
add_header 'Access-Control-Allow-Origin' 'https://ad.ningmuyun.com' always;
|
|||
|
|
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
|
|||
|
|
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With' always;
|
|||
|
|
add_header 'Access-Control-Allow-Credentials' 'true' always;
|
|||
|
|
|
|||
|
|
if ($request_method = 'OPTIONS') {
|
|||
|
|
add_header 'Access-Control-Allow-Origin' 'https://ad.ningmuyun.com';
|
|||
|
|
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
|
|||
|
|
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With';
|
|||
|
|
add_header 'Access-Control-Allow-Credentials' 'true';
|
|||
|
|
add_header 'Content-Type' 'text/plain; charset=UTF-8';
|
|||
|
|
add_header 'Content-Length' 0;
|
|||
|
|
return 204;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 路径映射说明
|
|||
|
|
- **前端请求**: `/api/government/auth/login`
|
|||
|
|
- **nginx匹配**: `location ^~ /api/government/auth/`
|
|||
|
|
- **代理转发**: `http://localhost:5352/api/auth/login`
|
|||
|
|
- **后端处理**: `/api/auth/login` (authController.login)
|
|||
|
|
|
|||
|
|
## 验证步骤
|
|||
|
|
|
|||
|
|
1. **重启nginx服务**:
|
|||
|
|
```bash
|
|||
|
|
sudo nginx -t # 检查配置语法
|
|||
|
|
sudo systemctl reload nginx # 重新加载配置
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
2. **测试登录接口**:
|
|||
|
|
```bash
|
|||
|
|
curl -X POST https://ad.ningmuyun.com/api/government/auth/login \
|
|||
|
|
-H "Content-Type: application/json" \
|
|||
|
|
-d '{"username":"test","password":"test"}'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
3. **检查前端登录功能**:
|
|||
|
|
- 访问政府端管理系统登录页面
|
|||
|
|
- 输入正确的用户名和密码
|
|||
|
|
- 确认登录成功
|
|||
|
|
|
|||
|
|
## 相关文件
|
|||
|
|
- `government-backend/_etc_nginx_conf.d_ningmuyun_one.conf` - nginx配置文件
|
|||
|
|
- `government-backend/routes/auth.js` - 认证路由
|
|||
|
|
- `government-backend/controllers/authController.js` - 认证控制器
|
|||
|
|
- `government-admin/src/utils/api.js` - 前端API配置
|
|||
|
|
- `government-admin/src/stores/auth.js` - 前端认证状态管理
|
|||
|
|
|
|||
|
|
## 修复时间
|
|||
|
|
2024年12月19日
|
|||
|
|
|
|||
|
|
## 修复状态
|
|||
|
|
✅ 已修复 - nginx配置已更新,添加了政府认证API代理配置
|
|||
|
|
|
|||
|
|
## 重要说明
|
|||
|
|
**配置优先级**: 在nginx中,更具体的location规则会优先匹配。因此:
|
|||
|
|
- `/api/government/auth/` 配置必须在 `/api/government/` 配置之前
|
|||
|
|
- 这样 `/api/government/auth/login` 会匹配到第一个规则,正确代理到 `/api/auth/login`
|
|||
|
|
- 其他 `/api/government/` 开头的请求会匹配到第二个规则
|
|||
|
|
|
|||
|
|
## 测试方法
|
|||
|
|
运行测试脚本验证修复效果:
|
|||
|
|
```bash
|
|||
|
|
cd government-backend
|
|||
|
|
node test-login-api.js
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 重启nginx命令
|
|||
|
|
```bash
|
|||
|
|
sudo nginx -t # 检查配置语法
|
|||
|
|
sudo systemctl reload nginx # 重新加载配置
|
|||
|
|
```
|