From d6572d221708acb49c971bc7888d74a96ff20aa5 Mon Sep 17 00:00:00 2001 From: vet Date: Mon, 13 Apr 2026 23:28:06 +0700 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 00-init-tools.sh | 78 ++++++++++++++++++++++++++++++++++++-- 01-init-env.sh | 7 ++++ 07-start-frontend.sh | 7 ++++ README.md | 15 ++++++++ common.sh | 22 +++++++++++ nginx/openim-pc-proxy.conf | 74 ++++++++++++++++++++++++++++++++++++ 6 files changed, 200 insertions(+), 3 deletions(-) create mode 100644 nginx/openim-pc-proxy.conf diff --git a/00-init-tools.sh b/00-init-tools.sh index 595f9d6..2de3c24 100755 --- a/00-init-tools.sh +++ b/00-init-tools.sh @@ -7,14 +7,16 @@ # 2. 配置 GOPROXY(自动测速选最快节点) # 3. 安装 Node.js / npm(前端依赖) # 4. 安装 Docker(基础设施容器) -# 5. 写入 /etc/profile.d/deploy-env.sh(永久生效) +# 5. 安装 Nginx 并写入 PC/OpenIM 反代(:80 → 10001/10002/10008,见 nginx/openim-pc-proxy.conf) +# 6. 写入 /etc/profile.d/deploy-env.sh(永久生效) # # 用法: -# ./deploy-test/00-init-tools.sh # 安装全部 +# ./deploy-test/00-init-tools.sh # 安装全部(含 Nginx 反代) # ./deploy-test/00-init-tools.sh go # 只安装/配置 Go # ./deploy-test/00-init-tools.sh node # 只安装 Node.js # ./deploy-test/00-init-tools.sh docker # 只安装 Docker # ./deploy-test/00-init-tools.sh goproxy # 只配置 GOPROXY +# sudo ./deploy-test/00-init-tools.sh nginx # 只安装 Nginx 反代(需 root) # # 前置条件: root 或 sudo 权限,Ubuntu/Debian 系统 # @@ -241,6 +243,71 @@ _install_docker() { success " Docker $(docker --version | awk '{print $3}' | tr -d ',') 安装完成" } +# ────────────────────────────────────────────────────────────────────────────── +# 5. Nginx — PC / OpenIM 统一入口(HTTP :80,反代本机 10001/10002/10008) +# ────────────────────────────────────────────────────────────────────────────── +_install_pc_nginx_proxy() { + step "安装 Nginx 并配置 OpenIM/PC 反代" + + if [[ "$(id -u)" -ne 0 ]]; then + error " Nginx 安装需 root,请执行: sudo $0 nginx" + return 1 + fi + + local script_dir + script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + local conf_src="${script_dir}/nginx/openim-pc-proxy.conf" + local conf_name="openim-pc-proxy.conf" + + if [[ ! -f "$conf_src" ]]; then + error " 找不到配置: $conf_src" + return 1 + fi + + if ! _has nginx; then + if _has apt-get; then + apt-get update -y + apt-get install -y nginx + elif _has dnf; then + dnf install -y nginx + elif _has yum; then + yum install -y nginx + else + error " 未检测到 apt/dnf/yum,请先手动安装 nginx" + return 1 + fi + fi + + if [[ -d /etc/nginx/sites-available ]]; then + install -m 0644 "$conf_src" "/etc/nginx/sites-available/${conf_name}" + mkdir -p /etc/nginx/sites-enabled + ln -sf "/etc/nginx/sites-available/${conf_name}" "/etc/nginx/sites-enabled/${conf_name}" + if [[ -f /etc/nginx/sites-enabled/default ]]; then + rm -f /etc/nginx/sites-enabled/default + info " 已移除 sites-enabled/default,避免与 openim-pc-proxy 冲突" + fi + else + install -m 0644 "$conf_src" "/etc/nginx/conf.d/${conf_name}" + fi + + nginx -t + systemctl enable nginx 2>/dev/null || true + systemctl restart nginx + + success " Nginx 反代已启用(配置: $conf_src)" + info " 请放行 TCP 80;curl -sS http://127.0.0.1/nginx-health 应返回 ok" + info " .env.deploy-test 中 PC_BACKEND_ORIGIN=http://<公网IP>(与 DEPLOY_TEST_IP 一致)" +} + +# all 时非 root 则跳过(不中断 Go/Node/Docker) +_run_nginx_if_root() { + if [[ "$(id -u)" -eq 0 ]]; then + _install_pc_nginx_proxy + else + warn " 当前非 root,已跳过 Nginx。需要时在服务器上执行: sudo $0 nginx" + fi +} + # ────────────────────────────────────────────────────────────────────────────── # 执行 # ────────────────────────────────────────────────────────────────────────────── @@ -258,15 +325,19 @@ case "$TARGET" in docker) _install_docker ;; + nginx) + _install_pc_nginx_proxy + ;; all) _install_go _config_goproxy _install_node _install_docker + _run_nginx_if_root ;; *) error "未知目标: $TARGET" - echo "用法: $0 [all|go|goproxy|node|docker]" + echo "用法: $0 [all|go|goproxy|node|docker|nginx]" exit 1 ;; esac @@ -284,6 +355,7 @@ _has npm && echo " npm: $(npm --version)" || echo _has pnpm && echo " pnpm: $(pnpm --version)" || echo " pnpm: 未安装" _has yarn && echo " yarn: $(yarn --version)" || echo " yarn: 未安装" _has docker && echo " Docker: $(docker --version | awk '{print $3}' | tr -d ',')" || echo " Docker: 未安装" +_has nginx && echo " Nginx: $(nginx -v 2>&1 | sed 's/^nginx version: //')" || echo " Nginx: 未安装" echo "" echo -e "${BOLD}GOPROXY 配置:${NC}" _has go && go env GOPROXY || echo " (go 未安装)" diff --git a/01-init-env.sh b/01-init-env.sh index 90319fc..82b88e9 100755 --- a/01-init-env.sh +++ b/01-init-env.sh @@ -103,6 +103,13 @@ CF_CUSTOMER_CODE=huonxouoa55ent9z # 来源: livecloud/config/config.yml → cloud.tencent 段 TENCENT_SDK_APP_ID=20033091 TENCENT_SDK_SECRET_KEY=cceba44084aaa04f8c48a1858ffd5385875c3a5ec006d34278d9d3714b40e3b0 + +# ── PC 客户端(Vite dev)对接的后端公网地址(可选)─────────────────────────── +# 仅 IP:在服务器上先部署 Nginx 反代(sudo ./deploy-test/00-init-tools.sh nginx) +# 填写与下方 DEPLOY_TEST_IP 一致的 http:// 根地址,无末尾斜杠 +# ./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_BACKEND_ORIGIN=http://54.116.29.247 EOF success ".env.deploy-test 已写入: $ENV_FILE" diff --git a/07-start-frontend.sh b/07-start-frontend.sh index a3aced0..95906e3 100755 --- a/07-start-frontend.sh +++ b/07-start-frontend.sh @@ -8,6 +8,7 @@ # # 项目与端口: # pc → 默认 yarn dev:web :5173(无 Electron);本机要 Electron 时设 PC_ELECTRON=1 使用 yarn dev +# 后端地址:在 .env.deploy-test 设 PC_BACKEND_ORIGIN,启动时注入 VITE_*(不改 pc 目录) # meetingh5 → React + Vite :5188 # h5 → Vue + Vite :3003 # cms → UMI Max :8001 @@ -123,6 +124,9 @@ _start_fe() { fi info "启动 ${BOLD}$name${NC} ..." + if [[ "$name" == "pc" ]] && [[ -n "${PC_BACKEND_ORIGIN:-}" ]]; then + info " pc 后端 PC_BACKEND_ORIGIN=${PC_BACKEND_ORIGIN}(注入 VITE_*,不写 pc 目录)" + fi # 写日志分隔符 { @@ -133,6 +137,9 @@ _start_fe() { # 后台启动(带环境变量前缀) ( cd "$dir" + if [[ "$name" == "pc" ]]; then + pc_export_vite_backend_env + fi if [[ -n "$env_prefix" ]]; then # shellcheck disable=SC2086 nohup env $env_prefix $cmd >> "$logfile" 2>&1 & diff --git a/README.md b/README.md index c1642e0..737ca3f 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,21 @@ TENCENT_SDK_SECRET_KEY=xxx > - `ws` → meetingmsg 弹幕 WebSocket `:8000` > - `liveApi` → livestream 直播间 API `:8081` +### Nginx 反代(仅公网 IP,供 PC / 浏览器访问后端) + +无域名时,在**测试服务器**上部署 Nginx,统一监听 **HTTP :80**,把路径转发到本机 `openim-server` / `chat-api`: + +| 路径前缀 | 后端 | +|----------|------| +| `/api/im/` | `127.0.0.1:10002` | +| `/api/user/`、`/api/chat/` | `127.0.0.1:10008` | +| `/msg_gateway` | `127.0.0.1:10001`(WebSocket) | + +1. 服务器上已执行 `05-start.sh` 等,保证 `10001/10002/10008` 在监听。 +2. 仓库根目录执行:`sudo ./deploy-test/00-init-tools.sh nginx`(会安装 `nginx` 并写入配置 `deploy-test/nginx/openim-pc-proxy.conf`;亦已包含在 `00-init-tools.sh` 无参的 **all** 流程末尾,需 root)。 +3. 云安全组放行 **TCP 80**。 +4. `.env.deploy-test` 中设置 **`PC_BACKEND_ORIGIN=http://`**(无末尾 `/`),与 `DEPLOY_TEST_IP` 一致;再 `./deploy-test/07-start-frontend.sh pc` 启动 PC 时即注入 `VITE_*`。 + ### Docker 基础设施 | 服务 | 端口 | diff --git a/common.sh b/common.sh index 2b5f36f..ce06edc 100755 --- a/common.sh +++ b/common.sh @@ -225,3 +225,25 @@ print_svc_status() { # ── 所有后端服务名列表 ────────────────────────────────────────────────────────── ALL_SVCS=(openim-server chat-rpc admin-rpc chat-api admin-api meetingmsg livecloud livestream build-server) + +# ── PC 前端 Vite 环境(不写 pc 目录,由 07-start-frontend 在子 shell 内 export)──────── +# 依赖 .env.deploy-test / .env.deploy-local 中的 PC_BACKEND_ORIGIN(及可选 PC_VITE_*) +pc_export_vite_backend_env() { + local o="${PC_BACKEND_ORIGIN:-}" + o="${o%/}" + [[ -z "$o" ]] && return 0 + + local host + host=$(printf '%s' "$o" | sed -E 's#^https?://([^/]+).*#\1#') + [[ -z "$host" ]] && return 0 + + export VITE_BASE_DOMAIN="${PC_VITE_BASE_DOMAIN:-$host}" + export VITE_API_URL="${PC_VITE_API_URL:-$o/api/im}" + export VITE_CHAT_URL="${PC_VITE_CHAT_URL:-$o/api/chat}" + export VITE_USER_URL="${PC_VITE_USER_URL:-$o/api/user}" + if [[ "$o" == http://* ]]; then + export VITE_WS_URL="${PC_VITE_WS_URL:-ws://${host}/msg_gateway}" + else + export VITE_WS_URL="${PC_VITE_WS_URL:-wss://${host}/msg_gateway}" + fi +} diff --git a/nginx/openim-pc-proxy.conf b/nginx/openim-pc-proxy.conf new file mode 100644 index 0000000..083dca3 --- /dev/null +++ b/nginx/openim-pc-proxy.conf @@ -0,0 +1,74 @@ +# OpenIM / PC 客户端统一入口(HTTP :80) +# 后端均为本机 deploy-test 单机进程:openim-server、chat-api +# +# 安装:在测试服务器上以 root 执行 +# sudo ./deploy-test/00-init-tools.sh nginx +# +# 安全组 / 防火墙须放行 TCP 80;后端 10001/10002/10008 仅需本机访问(127.0.0.1) + +server { + listen 80; + listen [::]:80; + server_name _; + + client_max_body_size 100m; + + # OpenIM HTTP API → openim-server :10002 + location /api/im/ { + proxy_pass http://127.0.0.1:10002/; + 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_read_timeout 300s; + proxy_send_timeout 300s; + } + + # 用户 / 登录相关 → chat-api :10008(与 im-cms-nginx 一致) + location /api/user/ { + proxy_pass http://127.0.0.1:10008/; + 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_read_timeout 300s; + proxy_send_timeout 300s; + } + + # Chat API → chat-api :10008 + location /api/chat/ { + proxy_pass http://127.0.0.1:10008/; + 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_read_timeout 300s; + proxy_send_timeout 300s; + } + + # MsgGateway WebSocket → openim-server :10001 + location /msg_gateway { + proxy_pass http://127.0.0.1:10001; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + 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_cache_bypass $http_upgrade; + proxy_buffering off; + proxy_read_timeout 86400s; + proxy_send_timeout 86400s; + } + + # 可选:健康检查 + location = /nginx-health { + access_log off; + default_type text/plain; + return 200 "ok\n"; + } +}