diff --git a/nginx.conf.gateway b/nginx.conf.gateway index 8dcffa9..dd4eb3d 100644 --- a/nginx.conf.gateway +++ b/nginx.conf.gateway @@ -83,58 +83,108 @@ http { } } - # --- 统一网关服务 --- + # ------------------------------- + # 2. Sys系统服务 (8000) + # ------------------------------- server { - listen 8000 ssl; # 使用 http2 提升性能 - server_name 106.52.199.114; # 替换为您的域名或IP - - # --- SSL 配置 --- - ssl_certificate /etc/nginx/certs/gateway.crt; + listen 8000 ssl; + server_name 106.52.199.114; + ssl_certificate /etc/nginx/certs/gateway.crt; ssl_certificate_key /etc/nginx/certs/gateway.key; - ssl_protocols TLSv1.2 TLSv1.3; - ssl_prefer_server_ciphers on; - ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; - ssl_session_cache shared:SSL:10m; - ssl_session_timeout 10m; - - # --- 统一代理头配置 --- - # 这些头对后端服务正确识别客户端信息至关重要 - 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_set_header X-Forwarded-Host $host; - - # 2. Sys API 路由 + ssl_protocols TLSv1.2 TLSv1.3; + # API路由 location /api/sys/ { proxy_pass https://sys_api/api/; proxy_ssl_server_name on; - proxy_ssl_session_reuse off; proxy_ssl_verify off; - proxy_set_header Authorization $http_authorization; # 传递 Authorization 头 + proxy_set_header Authorization $http_authorization; } - # 3. Lmg API 路由 - location /api/lmg/ { - proxy_pass https://lmg_api/api/; + # 认证服务代理 + location /auth/ { + # 关键 1: 移除路径中的 /auth/ 前缀 + proxy_pass https://sys_api/; # 末尾的 / 确保路径替换 + proxy_ssl_server_name on; proxy_ssl_session_reuse off; proxy_ssl_verify off; + # 关键 2: 覆盖 Host 头,模拟 Vite 的 changeOrigin=true + proxy_set_header Host $proxy_host; # 设置为目标地址的 Host + + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header Authorization $http_authorization; # 传递 Authorization 头 + # 其他标准代理头 + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + # 关键 4: 重写后端返回的 Location 头(解决重定向端口丢失) + proxy_redirect https://sys_api:19902/ http://$host:$server_port/auth/; + proxy_redirect https://sys_api/ http://$host:$server_port/auth/; } - # 4. Lmg UI 路由 - # IMPORTANT: lmg-ui 的前端路由基础路径(base path)需要配置为 /lmg/ + # UI前端路由 + location / { + proxy_pass http://sys_ui; + proxy_set_header Host $host; + } + } + + + # ------------------------------- + # 3. Lmg系统服务 (8002) + # ------------------------------- + server { + listen 8002 ssl; + server_name 106.52.199.114; + ssl_certificate /etc/nginx/certs/gateway.crt; + ssl_certificate_key /etc/nginx/certs/gateway.key; + ssl_protocols TLSv1.2 TLSv1.3; + # API路由 + location /api/lmg/ { + proxy_pass https://lmg_api/api/; + proxy_ssl_server_name on; + proxy_ssl_verify off; + proxy_set_header Authorization $http_authorization; + } + + # UI前端路由(基础路径需配置为/lmg/) location /lmg/ { - # 将 /lmg/path -> http://lmg_ui/path - proxy_pass http://lmg_ui/; + proxy_pass http://lmg_ui/; # 注意末尾的/ + proxy_set_header Host $host; + } + + location /auth/ { + # 关键 1: 移除路径中的 /auth/ 前缀 + proxy_pass https://sys_api/; # 末尾的 / 确保路径替换 + + proxy_ssl_server_name on; + proxy_ssl_session_reuse off; + proxy_ssl_verify off; + # 关键 2: 覆盖 Host 头,模拟 Vite 的 changeOrigin=true + proxy_set_header Host $proxy_host; # 设置为目标地址的 Host + + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-Host $host:$server_port; + proxy_set_header Authorization $http_authorization; # 传递 Authorization 头 + # 其他标准代理头 + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + # 关键 4: 重写后端返回的 Location 头(解决重定向端口丢失) + proxy_redirect https://sys_api:19902/ http://$host:$server_port/auth/; + proxy_redirect https://sys_api/ http://$host:$server_port/auth/; + } + + location /api/sys/ { + proxy_pass https://sys_api/api/; + proxy_ssl_server_name on; + proxy_ssl_verify off; + proxy_set_header Authorization $http_authorization; } - # 5. Sys UI 和根路径路由 - location / { - # 根路径 / 直接访问 sys_ui - # 将 /path -> http://sys_ui/path - proxy_pass http://sys_ui; + # 根路径重定向到Lmg UI(可选) + location = / { + return 301 /lmg/; } - } }