Răsfoiți Sursa

fix: nginx 后端地址改为运行时可配置(BACKEND_URL),支持独立部署

kinglee 2 săptămâni în urmă
părinte
comite
119e931af0
5 a modificat fișierele cu 52 adăugiri și 10 ștergeri
  1. 4 1
      Dockerfile
  2. 20 9
      README.md
  3. 2 0
      docker-compose.yml
  4. 8 0
      docker-entrypoint.sh
  5. 18 0
      nginx.conf.template

+ 4 - 1
Dockerfile

@@ -12,6 +12,9 @@ ENV ENABLE_PLAYGROUND=$ENABLE_PLAYGROUND
 RUN pnpm build
 RUN pnpm build
 
 
 FROM nginx:alpine
 FROM nginx:alpine
+RUN apk add --no-cache gettext
 COPY --from=builder /app/dist /usr/share/nginx/html
 COPY --from=builder /app/dist /usr/share/nginx/html
-COPY nginx.conf /etc/nginx/conf.d/default.conf
+COPY nginx.conf.template /etc/nginx/conf.d/default.conf.template
+COPY docker-entrypoint.sh /docker-entrypoint.d/99-envsubst.sh
+RUN chmod +x /docker-entrypoint.d/99-envsubst.sh
 EXPOSE 80
 EXPOSE 80

+ 20 - 9
README.md

@@ -84,9 +84,18 @@ ENABLE_PLAYGROUND=false
 docker compose --env-file .env.prod up -d --build
 docker compose --env-file .env.prod up -d --build
 ```
 ```
 
 
-### Nginx 代理规则
+### 配置后端代理
 
 
-容器内 Nginx 默认将 `/v1`、`/v2`、`/auth`、`/version`、`/proxy`、`/update`、`/cli`、`/grafana` 等路径代理到后端 `backend:80`,部署时确保 backend 服务与前端容器在同一 Docker 网络中。
+通过 `BACKEND_URL` 环境变量指定后端服务地址:
+
+```env
+UI_PORT=8080
+BACKEND_URL=http://your-backend-host:80
+APP_TITLE=成都网讯MaaS底座
+ENABLE_PLAYGROUND=false
+```
+
+容器内 Nginx 会自动将 `/v1`、`/v2`、`/auth`、`/version`、`/proxy`、`/update`、`/cli`、`/grafana` 等路径代理到 `BACKEND_URL` 指定的后端地址。
 
 
 ## 项目结构
 ## 项目结构
 
 
@@ -116,17 +125,19 @@ src/
   services/             # API 服务层
   services/             # API 服务层
 Dockerfile              # 多阶段构建(Node 18 构建 + Nginx 部署)
 Dockerfile              # 多阶段构建(Node 18 构建 + Nginx 部署)
 docker-compose.yml      # 容器编排配置
 docker-compose.yml      # 容器编排配置
-nginx.conf              # Nginx 反向代理配置
+nginx.conf.template     # Nginx 反向代理模板(运行时通过 envsubst 渲染)
+docker-entrypoint.sh    # Nginx 容器入口脚本
 ```
 ```
 
 
 ## 环境变量
 ## 环境变量
 
 
-| 变量名              | 说明                        | 默认值           |
-| ------------------- | --------------------------- | ---------------- |
-| `APP_TITLE`         | 应用标题(左上角显示)      | 成都网讯MaaS底座 |
-| `ENABLE_PLAYGROUND` | 是否启用 Playground 菜单    | true             |
-| `PROXY_HOST`        | 开发环境后端代理地址        | -                |
-| `UI_PORT`           | Docker 部署时的容器映射端口 | 8080             |
+| 变量名              | 说明                        | 默认值                |
+| ------------------- | --------------------------- | --------------------- |
+| `APP_TITLE`         | 应用标题(左上角显示)      | 成都网讯MaaS底座      |
+| `ENABLE_PLAYGROUND` | 是否启用 Playground 菜单    | true                  |
+| `PROXY_HOST`        | 开发环境后端代理地址        | -                     |
+| `UI_PORT`           | Docker 部署时的容器映射端口 | 8080                  |
+| `BACKEND_URL`       | 后端服务地址                | http://localhost:8080 |
 
 
 ## 其他命令
 ## 其他命令
 
 

+ 2 - 0
docker-compose.yml

@@ -7,6 +7,8 @@ services:
         APP_TITLE: ${APP_TITLE:-成都网讯MaaS底座}
         APP_TITLE: ${APP_TITLE:-成都网讯MaaS底座}
         ENABLE_PLAYGROUND: ${ENABLE_PLAYGROUND:-true}
         ENABLE_PLAYGROUND: ${ENABLE_PLAYGROUND:-true}
     image: maas-base-ui:latest
     image: maas-base-ui:latest
+    environment:
+      - BACKEND_URL=${BACKEND_URL:-http://localhost:8080}
     ports:
     ports:
       - "${UI_PORT:-8080}:80"
       - "${UI_PORT:-8080}:80"
     restart: unless-stopped
     restart: unless-stopped

+ 8 - 0
docker-entrypoint.sh

@@ -0,0 +1,8 @@
+#!/bin/sh
+set -e
+
+export BACKEND_URL="${BACKEND_URL:-http://backend:80}"
+
+envsubst '${BACKEND_URL}' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf
+
+exec nginx -g 'daemon off;'

+ 18 - 0
nginx.conf.template

@@ -0,0 +1,18 @@
+server {
+    listen 80;
+    root /usr/share/nginx/html;
+    index index.html;
+
+    location / {
+        try_files $uri $uri/ /index.html;
+    }
+
+    location ~ ^/(v1|v2|auth|version|proxy|update|cli|grafana) {
+        proxy_pass ${BACKEND_URL};
+        proxy_set_header Host $host;
+        proxy_set_header X-Real-IP $remote_addr;
+        proxy_http_version 1.1;
+        proxy_set_header Upgrade $http_upgrade;
+        proxy_set_header Connection "upgrade";
+    }
+}