Files
tyc-webview-v2/scripts/seo-static-generator/generateNginxConfig.js
2026-02-25 11:45:21 +08:00

75 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Nginx配置生成脚本
*/
import { generateNginxConfig, generateHtaccessConfig } from './crawlerMiddleware.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 配置选项
const config = {
serverName: 'www.tianyuancha.cn',
spaRoot: '/var/www/tyc-webview-v2/dist',
staticRoot: '/var/www/tyc-webview-v2/static-pages',
sslEnabled: false, // 如果使用HTTPS设置为true
sslCertPath: '/etc/nginx/ssl/cert.pem',
sslKeyPath: '/etc/nginx/ssl/key.pem'
};
// 生成Nginx配置
function main() {
console.log('='.repeat(60));
console.log(' 生成 Nginx 配置文件');
console.log('='.repeat(60));
console.log();
try {
// 生成Nginx配置
const nginxConfig = generateNginxConfig(config);
const nginxConfigPath = path.join(__dirname, 'nginx.conf');
fs.writeFileSync(nginxConfigPath, nginxConfig, 'utf-8');
console.log('✅ Nginx配置文件生成成功!');
console.log(`📁 保存路径: ${nginxConfigPath}`);
console.log();
console.log('📝 使用说明:');
console.log('1. 将生成的 nginx.conf 复制到 Nginx 配置目录');
console.log(`2. 修改配置中的路径spaRoot、staticRoot等为实际路径`);
console.log('3. 测试配置: nginx -t');
console.log('4. 重载Nginx: nginx -s reload');
console.log();
// 生成Apache .htaccess配置
const htaccessConfig = generateHtaccessConfig({
staticRoot: '/static-pages',
spaRoot: '/'
});
const htaccessPath = path.join(__dirname, '.htaccess');
fs.writeFileSync(htaccessPath, htaccessConfig, 'utf-8');
console.log('✅ Apache .htaccess 配置文件生成成功!');
console.log(`📁 保存路径: ${htaccessPath}`);
console.log();
console.log('📝 使用说明:');
console.log('1. 将生成的 .htaccess 复制到网站根目录');
console.log('2. 确保 Apache 启用了 mod_rewrite 模块');
console.log('3. 重启Apache服务');
console.log();
} catch (error) {
console.error('❌ 生成失败:', error);
process.exit(1);
}
}
// 运行
main();