nginx如何将请求转发到后端服务器,后端服务器又是如何识别请求到其子服务
nginx中有以下配置:
location /dataplatform/ {
proxy_pass http://10.104.0.165:8601/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /fusion-fms-costallocat/ {
proxy_pass http://10.104.0.165:8601;
}
在网关的yml文件中配置了routes:
routes:
- id: ss-platform-authzth
uri: lb://ss-platform-authzth
predicates:
- Path=/authzth/**
filters:
- StripPrefix=1
- id: ss-platform-common
uri: lb://ss-platform-common
predicates:
- Path=/common/**
filters:
- StripPrefix=1
前端传入的路径为http://10.104.0.164:18080/dataplatform/authzth/user/enquiry 此时网关是如何识别转发,请求到对应的子服务中?
1. 请求流程概述
-
前端请求:
前端发送请求:http://10.104.0.164:18080/dataplatform/authzth/user/enquiry
-
Nginx 处理:
Nginx 根据location /dataplatform/
规则,将请求转发到http://10.104.0.165:8601/
-
Spring Cloud Gateway 处理:
网关根据Path=/authzth/**
规则,将请求转发到ss-platform-authzth
服务。 -
子服务处理:
ss-platform-authzth
服务接收到请求并处理。
2. 详细解析
(1)Nginx 处理
-
Nginx 配置:
nginx
location /dataplatform/ { proxy_pass http://10.104.0.165:8601/; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /fusion-fms-costallocat/ { proxy_pass http://10.104.0.165:8601; }
-
配置解释:
-
匹配所有以
/fusion-fms-costallocat/
开头的请求路径。 -
例如,
/fusion-fms-costallocat/config
或/fusion-fms-costallocat/log
都会被匹配。
-
-
转发规则:
-
将请求转发到
http://10.104.0.165:8601
。 -
注意:
proxy_pass
末尾没有/
,因此路径会直接追加到目标 URL 后面。-
例如,
/fusion-fms-costallocat/config
会被转发到http://10.104.0.165:8601/fusion-fms-costallocat/config
。
-
-
-
请求路径:
-
前端请求:
http://10.104.0.164:18080/dataplatform/authzth/user/enquiry
。
-
-
Nginx 处理:
-
匹配到
location /dataplatform/
规则。 -
proxy_pass
末尾有/
,因此会将/dataplatform/
替换为/
,转发到http://10.104.0.165:8601/
。 -
转发后的请求路径为:
http://10.104.0.165:8601/authzth/user/enquiry
。
-
注:对于 /
fusion-fms-costallocat /
,proxy_pass
末尾没有 /
,因此路径会直接追加到目标 URL 后面。例如,/fusion-fms-costallocat/config
会被转发到 http://10.104.0.165:8601/fusion-fms-costallocat/config
。
(2)Spring Cloud Gateway 处理
-
网关配置:
yaml
routes: - id: ss-platform-authzth uri: lb://ss-platform-authzth predicates: - Path=/authzth/** filters: - StripPrefix=1
-
请求路径:
-
Nginx 转发后的请求路径:
http://10.104.0.165:8601/authzth/user/enquiry
。
-
-
网关处理:
-
匹配到
Path=/authzth/**
规则。 -
使用
StripPrefix=1
过滤器,移除路径中的第一个前缀/authzth
。 -
转发后的请求路径为:
http://ss-platform-authzth/user/enquiry
。
-
(3)子服务处理
-
目标服务:
-
ss-platform-authzth
服务。
-
-
请求路径:
-
网关转发后的请求路径:
http://ss-platform-authzth/user/enquiry
。
-
-
子服务处理:
-
ss-platform-authzth
服务接收到请求路径/user/enquiry
,并处理该请求。
-
3. 完整请求流程
-
前端请求:
http://10.104.0.164:18080/dataplatform/authzth/user/enquiry
。 -
Nginx 转发:
匹配location /dataplatform/
,转发到http://10.104.0.165:8601/authzth/user/enquiry
。 -
网关转发:
匹配Path=/authzth/**
,移除/authzth
,转发到http://ss-platform-authzth/user/enquiry
。 -
子服务处理:
ss-platform-authzth
服务处理/user/enquiry
请求。
4. 路径变化总结
阶段 | 路径变化 |
---|---|
前端请求 | /dataplatform/authzth/user/enquiry |
Nginx 转发后 | /authzth/user/enquiry |
网关转发后 | /user/enquiry |
子服务接收 | /user/enquiry |
5. 关键点说明
-
Nginx 的
proxy_pass
:-
末尾的
/
表示将/dataplatform/
替换为/
。
-
-
网关的
StripPrefix=1
:-
移除路径中的第一个前缀
/authzth
。
-
-
负载均衡:
-
网关的
uri: lb://ss-platform-authzth
表示使用负载均衡。
-