const { Client } = require('ssh2'); const config = { host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' }; const conn = new Client(); conn.on('ready', async () => { console.log('SSH连接成功'); try { console.log('\n=== 检查虚拟主机配置 ==='); await executeCommand('ls -la /www/server/panel/vhost/nginx/'); console.log('\n=== 检查是否已有h5相关配置 ==='); await executeCommand('grep -r "h5" /www/server/panel/vhost/nginx/ 2>/dev/null | head -20'); console.log('\n=== 创建H5反向代理配置 ==='); const proxyConfig = `server { listen 80; server_name hs.yifenbao.cn; location /h5/ { proxy_pass http://127.0.0.1:4001/h5/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /api/ { proxy_pass http://127.0.0.1:4001/api/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } `; await executeCommand(`echo '${proxyConfig.replace(/'/g, "'\\''")}' > /www/server/panel/vhost/nginx/hs.yifenbao.cn.conf`); console.log('\n=== 测试Nginx配置 ==='); await executeCommand('/www/server/nginx/sbin/nginx -t'); console.log('\n=== 重新加载Nginx ==='); await executeCommand('/www/server/nginx/sbin/nginx -s reload'); console.log('\n=== 测试H5访问 ==='); await executeCommand('curl -s http://localhost/h5/'); } catch (error) { console.error('配置失败:', error.message); } finally { conn.end(); } }); function executeCommand(cmd) { return new Promise((resolve) => { conn.exec(cmd, (err, stream) => { if (err) { console.error('命令执行错误:', err.message); resolve(); return; } stream.stdout.on('data', (data) => process.stdout.write(data)); stream.stderr.on('data', (data) => process.stdout.write(data)); stream.on('close', () => resolve()); }); }); } conn.on('error', (err) => { console.error('SSH连接错误:', err.message); }); conn.connect(config);