const axios = require('axios'); // 调试nginx配置问题 async function testNginxDebug() { console.log('🔍 调试nginx配置问题...\n'); const testCases = [ { name: '直接测试后端服务', url: 'http://localhost:5352/api/government/departments', expected: '应该返回200' }, { name: '通过nginx测试部门接口', url: 'https://ad.ningmuyun.com/api/government/departments', expected: '应该返回200' }, { name: '通过nginx测试数据中心接口', url: 'https://ad.ningmuyun.com/api/government/data-center', expected: '应该返回200' }, { name: '通过nginx测试市场价格接口', url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef', expected: '应该返回200' }, { name: '通过nginx测试登录接口', url: 'https://ad.ningmuyun.com/api/government/auth/login', method: 'POST', data: { username: 'admin', password: 'admin123' }, expected: '应该返回401认证错误' } ]; for (const test of testCases) { console.log(`📋 ${test.name}`); console.log(` URL: ${test.url}`); console.log(` 预期: ${test.expected}`); try { const config = { method: test.method || 'GET', url: test.url, headers: { 'Content-Type': 'application/json' }, timeout: 10000, validateStatus: function (status) { return status < 500; // 接受所有小于500的状态码 } }; if (test.data) { config.data = test.data; } const response = await axios(config); console.log(` ✅ 状态码: ${response.status}`); if (response.status === 200) { console.log(` ✅ 成功: 接口正常工作`); if (Array.isArray(response.data)) { console.log(` 📊 数据量: ${response.data.length} 条记录`); } else if (response.data && typeof response.data === 'object') { console.log(` 📊 数据类型: 对象,包含 ${Object.keys(response.data).length} 个字段`); } } else if (response.status === 401) { console.log(` ✅ 正常: 需要认证(接口路径正确)`); } else if (response.status === 404) { console.log(` ❌ 错误: 接口不存在`); } } catch (error) { if (error.response) { console.log(` ❌ 状态码: ${error.response.status}`); console.log(` ❌ 错误: ${error.response.statusText}`); if (error.response.status === 404) { console.log(` 🔍 分析: nginx配置问题,路径映射不正确`); } } else if (error.request) { console.log(` ❌ 网络错误: ${error.message}`); } else { console.log(` ❌ 请求配置错误: ${error.message}`); } } console.log(' ' + '─'.repeat(60)); console.log(''); } console.log('🎯 调试完成!'); console.log(''); console.log('📝 当前nginx配置:'); console.log(' location ^~ /api/government/auth/ {'); console.log(' proxy_pass http://localhost:5352/api/auth/;'); console.log(' }'); console.log(' location ^~ /api/government/ {'); console.log(' proxy_pass http://localhost:5352/api/government/;'); console.log(' }'); console.log(''); console.log('🔄 如果所有接口都返回404,可能需要:'); console.log(' 1. 检查nginx配置语法: sudo nginx -t'); console.log(' 2. 重启nginx服务: sudo systemctl reload nginx'); console.log(' 3. 检查nginx错误日志: sudo tail -f /var/log/nginx/error.log'); } // 运行测试 testNginxDebug().catch(console.error);