diff --git a/backend/services/docker-compose.2.0.yml b/backend/services/docker-compose.2.0.yml index 4b8c6c8d..814f2d0a 100644 --- a/backend/services/docker-compose.2.0.yml +++ b/backend/services/docker-compose.2.0.yml @@ -480,6 +480,10 @@ services: PORT: 3100 NEXT_PUBLIC_API_URL: http://mining-admin-service:3023 NEXT_PUBLIC_APP_NAME: 挖矿管理后台 + # 直连后端服务 (同一 Docker 网络内, 不经 Kong 网关) + # 如需走 Kong 网关, 设置 API_GATEWAY_URL 即可 (如 https://mapi.szaiai.com) + TRADING_SERVICE_URL: http://trading-service:3022 + MINING_SERVICE_URL: http://mining-service:3021 ports: - "3100:3100" healthcheck: diff --git a/frontend/mining-admin-web/next.config.js b/frontend/mining-admin-web/next.config.js index 696a9ded..48c051bd 100644 --- a/frontend/mining-admin-web/next.config.js +++ b/frontend/mining-admin-web/next.config.js @@ -3,27 +3,21 @@ const nextConfig = { reactStrictMode: true, output: 'standalone', async rewrites() { - // 获取 API 基础 URL(Kong 网关地址) - // 生产环境: https://mapi.szaiai.com - // 开发环境: http://localhost:3023 (mining-admin-service) - const apiGatewayUrl = process.env.API_GATEWAY_URL || 'https://mapi.szaiai.com'; + // API 路由模式: + // 1. 设置了 API_GATEWAY_URL -> 通过 Kong 网关代理 (适用于前端与后端不在同一 Docker 网络) + // 2. 未设置 API_GATEWAY_URL -> 直连各后端服务 (适用于同一 Docker 网络, 推荐 standalone 模式) + const apiGatewayUrl = process.env.API_GATEWAY_URL || ''; const miningAdminUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3023'; const tradingServiceUrl = process.env.TRADING_SERVICE_URL || 'http://localhost:3022'; const miningServiceUrl = process.env.MINING_SERVICE_URL || 'http://localhost:3021'; - // 检查是否是生产环境(使用 Kong 网关) - const isProduction = process.env.NODE_ENV === 'production'; - // 移除末尾可能存在的路径避免重复 const cleanMiningAdminUrl = miningAdminUrl.replace(/\/api\/v2.*$/, ''); const cleanTradingUrl = tradingServiceUrl.replace(/\/api\/v2.*$/, ''); const cleanMiningUrl = miningServiceUrl.replace(/\/api\/v2.*$/, ''); - if (isProduction) { - // 生产环境:通过 Kong 网关 - // /api/trading/* -> Kong -> trading-service - // /api/mining/* -> Kong -> mining-service - // /api/* -> Kong -> mining-admin-service + if (apiGatewayUrl) { + // 通过 Kong 网关: 所有请求经 Kong 路由分发到各服务 return [ { source: '/api/trading/:path*', @@ -39,15 +33,16 @@ const nextConfig = { }, ]; } else { - // 开发环境:直连各服务 + // 直连各服务: 前端与后端在同一 Docker 网络内直接通信 return [ { source: '/api/trading/:path*', destination: `${cleanTradingUrl}/api/v2/:path*`, }, { + // mining-service 的 controller 有 mining/ 前缀, 需保留 source: '/api/mining/:path*', - destination: `${cleanMiningUrl}/api/v2/:path*`, + destination: `${cleanMiningUrl}/api/v2/mining/:path*`, }, { source: '/api/:path*', diff --git a/frontend/mining-admin-web/src/lib/api/client.ts b/frontend/mining-admin-web/src/lib/api/client.ts index a391617c..93018b34 100644 --- a/frontend/mining-admin-web/src/lib/api/client.ts +++ b/frontend/mining-admin-web/src/lib/api/client.ts @@ -1,7 +1,8 @@ import axios from 'axios'; -// 生产环境使用 NEXT_PUBLIC_API_URL,开发环境使用 /api 代理 -const baseURL = process.env.NEXT_PUBLIC_API_URL || '/api'; +// 始终使用 /api 前缀, 通过 Next.js rewrite 代理到后端服务 +// NEXT_PUBLIC_API_URL 仅供服务端使用 (next.config.js rewrites), 不在客户端使用 +const baseURL = '/api'; export const apiClient = axios.create({ baseURL,