You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:11:02