移除 Kong 路由前缀

问题描述 投票:0回答:2

我有一个入口为

/api/my-service

的服务

我的金刚配置是这样的:

services:
  - name: myService
    protocol: http
    host: myservice.mynamespace
    port: 8080
    path: /
    plugins:
      - name: jwt
        config:
           key_claim_name: iss
           claims_to_verify:
             - exp
    routes:
      - tags:
          - OAS3_import
          - OAS3file_openapi.json
        name: myservice-backend
        methods:
          - GET
        paths:
          - /api/my-service/v0/contexts
        strip_path: false
      - tags:

当我请求

http://kongProxy/api/my-service/v0/myEndpoint
时,我希望kong创建一个像
http://my-service/v0/myEndpoint

这样的上游请求

我以为我可以使用 strip_path 设置,但是去掉后缀,而不是前缀。

我查看了请求转换器,但看起来像这样的东西有点矫枉过正。而且我更愿意避免它,因为 cong 图像已经提供给我并且希望避免请求修改。

有什么想法吗?

kubernetes kong
2个回答
1
投票

为了得到你想要的,你的配置应该是这样的:

services:
  - name: myService
    protocol: http
    host: myservice.mynamespace
    port: 8080
    path: /
    plugins:
      - name: jwt
        config:
           key_claim_name: iss
           claims_to_verify:
             - exp
    routes:
      - tags:
          - OAS3_import
          - OAS3file_openapi.json
        name: myservice-backend
        methods:
          - GET
        paths:
          - /api/my-service
        strip_path: true
      - tags:

然后,请求

http://kongProxy/api/my-service/v0/myEndpoint

将发送至

http://myservice.mynamespace:8080/v0/myEndpoint


0
投票

我认为你可以用正则表达式匹配来做到这一点。如果您创建一个匹配前缀 * 的正则表达式组,剥离路径将删除它而不是不匹配的后缀。抱歉,我现在无法对此进行测试。

https://docs.konghq.com/gateway/latest/how-kong-works/routing-traffic/#using-regex-in-paths

© www.soinside.com 2019 - 2024. All rights reserved.