3.7 KiB
3.7 KiB
政府端API 404错误问题分析报告
问题描述
- 错误URL:
https://ad.ningmuyun.com/api/government/market-price?type=beef - 错误状态: 404 Not Found
- 影响范围: 政府端管理系统市场价格功能
问题根因分析
1. 后端代码检查 ✅
- 路由配置:
government-backend/routes/government.js第11行router.get('/market-price', governmentController.getMarketPrice); - 控制器方法:
governmentController.getMarketPrice已正确实现 - 应用路由注册:
app.js第50行已正确注册app.use('/api/government', require('./routes/government'));
2. 前端代码检查 ✅
- API调用:
government-admin/src/views/MarketPrice.vue第94行const response = await axios.get('/api/government/market-price', { params: { type } }) - 代理配置:
vite.config.js开发环境代理配置正确
3. Nginx配置问题 ❌
问题所在: nginx配置中政府API的路径匹配错误
原始错误配置:
location ^~ /government/api/ {
proxy_pass http://localhost:5352/;
}
问题分析:
- 前端请求路径:
/api/government/market-price - nginx匹配路径:
/government/api/ - 路径不匹配导致404错误
解决方案
修复nginx配置
将政府API代理路径从 /government/api/ 修改为 /api/government/:
# 政府API代理 - 修复路径匹配问题
location ^~ /api/government/ {
proxy_pass http://localhost:5352/api/government/;
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;
}
}
部署步骤
-
备份当前nginx配置:
sudo cp /etc/nginx/conf.d/ningmuyun_one.conf /etc/nginx/conf.d/ningmuyun_one.conf.backup -
应用修复配置:
sudo cp nginx-fix-government-api.conf /etc/nginx/conf.d/ # 或者手动编辑配置文件,将上述配置添加到 ad.ningmuyun.com server 块中 -
测试nginx配置:
sudo nginx -t -
重新加载nginx:
sudo systemctl reload nginx -
验证修复:
curl -k https://ad.ningmuyun.com/api/government/market-price?type=beef
预期结果
修复后,https://ad.ningmuyun.com/api/government/market-price?type=beef 应该返回正确的市场价格数据,而不是404错误。
相关文件
government-backend/_etc_nginx_conf.d_ningmuyun_one.conf- 主nginx配置文件government-backend/nginx-fix-government-api.conf- 修复配置片段government-backend/routes/government.js- 后端路由government-backend/controllers/governmentController.js- 控制器实现government-admin/src/views/MarketPrice.vue- 前端页面