Home > Net >  nginx-ingress rewrite-target to /api not working
nginx-ingress rewrite-target to /api not working

Time:12-02

i am trying to get api as location (/api) to work with intress settings

this is my ingress:

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

when i curl dev.example.com/api/check_version i get error

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"):

if i add annotation nginx.ingress.kubernetes.io/rewrite-target: /

get error

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] "/"):

how to make a rewrite correctly in this case?

CodePudding user response:

Based on the comments, the following seems to have fixed the issue.

You are looking to strip off the /api part off before the request is sent to the backend, thus requesting /api/check_version will become /check_version before it hits the backend:

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

...<omitted>...

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