Kong Ingress配置问题:如何消除URL中的重复/api路径?
Kong Ingress路径映射问题解决方法
你的问题核心是Kong的路径转发逻辑和后端服务的路径前缀不匹配导致的:当前配置中konghq.com/strip-path: "true"会把请求中匹配的/api前缀去掉后转发到后端,所以当你请求localhost:xxxx/api/service/endpoint时,后端收到的路径是/service/endpoint,但你的服务实际监听的是/api/service/endpoint,因此需要两次/api才能让后端收到正确路径。
可以通过以下两种方式调整配置解决:
方案一:关闭路径剥离(strip-path)
修改Ingress注解,将konghq.com/strip-path设为"false",这样Kong会将完整的请求路径转发到后端服务:
ingress: name: kong-ingress annotations: konghq.com/strip-path: "false" rules: - http: paths: - path: /api pathType: ImplementationSpecific backend: service: name: mt-amp-api-gateway port: number: 8080
此时请求localhost:xxxx/api/service/endpoint时,后端会收到完整的/api/service/endpoint路径,与服务的监听端点匹配。
方案二:使用路径重写(rewrite)
保留strip-path: "true",同时添加konghq.com/rewrite注解,并调整path为正则匹配格式,实现路径重写:
ingress: name: kong-ingress annotations: konghq.com/strip-path: "true" konghq.com/rewrite: "/api/$1" rules: - http: paths: - path: /api/(.*) pathType: ImplementationSpecific backend: service: name: mt-amp-api-gateway port: number: 8080
这个配置会将请求/api/service/endpoint中的service/endpoint部分提取出来,重写为/api/service/endpoint后转发到后端,同样能匹配服务的监听端点。
内容的提问来源于stack exchange,提问作者Sri hari
相关产品推荐
相关产品推荐

