Files
huishou/fix-nginx-static.mjs
T
2026-07-27 13:37:48 +08:00

151 lines
4.6 KiB
JavaScript

import { Client } from 'ssh2';
const conn = new Client();
conn.on('ready', () => {
console.log('=== 修改Nginx配置(添加静态文件支持) ===\n');
const config = `server {
listen 80;
server_name hs.yifenbao.cn;
return 301 https://\$server_name\$request_uri;
}
server {
listen 443 ssl http2;
server_name hs.yifenbao.cn;
ssl_certificate /www/server/panel/vhost/cert/hs.yifenbao.cn/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/hs.yifenbao.cn/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
root /www/huishou/dist/build/h5;
location ~* \\.(txt|html|js|css|png|jpg|jpeg|gif|svg|ico)$ {
expires 1h;
add_header Cache-Control "public, no-transform";
}
location /assets/ {
root /www/huishou/dist/build/h5;
expires 1h;
}
location /static/ {
root /www/huishou/dist/build/h5;
expires 1h;
}
location /admin/ {
root /www/huishou;
try_files \$uri \$uri/ /admin/index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:4000/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;
proxy_set_header X-Forwarded-Proto \$scheme;
}
location /icons/ {
proxy_pass http://127.0.0.1:4000/icons/;
proxy_set_header Host \$host;
}
location = / {
proxy_pass http://127.0.0.1:4000/;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
location / {
proxy_pass http://127.0.0.1:4000/;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
`;
conn.exec('cat > /www/server/panel/vhost/nginx/hs.yifenbao.cn.conf << "ENDOFCONFIG"\n' + config + '\nENDOFCONFIG', (err, stream) => {
if (err) {
console.log('写入配置失败:', err.message);
conn.end();
return;
}
stream.on('close', () => {
console.log('配置文件已更新');
conn.exec('/www/server/nginx/sbin/nginx -t', (err, stream2) => {
if (err) {
console.log('配置检查失败:', err.message);
conn.end();
return;
}
stream2.on('data', (data) => {
console.log('配置检查:', data.toString());
});
stream2.on('close', () => {
conn.exec('/www/server/nginx/sbin/nginx -s reload', (err, stream3) => {
if (err) {
console.log('重启失败:', err.message);
conn.end();
return;
}
stream3.on('close', () => {
console.log('Nginx已重启');
setTimeout(() => {
conn.exec('curl -s -k https://hs.yifenbao.cn/', (err, stream4) => {
if (err) {
console.log('测试失败:', err.message);
} else {
stream4.on('data', (data) => {
console.log('\n测试响应:', data.toString().substring(0, 300));
});
}
stream4.on('close', () => {
conn.end();
});
});
}, 2000);
});
});
});
});
});
});
});
conn.on('error', (err) => {
console.log('连接错误:', err.message);
});
conn.connect({
host: 'hs.yifenbao.cn',
port: 22,
username: 'root',
password: 'Torch1112'
});