diff --git a/00-init-tools.sh b/00-init-tools.sh index 3c0ecbd..6031643 100755 --- a/00-init-tools.sh +++ b/00-init-tools.sh @@ -8,7 +8,7 @@ # 3. 安装 Node.js / npm(前端依赖) # 4. 安装 Docker(基础设施容器) # 5. 安装 etcdctl(查看 Etcd 服务注册) -# 6. 安装 Nginx 并写入 PC/OpenIM 反代(本机 HTTP :80;外部 HTTPS 由 LB/CDN 终止) +# 6. 安装 Nginx 并写入 PC/CMS/Build-CMS/OpenIM 反代(本机 HTTP :80;外部 HTTPS 由 LB/CDN 终止) # 7. 写入 /etc/profile.d/deploy-env.sh(永久生效) # # 用法: @@ -309,10 +309,10 @@ _install_etcdctl() { } # ────────────────────────────────────────────────────────────────────────────── -# 6. Nginx — PC / OpenIM 统一入口(本机 HTTP :80) +# 6. Nginx — PC / CMS / Build-CMS / OpenIM 统一入口(本机 HTTP :80) # ────────────────────────────────────────────────────────────────────────────── _install_pc_nginx_proxy() { - step "安装 Nginx 并配置 OpenIM/PC 反代" + step "安装 Nginx 并配置 OpenIM/PC/CMS/Build-CMS 反代" if [[ "$(id -u)" -ne 0 ]]; then error " Nginx 安装需 root,请执行: sudo $0 nginx" @@ -376,10 +376,10 @@ _install_pc_nginx_proxy() { success " Nginx 反代已启用(配置: $conf_src)" 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 " PC 前端入口: Nginx :80 → 127.0.0.1:5173(请先执行 ./deploy-test/07-start-frontend.sh pc)" - info " 浏览器打开 https://${proxy_domain}/" + info " 外部 HTTPS 可由 LB/CDN/其它网关终止后转发到本机 :80" + info " PC 入口: https://${proxy_domain}/" + info " CMS 入口: http://cms-jack.imharry.work/" + info " Build CMS 入口: http://build-jack.imharry.work/" } # all 时非 root 则跳过(不中断 Go/Node/Docker/etcdctl) diff --git a/08-build-static-frontend.sh b/08-build-static-frontend.sh new file mode 100644 index 0000000..2ac2461 --- /dev/null +++ b/08-build-static-frontend.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash +# ============================================================================= +# 08-build-static-frontend.sh — 构建静态前端资源(dist) +# +# 用法: +# ./08-build-static-frontend.sh # 构建 cms + build-cms +# ./08-build-static-frontend.sh cms # 只构建 cms +# ./08-build-static-frontend.sh build-cms # 只构建 build-cms +# +# 产物目录: +# /app/cms/dist +# /app/build-cms/dist +# ============================================================================= +set -euo pipefail +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh" +init_dirs +init_script_log + +header "构建静态前端资源" + +declare -A FE_DIR=( + [cms]="cms" + [build-cms]="build-cms" +) + +declare -A FE_PM=( + [cms]="pnpm" + [build-cms]="pnpm" +) + +declare -A FE_BUILD_CMD=( + [cms]="pnpm run build" + [build-cms]="pnpm run build" +) + +FE_PROJECTS=(cms build-cms) +TARGET="${1:-all}" + +_valid_target() { + local target="$1" + for p in "${FE_PROJECTS[@]}"; do + [[ "$p" == "$target" ]] && return 0 + done + return 1 +} + +_build_fe() { + local name="$1" + local dir="$ROOT_DIR/${FE_DIR[$name]}" + local pm="${FE_PM[$name]}" + local cmd="${FE_BUILD_CMD[$name]}" + local dist_dir="$dir/dist" + + if [[ ! -d "$dir" ]]; then + error "$name 目录不存在: $dir" + return 1 + fi + + if [[ ! -d "$dir/node_modules" ]]; then + error "$name node_modules 不存在,请先执行: ./deploy-test/06-install-frontend.sh $name" + return 1 + fi + + if ! command -v "$pm" &>/dev/null; then + error "$name 需要 $pm,未安装" + return 1 + fi + + info "构建 ${BOLD}$name${NC} ..." + ( + cd "$dir" + rm -rf "$dist_dir" + # shellcheck disable=SC2086 + $cmd + ) + + if [[ ! -d "$dist_dir" ]]; then + error "$name 构建完成但 dist 不存在: $dist_dir" + return 1 + fi + + success "$name 构建完成 → $dist_dir" +} + +if [[ "$TARGET" == "all" ]]; then + FAILED=() + for proj in "${FE_PROJECTS[@]}"; do + _build_fe "$proj" || FAILED+=("$proj") + done + + if [[ ${#FAILED[@]} -gt 0 ]]; then + error "以下静态前端构建失败: ${FAILED[*]}" + exit 1 + fi +else + if ! _valid_target "$TARGET"; then + error "未知项目: $TARGET" + echo "可用: ${FE_PROJECTS[*]}" + exit 1 + fi + _build_fe "$TARGET" +fi + +echo "" +echo -e "${BOLD}下一步:${NC}" +echo " 1. 以 root 执行: sudo ./deploy-test/00-init-tools.sh nginx" +echo " 2. 域名访问:" +echo " - http://cms-jack.imharry.work/" +echo " - http://build-jack.imharry.work/" diff --git a/nginx/openim-pc-proxy.conf b/nginx/openim-pc-proxy.conf index e421c7b..c8bf8f4 100644 --- a/nginx/openim-pc-proxy.conf +++ b/nginx/openim-pc-proxy.conf @@ -126,3 +126,95 @@ server { proxy_send_timeout 86400s; } } + +server { + listen 80; + listen [::]:80; + server_name cms-jack.imharry.work; + + root /app/cms/dist; + index index.html; + + access_log /var/log/nginx/openim-cms-access.log openim_pc_gateway; + error_log /var/log/nginx/openim-cms-error.log warn; + + client_max_body_size 100m; + + 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; + } + + 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; + } + + location /api/admin/ { + proxy_pass http://127.0.0.1:10009/; + 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; + } + + location = /nginx-health { + access_log off; + default_type text/plain; + return 200 "ok\n"; + } + + location / { + try_files $uri $uri/ /index.html; + } +} + +server { + listen 80; + listen [::]:80; + server_name build-jack.imharry.work; + + root /app/build-cms/dist; + index index.html; + + access_log /var/log/nginx/openim-build-cms-access.log openim_pc_gateway; + error_log /var/log/nginx/openim-build-cms-error.log warn; + + client_max_body_size 100m; + + location /api/ { + proxy_pass http://127.0.0.1:8281; + 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; + } + + location = /nginx-health { + access_log off; + default_type text/plain; + return 200 "ok\n"; + } + + location / { + try_files $uri $uri/ /index.html; + } +}