115 lines
3.8 KiB
JavaScript
115 lines
3.8 KiB
JavaScript
|
|
const axios = require('axios');
|
|||
|
|
|
|||
|
|
// 测试nginx配置问题
|
|||
|
|
async function testNginxConfig() {
|
|||
|
|
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/auth/login',
|
|||
|
|
method: 'POST',
|
|||
|
|
data: { username: 'admin', password: 'admin123' },
|
|||
|
|
expected: '应该返回401认证错误'
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
name: '测试重复路径接口',
|
|||
|
|
url: 'https://ad.ningmuyun.com/api/government/government/departments',
|
|||
|
|
expected: '应该返回200(如果nginx配置有问题)'
|
|||
|
|
}
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
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(' 1. nginx配置语法错误');
|
|||
|
|
console.log(' 2. nginx服务未重启');
|
|||
|
|
console.log(' 3. location规则冲突');
|
|||
|
|
console.log(' 4. proxy_pass配置错误');
|
|||
|
|
console.log('');
|
|||
|
|
console.log('🔄 建议的解决步骤:');
|
|||
|
|
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');
|
|||
|
|
console.log(' 4. 检查nginx访问日志: sudo tail -f /var/log/nginx/ad.ningmuyun.com.access.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(' }');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 运行测试
|
|||
|
|
testNginxConfig().catch(console.error);
|