From 868a22e940e76e1eefb20ea1fc47bee09f5659f1 Mon Sep 17 00:00:00 2001 From: vet Date: Mon, 20 Apr 2026 10:45:06 +0700 Subject: [PATCH] test --- 00-init-tools.sh | 12 ++++++------ 01-init-env.sh | 4 ++-- 07-start-frontend.sh | 10 +++++++--- README.md | 12 ++++++------ common.sh | 27 +++++++++++++++++++++++++-- nginx/openim-pc-proxy.conf | 3 ++- pc-sdk-probe.sh | 8 ++++---- status.sh | 6 +++--- 8 files changed, 55 insertions(+), 27 deletions(-) diff --git a/00-init-tools.sh b/00-init-tools.sh index 87305f4..39ea8a0 100755 --- a/00-init-tools.sh +++ b/00-init-tools.sh @@ -7,7 +7,7 @@ # 2. 配置 GOPROXY(自动测速选最快节点) # 3. 安装 Node.js / npm(前端依赖) # 4. 安装 Docker(基础设施容器) -# 5. 安装 Nginx 并写入 PC/OpenIM 反代(域名 HTTP :80) +# 5. 安装 Nginx 并写入 PC/OpenIM 反代(本机 HTTP :80;外部 HTTPS 由 LB/CDN 终止) # 6. 写入 /etc/profile.d/deploy-env.sh(永久生效) # # 用法: @@ -244,7 +244,7 @@ _install_docker() { } # ────────────────────────────────────────────────────────────────────────────── -# 5. Nginx — PC / OpenIM 统一入口(HTTP :80) +# 5. Nginx — PC / OpenIM 统一入口(本机 HTTP :80) # ────────────────────────────────────────────────────────────────────────────── _install_pc_nginx_proxy() { step "安装 Nginx 并配置 OpenIM/PC 反代" @@ -310,10 +310,10 @@ _install_pc_nginx_proxy() { systemctl restart nginx success " Nginx 反代已启用(配置: $conf_src)" - info " 请放行 TCP 80;curl -sS -H 'Host: ${proxy_domain}' http://127.0.0.1/nginx-health 应返回 ok" - info " HTTP :80 会代理 PC dev server + API + WebSocket" - info " 建议 .env.deploy-test 设置 PC_BACKEND_ORIGIN=http://${proxy_domain}" - info " 浏览器打开 http://${proxy_domain}/" + info " 本机 Nginx 仅监听 TCP 80;curl -sS -H 'Host: ${proxy_domain}' http://127.0.0.1/nginx-health 应返回 ok" + info " 外部推荐入口 https://${proxy_domain} 应由 LB/CDN/其它网关终止 HTTPS 后转发到本机 :80" + info " 建议 .env.deploy-test 设置 PC_BACKEND_ORIGIN=https://${proxy_domain}" + info " 浏览器打开 https://${proxy_domain}/" } # all 时非 root 则跳过(不中断 Go/Node/Docker) diff --git a/01-init-env.sh b/01-init-env.sh index 9da2665..802c0db 100755 --- a/01-init-env.sh +++ b/01-init-env.sh @@ -106,11 +106,11 @@ TENCENT_SDK_SECRET_KEY=cceba44084aaa04f8c48a1858ffd5385875c3a5ec006d34278d9d3714 # ── PC 客户端(Vite dev)对接的后端公网地址(可选)─────────────────────────── # 通过域名访问 Nginx 反代(sudo ./deploy-test/00-init-tools.sh nginx)。 -# 填写 HTTP 根地址,无末尾斜杠;Nginx 会将 / 代理到 pc :5173,并代理 API/WS。 +# 填写 HTTPS 根地址,无末尾斜杠;Nginx 会将 / 代理到 pc :5173,并代理 API/WS。 # ./deploy-test/07-start-frontend.sh 启动 pc 时会 export VITE_*,覆盖 pc/.env,无需改 pc 目录 # 若某路径与网关不一致,可单独覆盖:PC_VITE_API_URL / PC_VITE_WS_URL / PC_VITE_CHAT_URL / PC_VITE_USER_URL PC_PROXY_DOMAIN=cms-jack.imharry.work -PC_BACKEND_ORIGIN=http://cms-jack.imharry.work +PC_BACKEND_ORIGIN=https://cms-jack.imharry.work EOF success ".env.deploy-test 已写入: $ENV_FILE" diff --git a/07-start-frontend.sh b/07-start-frontend.sh index ca81524..b385239 100755 --- a/07-start-frontend.sh +++ b/07-start-frontend.sh @@ -30,8 +30,7 @@ if [[ -f "$ENV_FILE" ]]; then set +a fi DEPLOY_TEST_IP="${DEPLOY_TEST_IP:-127.0.0.1}" -PC_PROXY_DOMAIN="${PC_PROXY_DOMAIN:-cms-jack.imharry.work}" -PC_BACKEND_ORIGIN="${PC_BACKEND_ORIGIN:-http://${PC_PROXY_DOMAIN}}" +normalize_pc_proxy_env init_script_log # ← 脚本执行日志 @@ -104,6 +103,11 @@ _start_fe() { # 已在运行 if [[ -f "$pidfile" ]] && kill -0 "$(cat "$pidfile")" 2>/dev/null; then warn "$name 已在运行 (PID=$(cat "$pidfile")),跳过" + if [[ "$name" == "pc" ]]; then + warn " Vite 环境变量只在启动时读取;若仍请求旧地址,请执行: ./deploy-test/stop-frontend.sh pc && ./deploy-test/07-start-frontend.sh pc" + pc_export_vite_backend_env + pc_print_vite_backend_env + fi return 0 fi @@ -200,7 +204,7 @@ if [[ "$TARGET" == "all" ]]; then echo "" echo -e "${BOLD}访问地址:${NC}" - echo " PC: ${PC_BACKEND_ORIGIN%/}/ (Nginx HTTP :80 → pc :5173;默认 yarn dev:web)" + echo " PC: ${PC_BACKEND_ORIGIN%/}/ (外部 HTTPS → 本机 Nginx :80 → pc :5173;默认 yarn dev:web)" echo " PC 直连调试: http://${DEPLOY_TEST_IP}:5173 (绕过 Nginx;Electron 用 PC_ELECTRON=1 或 pc 目录内 yarn dev)" echo " H5: http://${DEPLOY_TEST_IP}:3003" echo " CMS: http://${DEPLOY_TEST_IP}:8001" diff --git a/README.md b/README.md index 4117c7d..b2294dd 100644 --- a/README.md +++ b/README.md @@ -283,9 +283,9 @@ TENCENT_SDK_SECRET_KEY=xxx > - `ws` → meetingmsg 弹幕 WebSocket `:8000` > - `liveApi` → livestream 直播间 API `:8888` -### Nginx 反代(域名 HTTP 入口,供 PC / 浏览器访问后端) +### Nginx 反代(域名 HTTPS 入口,供 PC / 浏览器访问后端) -在**测试服务器**上部署 Nginx,通过域名 `cms-jack.imharry.work` 的 HTTP `:80` 统一反代 PC Vite dev server、OpenIM API、chat-api 与 MsgGateway WebSocket;不启用 HTTPS。 +在**测试服务器**上部署 Nginx,本机 Nginx 仅监听 HTTP `:80`,统一反代 PC Vite dev server、OpenIM API、chat-api 与 MsgGateway WebSocket。外部访问仍使用 `https://cms-jack.imharry.work`,HTTPS 由外层 LB/CDN/其它网关终止后转发到本机 `:80`。 | 路径前缀 | 后端 | |----------|------| @@ -297,11 +297,11 @@ TENCENT_SDK_SECRET_KEY=xxx 1. 服务器上已执行 `05-start.sh` 等,保证 `10001/10002/10008` 在监听。 2. 域名 `cms-jack.imharry.work` 已解析到测试服务器公网 IP。 3. 仓库根目录执行:`sudo ./deploy-test/00-init-tools.sh nginx`(会安装 `nginx`,并写入配置 `deploy-test/nginx/openim-pc-proxy.conf`;亦已包含在 `00-init-tools.sh` 无参的 **all** 流程末尾,需 root)。 -4. 云安全组放行 **TCP 80**。 -5. `.env.deploy-test` 中设置 **`PC_BACKEND_ORIGIN=http://cms-jack.imharry.work`**(无末尾 `/`);再 `./deploy-test/07-start-frontend.sh pc` 启动 PC 时即注入 `VITE_*`。 -6. 浏览器打开 **`http://cms-jack.imharry.work/`**。 +4. 测试服务器本机/安全组放行 **TCP 80**;外层 LB/CDN/网关对公网提供 HTTPS `443` 并转发到本机 `80`。 +5. `.env.deploy-test` 中设置 **`PC_BACKEND_ORIGIN=https://cms-jack.imharry.work`**(无末尾 `/`);再 `./deploy-test/07-start-frontend.sh pc` 启动 PC 时即注入 `VITE_*`。 +6. 浏览器打开 **`https://cms-jack.imharry.work/`**。 -**域名访问**:本配置中 `openim-pc-proxy` 使用 `server_name cms-jack.imharry.work`,并监听 `80`;`GET /` 直接代理 PC Vite。`00-init-tools.sh nginx` 会禁用 `sites-enabled/default`,并尝试去掉 `sites-available/default` 里的 `default_server`,避免默认站点抢占 `:80`。若机上还有其它站点也写了 `default_server`,`nginx -t` 会报错,需在该站点配置中删除 `default_server`(保留 `listen 80;` 即可)。 +**域名访问**:本配置中 `openim-pc-proxy` 使用 `server_name cms-jack.imharry.work`,只监听本机 `80`;推荐外部 `GET /` 走 `https://cms-jack.imharry.work/`,由外层 HTTPS 入口转发到本机 `80` 后代理 PC Vite。`00-init-tools.sh nginx` 会禁用 `sites-enabled/default`,并尝试去掉 `sites-available/default` 里的 `default_server`,避免默认站点抢占 `:80`。若机上还有其它站点也写了 `default_server`,`nginx -t` 会报错,需在该站点配置中删除 `default_server`(保留 `listen 80;` 即可)。 ### Docker 基础设施 diff --git a/common.sh b/common.sh index 8848b5c..b114a82 100755 --- a/common.sh +++ b/common.sh @@ -226,9 +226,32 @@ print_svc_status() { # ── 所有后端服务名列表 ────────────────────────────────────────────────────────── ALL_SVCS=(openim-server chat-rpc admin-rpc chat-api admin-api meetingmsg livecloud livestream build-server) +# ── PC 反代入口环境归一化 ─────────────────────────────────────────────────────── +normalize_pc_proxy_env() { + PC_PROXY_DOMAIN="${PC_PROXY_DOMAIN:-cms-jack.imharry.work}" + local default_origin="https://${PC_PROXY_DOMAIN}" + local origin="${PC_BACKEND_ORIGIN:-}" + origin="${origin%/}" + + if [[ -z "$origin" ]]; then + PC_BACKEND_ORIGIN="$default_origin" + elif [[ -n "${DEPLOY_TEST_IP:-}" ]] && [[ "$origin" =~ ^https?://${DEPLOY_TEST_IP}(:[0-9]+)?$ ]]; then + warn " 检测到旧 PC_BACKEND_ORIGIN=${origin},已切换为 ${default_origin}" + PC_BACKEND_ORIGIN="$default_origin" + elif [[ "$origin" == "http://${PC_PROXY_DOMAIN}" ]]; then + warn " 检测到 HTTP PC_BACKEND_ORIGIN=${origin},已切换为 ${default_origin}" + PC_BACKEND_ORIGIN="$default_origin" + else + PC_BACKEND_ORIGIN="$origin" + fi + + export PC_PROXY_DOMAIN PC_BACKEND_ORIGIN +} + # ── PC 前端 Vite 环境(不写 pc 目录,由 07-start-frontend 在子 shell 内 export)──────── # 依赖 .env.deploy-test / .env.deploy-local 中的 PC_BACKEND_ORIGIN(及可选 PC_VITE_*) pc_export_vite_backend_env() { + normalize_pc_proxy_env local o="${PC_BACKEND_ORIGIN:-}" o="${o%/}" [[ -z "$o" ]] && return 0 @@ -262,7 +285,7 @@ pc_print_vite_backend_env() { info " VITE_ADMIN_URL=${VITE_ADMIN_URL:-${PC_VITE_ADMIN_URL:-}}" if [[ "$PC_BACKEND_ORIGIN" == http://* ]] && [[ "$PC_BACKEND_ORIGIN" != http://127.0.0.1* ]] && [[ "$PC_BACKEND_ORIGIN" != http://localhost* ]]; then - info " PC 使用 HTTP 公网入口: ${PC_BACKEND_ORIGIN}" + warn " PC_BACKEND_ORIGIN 当前是 HTTP: ${PC_BACKEND_ORIGIN};推荐使用 https://${VITE_BASE_DOMAIN}" fi } @@ -283,7 +306,7 @@ pc_check_nginx_gateway() { success " Nginx 网关可达: ${o}/nginx-health" else warn " Nginx 网关不可达: ${o}/nginx-health" - warn " PC WebSocket 默认连 ${VITE_WS_URL:-ws:///msg_gateway};请确认已执行 sudo ./deploy-test/00-init-tools.sh nginx,并放行 TCP 80" + warn " PC WebSocket 默认连 ${VITE_WS_URL:-wss:///msg_gateway};请确认已执行 sudo ./deploy-test/00-init-tools.sh nginx,本机 Nginx 放行 TCP 80,外层 HTTPS 入口放行 TCP 443" fi pc_probe_msg_gateway "$o" diff --git a/nginx/openim-pc-proxy.conf b/nginx/openim-pc-proxy.conf index e31ea56..ddb5a9d 100644 --- a/nginx/openim-pc-proxy.conf +++ b/nginx/openim-pc-proxy.conf @@ -10,7 +10,8 @@ # 返回 Access-Control-Allow-Origin: *。若在此再用 add_header 追加 $http_origin,浏览器会收到 # 「*, http://IP:5173」两个值并报错。故本配置不在 Nginx 层添加 CORS,预检 OPTIONS 也交给上游处理。 # -# 访问入口:http://cms-jack.imharry.work/ +# 推荐外部访问入口:https://cms-jack.imharry.work/ +# 本 Nginx 仅监听 HTTP :80;HTTPS 由外层 LB/CDN/网关终止后转发到本机 :80。 map $http_upgrade $connection_upgrade { default upgrade; diff --git a/pc-sdk-probe.sh b/pc-sdk-probe.sh index 70599ca..ac2de42 100755 --- a/pc-sdk-probe.sh +++ b/pc-sdk-probe.sh @@ -212,7 +212,7 @@ cat <<'EOF' userID = userID || await window.__deployTestReadLocalForage("IM_USERID"); token = token || await window.__deployTestReadLocalForage("IM_TOKEN"); const operationID = `deploy-test-${Date.now()}`; - const gatewayOrigin = window.location.protocol === "https:" ? window.location.origin : "http://54.116.29.247"; + const gatewayOrigin = window.location.origin || "https://cms-jack.imharry.work"; const config = { platformID: 5, apiAddr: apiAddr || `${gatewayOrigin}/api/im`, @@ -297,8 +297,8 @@ await window.__deployTestResetBrowserStorage() await window.__deployTestManualInitDB() 清理后按这个顺序重测: -1. 关闭其它 http://cms-jack.imharry.work/ / http://54.116.29.247:5173 标签页 -2. 打开 http://cms-jack.imharry.work/ 并刷新页面 +1. 关闭其它 https://cms-jack.imharry.work/ / http://54.116.29.247:5173 标签页 +2. 打开 https://cms-jack.imharry.work/ 并刷新页面 3. 重新粘贴本脚本输出的整段 JS(刷新会清掉已安装的 probe) 4. 再登录 5. 观察 counts 是否从 0 变 1,以及 /var/log/nginx/openim-pc-proxy-access.log 是否出现 /msg_gateway @@ -307,5 +307,5 @@ await window.__deployTestManualInitDB() await window.__deployTestAuthState() -HTTP 域名入口下 isSecureContext / crossOriginIsolated 通常为 false;重点观察 SDK 方法是否存在、login 是否返回成功,以及 /msg_gateway 是否建立连接。 +HTTPS 域名入口下 isSecureContext / crossOriginIsolated 应为 true,SharedArrayBuffer 应为 "function";同时观察 SDK 方法是否存在、login 是否返回成功,以及 /msg_gateway 是否建立连接。 EOF diff --git a/status.sh b/status.sh index 813f2a8..0968679 100755 --- a/status.sh +++ b/status.sh @@ -15,8 +15,8 @@ if [[ -f "$ENV_FILE" ]]; then source "$ENV_FILE" set +a fi -PC_PROXY_DOMAIN="${PC_PROXY_DOMAIN:-cms-jack.imharry.work}" -PC_BACKEND_ORIGIN="${PC_BACKEND_ORIGIN:-http://${PC_PROXY_DOMAIN}}" +DEPLOY_TEST_IP="${DEPLOY_TEST_IP:-127.0.0.1}" +normalize_pc_proxy_env header "服务运行状态" @@ -76,7 +76,7 @@ if command -v curl &>/dev/null && [[ -n "${PC_BACKEND_ORIGIN:-}" ]]; then echo " PC Chat API: ${PC_VITE_CHAT_URL:-${PC_BACKEND_ORIGIN}/api/chat}" echo " PC Admin API: ${PC_VITE_ADMIN_URL:-${PC_BACKEND_ORIGIN}/api/admin}" echo " PC WebSocket: ${PC_VITE_WS_URL:-$default_ws_url}" - echo " PC 页面入口: ${PC_BACKEND_ORIGIN}/ (Nginx HTTP :80 → pc :5173)" + echo " PC 页面入口: ${PC_BACKEND_ORIGIN}/ (外部 HTTPS → 本机 Nginx :80 → pc :5173)" echo " Nginx 日志: /var/log/nginx/openim-pc-proxy-access.log" pc_probe_msg_gateway "$PC_BACKEND_ORIGIN" else