From d33deed4c530ecf8a11f3d24c622679a60bb42c2 Mon Sep 17 00:00:00 2001 From: anygen-build-bot Date: Thu, 12 Feb 2026 10:40:23 +0000 Subject: [PATCH] chore(proxy): expose backend under /admin139 on frontend port --- .env | 3 ++- docker-compose.yml | 9 +++++---- docs/Docker部署.md | 6 +++--- frontend/next.config.ts | 8 ++++---- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.env b/.env index a34841f..edca7e4 100644 --- a/.env +++ b/.env @@ -10,7 +10,8 @@ CSP_SEED_ADMIN_USERNAME=admin CSP_SEED_ADMIN_PASSWORD=whoami139 # 前端请求后端的地址(浏览器侧) -NEXT_PUBLIC_API_BASE=http://localhost:8080 +# 使用同域反代访问后端(通过Next.js) +NEXT_PUBLIC_API_BASE=/admin139 # Next.js 服务端反代用(可选) BACKEND_INTERNAL_URL=http://backend:8080 diff --git a/docker-compose.yml b/docker-compose.yml index 71b4760..ecd5d27 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,8 +5,9 @@ services: build: context: . dockerfile: Dockerfile.backend - ports: - - "8080:8080" + # 如需直连后端调试,可打开端口映射;默认仅通过前端反代访问 + # ports: + # - "8080:8080" volumes: - csp_data:/data restart: unless-stopped @@ -18,8 +19,8 @@ services: context: . dockerfile: Dockerfile.frontend environment: - # 浏览器侧请求后端的公共地址(本机测试用 localhost) - - NEXT_PUBLIC_API_BASE=http://localhost:8080 + # 浏览器侧通过同域路径前缀访问后端(Next.js反代到backend) + - NEXT_PUBLIC_API_BASE=/admin139 # Next.js 服务端反代用(可选),仅在你把 NEXT_PUBLIC_API_BASE 设为 /api 时需要 - BACKEND_INTERNAL_URL=http://backend:8080 ports: diff --git a/docs/Docker部署.md b/docs/Docker部署.md index 9b2048d..6e0f60e 100644 --- a/docs/Docker部署.md +++ b/docs/Docker部署.md @@ -8,10 +8,10 @@ docker compose up -d --build ## 访问 -- 后端(健康检查):http://localhost:8080/api/health -- 后端(注册):`POST http://localhost:8080/api/v1/auth/register` -- 后端(登录):`POST http://localhost:8080/api/v1/auth/login` - 前端:http://localhost:7888 +- 后端(通过前端反代):http://localhost:7888/admin139/api/health +- 后端(注册):`POST http://localhost:7888/admin139/api/v1/auth/register` +- 后端(登录):`POST http://localhost:7888/admin139/api/v1/auth/login` ## 数据持久化 diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 66ee460..9c30999 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -4,15 +4,15 @@ const nextConfig: NextConfig = { // For local dev convenience. In production you should configure reverse proxy // and set NEXT_PUBLIC_API_BASE to your public API origin. async rewrites() { - // If the user sets NEXT_PUBLIC_API_BASE to "/api", we can proxy to backend - // from the Next.js server (SSR). This does NOT affect browser fetch. + // Reverse proxy backend under a path prefix, so browser can access backend + // with same-origin (no CORS): http://:7888/admin139/... const backendInternal = process.env.BACKEND_INTERNAL_URL; if (!backendInternal) return []; return [ { - source: "/api/:path*", - destination: `${backendInternal}/api/:path*`, + source: "/admin139/:path*", + destination: `${backendInternal}/:path*`, }, ]; },