nginx-ingress 重写目标到 /api 不工作

nginx-ingress rewrite-target to /api not working

我正在尝试将 api 作为位置 (/api) 以使用入口设置

这是我的入口:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-dev
  annotations:
    # use the shared ingress-nginx
    kubernetes.io/ingress.class: "nginx"
    #nginx.ingress.kubernetes.io/rewrite-target: /
    #nginx.ingress.kubernetes.io/app-root: /
    
spec:
  rules:
  - host: 'dev.example.com'
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: my-ruby
            port: 3000

当我卷曲时 dev.example.com/api/check_version 我收到错误

I, [2021-12-01T16:43:35.776502 #13]  INFO -- : [7253cca0b88503d625af527db32eb92e] Started GET "/api/check_serverr" for 10.42.1.228 at 2021-12-01 16:43:35 +0300
F, [2021-12-01T16:43:35.779603 #13] FATAL -- : [7253cca0b88503d625af527db32eb92e]
[7253cca0b88503d625af527db32eb92e] ActionController::RoutingError (No route matches [GET] "/api/check_version"):

如果我添加注释 nginx.ingress.kubernetes.io/rewrite-target: /

得到错误

I, [2021-12-01T16:49:11.153280 #13]  INFO -- : [7832de5c07e3a173ddc86ebab5735cec] Started GET "/" for 10.42.1.228 at 2021-12-01 16:49:11 +0300
F, [2021-12-01T16:49:11.154435 #13] FATAL -- : [7832de5c07e3a173ddc86ebab5735cec]
[7832de5c07e3a173ddc86ebab5735cec] ActionController::RoutingError (No route matches [GET] "/"):

在这种情况下如何正确重写?

根据评论,以下内容似乎已解决问题。

您希望在将请求发送到后端之前剥离 /api 部分,因此请求 /api/check_version 在到达后端之前将变为 /check_version

  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /

...<omitted>...

      - path: /api(/|$)(.*)