const axios = require('axios'); // 测试nginx配置修复后的API接口 async function testNginxFix() { console.log('🔍 测试nginx配置修复后的API接口...\n'); const testCases = [ { name: '登录接口测试', url: 'https://ad.ningmuyun.com/api/government/auth/login', method: 'POST', data: { username: 'admin', password: 'admin123' }, expected: '应该返回401认证错误(接口正常)' }, { name: '部门列表接口测试', url: 'https://ad.ningmuyun.com/api/government/departments', method: 'GET', expected: '应该返回200状态码和部门数据' }, { name: '数据中心接口测试', url: 'https://ad.ningmuyun.com/api/government/data-center', method: 'GET', expected: '应该返回200状态码和统计数据' }, { name: '市场价格接口测试', url: 'https://ad.ningmuyun.com/api/government/market-price?type=beef', method: 'GET', expected: '应该返回200状态码和价格数据' }, { name: '岗位列表接口测试', url: 'https://ad.ningmuyun.com/api/government/positions', method: 'GET', expected: '应该返回200状态码和岗位数据' } ]; for (const test of testCases) { console.log(`📋 ${test.name}`); console.log(` URL: ${test.url}`); console.log(` 预期: ${test.expected}`); try { const config = { method: test.method, 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('📝 修复说明:'); console.log(' 问题: location规则缩进不正确,被嵌套在错误的块内'); console.log(' 修复: 调整了location规则的缩进和位置'); console.log(''); console.log('🔄 现在需要重启nginx使配置生效:'); console.log(' sudo nginx -t # 检查配置语法'); console.log(' sudo systemctl reload nginx # 重新加载配置'); } // 运行测试 testNginxFix().catch(console.error);