Files
nxxmdata/government-backend/check-nginx-syntax.js
2025-10-23 17:26:47 +08:00

78 lines
2.3 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs');
const path = require('path');
// 检查nginx配置语法
const nginxConfigPath = path.join(__dirname, 'nginx.conf');
const nginxConfig = fs.readFileSync(nginxConfigPath, 'utf8');
console.log('检查nginx配置语法...');
console.log('='.repeat(50));
// 检查server块
const serverBlocks = nginxConfig.match(/server\s*{/g) || [];
console.log(`发现 ${serverBlocks.length} 个server块`);
// 检查location块
const locationBlocks = nginxConfig.match(/location\s+[^{]+{/g) || [];
console.log(`发现 ${locationBlocks.length} 个location块`);
// 检查API相关的location块
const apiLocationBlocks = locationBlocks.filter(block =>
block.includes('/api') || block.includes('proxy_pass')
);
console.log(`\nAPI相关的location块:`);
apiLocationBlocks.forEach((block, index) => {
console.log(`${index + 1}. ${block.trim()}`);
});
// 检查是否有重复的location路径
const locationPaths = locationBlocks.map(block => {
const pathMatch = block.match(/location\s+([^{]+)/);
return pathMatch ? pathMatch[1].trim() : '';
});
const duplicatePaths = locationPaths.filter((path, index) =>
locationPaths.indexOf(path) !== index && path !== ''
);
if (duplicatePaths.length > 0) {
console.log('\n❌ 发现重复的location路径:');
duplicatePaths.forEach(path => {
console.log(` - ${path}`);
});
} else {
console.log('\n✅ 没有重复的location路径');
}
// 检查proxy_pass配置
const proxyPassBlocks = nginxConfig.match(/proxy_pass\s+[^;]+;/g) || [];
console.log(`\nproxy_pass配置:`);
proxyPassBlocks.forEach((block, index) => {
console.log(`${index + 1}. ${block.trim()}`);
});
// 检查是否有路径重复的proxy_pass
const problematicProxyPass = proxyPassBlocks.filter(block => {
const url = block.match(/proxy_pass\s+([^;]+);/);
if (url) {
const proxyUrl = url[1].trim();
// 检查是否以/api/结尾
return proxyUrl.endsWith('/api/');
}
return false;
});
if (problematicProxyPass.length > 0) {
console.log('\n⚠ 发现可能有问题的proxy_pass配置:');
problematicProxyPass.forEach(block => {
console.log(` - ${block.trim()}`);
});
console.log(' 这些配置可能导致路径重复问题');
} else {
console.log('\n✅ proxy_pass配置看起来正常');
}
console.log('\n' + '='.repeat(50));
console.log('nginx配置检查完成');