From 09aa1ee35d67a68f343fc8cbc12ae1419c06881d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:29:44 +0000 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20V3=20nginx=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=B2=BE=E7=A1=AE=E5=AE=9A=E4=BD=8D=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3fragile=20sed=E6=A8=A1=E5=BC=8F=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=A2=84=E6=A3=80=E6=9F=A5=E5=92=8C=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:deploy_v3() 使用 sed '/^}/i\' 在多server块配置中重复注入V3 location块 导致nginx语法错误,switch_v3切换时nginx -t验证失败回滚 修复: - 新增 inject_v3_nginx_block() 精确定位最后一个顶层} - 新增 remove_v3_nginx_blocks() 清理错误注入 - 新增 validate_nginx() 显示实际错误信息 - switch_v3() 添加预检查和自动修复已损坏配置 - deploy_v3() 使用安全注入并报告nginx验证失败 Agent-Logs-Url: https://github.com/qinfendebingshuo/guanghulab/sessions/d77eb92f-55c9-4cb8-92db-2c4ce8754a8a Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com> --- server/proxy/deploy-brain-proxy.sh | 146 +++++++++++++++++++++++------ 1 file changed, 119 insertions(+), 27 deletions(-) diff --git a/server/proxy/deploy-brain-proxy.sh b/server/proxy/deploy-brain-proxy.sh index fd6766cf..19866aca 100644 --- a/server/proxy/deploy-brain-proxy.sh +++ b/server/proxy/deploy-brain-proxy.sh @@ -487,6 +487,78 @@ list_users() { node "$PROXY_DIR/service/user-manager.js" list } +# ── V3 Nginx配置辅助函数 ────────────────────── + +# 安全注入V3 location块到nginx配置 +# 仅在最后一个顶层 } 之前插入(主server块的结束位置) +# 避免旧 sed '/^}/i\' 模式在多server块配置中重复注入的问题 +inject_v3_nginx_block() { + local conf="$1" + if grep -q "proxy-v3" "$conf" 2>/dev/null; then + return 0 # 已存在 + fi + + local last_brace + last_brace=$(grep -n '^}' "$conf" | tail -1 | cut -d: -f1) + if [ -z "$last_brace" ] || [ "$last_brace" -le 1 ]; then + echo " ❌ 未找到有效的server块结束位置" + return 1 + fi + + { + head -n $((last_brace - 1)) "$conf" + cat <<'V3BLOCK' + + # ─── 光湖语言世界V3 (端口 3805) ─── + location /api/proxy-v3/ { + proxy_pass http://127.0.0.1:3805/; + proxy_http_version 1.1; + 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; + proxy_connect_timeout 10s; + proxy_read_timeout 30s; + proxy_send_timeout 30s; + add_header X-Content-Type-Options nosniff always; + add_header X-Frame-Options DENY always; + add_header Cache-Control "no-store, no-cache, must-revalidate" always; + } +V3BLOCK + tail -n +${last_brace} "$conf" + } > "${conf}.v3-tmp" + + if [ -s "${conf}.v3-tmp" ]; then + mv "${conf}.v3-tmp" "$conf" + return 0 + else + rm -f "${conf}.v3-tmp" + echo " ❌ V3注入生成文件为空" + return 1 + fi +} + +# 移除所有V3 location块(用于修复错误注入) +remove_v3_nginx_blocks() { + local conf="$1" + # 删除从V3注释行到该location块闭合}的所有内容 + sed -i '/# ─── 光湖语言世界V3/,/^[[:space:]]*}/d' "$conf" + # 清理多余空行 + sed -i '/^$/N;/^\n$/d' "$conf" +} + +# 验证nginx配置并返回错误信息 +validate_nginx() { + local result + result=$(nginx -t 2>&1) + if echo "$result" | grep -q "successful"; then + return 0 + else + echo "$result" | grep -E 'emerg|error' | head -5 + return 1 + fi +} + # ── deploy-v3: 部署V3生产环境 ────────────────── # V2继续运行·V3独立启动·/api/proxy-v3/ deploy_v3() { @@ -515,32 +587,27 @@ deploy_v3() { fi done - if [ -n "$NGINX_CONF" ] && ! grep -q "proxy-v3" "$NGINX_CONF" 2>/dev/null; then - echo " 添加V3路径..." - sed -i '/^}/i\ - # ─── 光湖语言世界V3 (端口 3805) ───\ - location /api/proxy-v3/ {\ - proxy_pass http://127.0.0.1:3805/;\ - proxy_http_version 1.1;\ - 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;\ - proxy_connect_timeout 10s;\ - proxy_read_timeout 30s;\ - proxy_send_timeout 30s;\ - add_header X-Content-Type-Options nosniff always;\ - add_header X-Frame-Options DENY always;\ - add_header Cache-Control "no-store, no-cache, must-revalidate" always;\ - }' "$NGINX_CONF" || true - echo " ✅ V3路径已添加" - else - echo " proxy-v3配置已存在或Nginx配置未找到" - fi + if [ -n "$NGINX_CONF" ]; then + if ! grep -q "proxy-v3" "$NGINX_CONF" 2>/dev/null; then + echo " 添加V3路径..." + if inject_v3_nginx_block "$NGINX_CONF"; then + echo " ✅ V3路径已添加" + else + echo " ❌ V3路径注入失败" + fi + else + echo " proxy-v3配置已存在" + fi - if nginx -t 2>/dev/null; then - nginx -s reload 2>/dev/null || systemctl reload nginx 2>/dev/null || true - echo " ✅ Nginx已重载" + if validate_nginx; then + nginx -s reload 2>/dev/null || systemctl reload nginx 2>/dev/null || true + echo " ✅ Nginx已重载" + else + echo " ❌ Nginx配置验证失败,V3 PM2进程已启动但Nginx未更新" + echo " 请手动检查: nginx -t" + fi + else + echo " ⚠️ 未找到Nginx配置文件,跳过V3路径注入" fi echo "" @@ -593,7 +660,32 @@ switch_v3() { exit 1 fi - # 备份当前Nginx配置 + # 预检查: 验证当前Nginx配置是否有效 + if ! validate_nginx; then + echo " ⚠️ 当前Nginx配置存在问题,尝试自动修复..." + + # 如果V3注入导致的问题(旧sed '/^}/i'在多server块配置中重复注入) + if grep -q "光湖语言世界V3" "$NGINX_CONF" 2>/dev/null; then + echo " 🔧 修复V3 location块注入错误..." + cp "$NGINX_CONF" "${NGINX_CONF}.pre-repair" + remove_v3_nginx_blocks "$NGINX_CONF" + inject_v3_nginx_block "$NGINX_CONF" + + if validate_nginx; then + echo " ✅ V3注入已修复" + nginx -s reload 2>/dev/null || systemctl reload nginx 2>/dev/null || true + else + echo " ❌ 自动修复失败,恢复原配置" + cp "${NGINX_CONF}.pre-repair" "$NGINX_CONF" + exit 1 + fi + else + echo " ❌ Nginx配置错误非V3相关,请手动检查: nginx -t" + exit 1 + fi + fi + + # 备份当前(已验证有效的)Nginx配置 cp "$NGINX_CONF" "${NGINX_CONF}.v2-backup" echo " ✅ 已备份Nginx配置到 ${NGINX_CONF}.v2-backup" @@ -606,7 +698,7 @@ switch_v3() { fi # 重载Nginx - if nginx -t 2>/dev/null; then + if validate_nginx; then nginx -s reload 2>/dev/null || systemctl reload nginx 2>/dev/null || true echo " ✅ Nginx已重载" else From 03234032e092eb15c02ff597aa1be61b64f58785 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 5 Apr 2026 18:31:34 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?trailing=20echo=EF=BC=8C=E4=BF=AE=E6=AD=A3blank=20line=E6=B8=85?= =?UTF-8?q?=E7=90=86sed=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agent-Logs-Url: https://github.com/qinfendebingshuo/guanghulab/sessions/d77eb92f-55c9-4cb8-92db-2c4ce8754a8a Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com> --- server/proxy/deploy-brain-proxy.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/proxy/deploy-brain-proxy.sh b/server/proxy/deploy-brain-proxy.sh index 19866aca..3faf2858 100644 --- a/server/proxy/deploy-brain-proxy.sh +++ b/server/proxy/deploy-brain-proxy.sh @@ -543,8 +543,8 @@ remove_v3_nginx_blocks() { local conf="$1" # 删除从V3注释行到该location块闭合}的所有内容 sed -i '/# ─── 光湖语言世界V3/,/^[[:space:]]*}/d' "$conf" - # 清理多余空行 - sed -i '/^$/N;/^\n$/d' "$conf" + # 清理连续空行(保留最多一个空行) + sed -i '/^$/N;/^\n$/D' "$conf" } # 验证nginx配置并返回错误信息