From 0ec8bea1c1e159f87635fad9884d218cd4ff46c6 Mon Sep 17 00:00:00 2001 From: yidl Date: Sun, 29 Jun 2025 00:04:06 +0800 Subject: [PATCH] save --- nginx.conf.gateway | 60 ++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/nginx.conf.gateway b/nginx.conf.gateway index 6784610..ff2c118 100644 --- a/nginx.conf.gateway +++ b/nginx.conf.gateway @@ -31,14 +31,9 @@ http { # --- 后端服务定义 (Upstreams) --- # 根据您的描述更新了端口号 - # 认证中心 (IdentityServer4) - upstream auth_server { - server sys-api:19902; - } - # Sys 系统 API upstream sys_api { - server sys-api:19901; + server sys-api:19902; } # Lmg 系统 API @@ -80,41 +75,51 @@ http { # --- 路由规则 (Locations) --- # 1. 认证中心 (IdentityServer4) 路由 - 专门处理并移除 'auth' 前缀 - # 匹配 /auth/ 开头的请求 - location ~ ^/auth/ { - # rewrite 规则: - # ^/auth(/.*)$ : 匹配以 /auth 开头,并捕获 / 后面的所有路径到 \$1 - # \$1 : 替换为 \$1,即只保留 /auth 后面的路径 - # break : 停止处理当前的 rewrite 规则,然后处理 proxy_pass - rewrite ^/auth(/.*)$ \$1 break; - proxy_pass https://auth_server; - 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 (192.168.1.100:19901) + + 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; - proxy_set_header X-Forwarded-Proto $scheme; - - proxy_ssl_verify off; # 关闭证书验证 + + # 关键 4: 重写后端返回的 Location 头(解决重定向端口丢失) + proxy_redirect https://sys_api/ http://$host:$server_port/auth/; + proxy_redirect https://sys_api/ http://$host:$server_port/auth/; } # 1. 认证中心 (IdentityServer4) 路由 # 匹配所有认证相关的路径 location ~ ^/(connect|Account)/ { - proxy_pass https://auth_server; + proxy_pass https://sys_api/; proxy_ssl_verify off; # 关闭证书验证 } # 2. Sys API 路由 location /api/sys/ { - # 将 /api/sys/path -> http://sys_api/api/sys/path - proxy_pass https://sys_api; - proxy_ssl_verify off; # 关闭证书验证 + 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 头 } - # 3. Lmg API 路由 location /api/lmg/ { - # 将 /api/lmg/path -> http://lmg_api/api/lmg/path - proxy_pass http://lmg_api; - proxy_ssl_verify off; # 关闭证书验证 + proxy_pass http://lmg_api/api/; + proxy_ssl_server_name on; + proxy_ssl_session_reuse off; + proxy_ssl_verify off; + proxy_set_header Authorization $http_authorization; # 传递 Authorization 头 } # 4. Lmg UI 路由 @@ -122,7 +127,6 @@ http { location /lmg/ { # 将 /lmg/path -> http://lmg_ui/path proxy_pass http://lmg_ui/; - proxy_ssl_verify off; # 关闭证书验证 } # 5. Sys UI 和根路径路由 @@ -132,7 +136,6 @@ http { # 根路径 / 直接访问 sys_ui # 将 /path -> http://sys_ui/path proxy_pass http://sys_ui; - proxy_ssl_verify off; # 关闭证书验证 } # 6. OAuth 回调地址路由 @@ -140,7 +143,6 @@ http { location = /Callback { # 将 /Callback -> http://sys_ui/Callback proxy_pass http://sys_ui; - proxy_ssl_verify off; # 关闭证书验证 } } }