skip to Main Content

I am using the Nginx annotations in Helm like so:

ingress:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  hosts:
    - host: "example.com"
      paths:
        - path: /api(/?)(.*)

When visiting example.com/api/, my URL is rewritten as expected and I am forwarded to my application.

However, when the trailing slash is omitted, e.g example.com/api, this no longer is the case. What could I do to ensure that the scenario without a trailing slash included is correctly evaluated?

2

Answers


  1. You can try to add another path for that specific endpoint in your paths array:

    ingress:
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$2
      hosts:
        - host: "example.com"
          paths:
            - path: /api
            ...
            - path: /api(/?)(.*)
            ...
    

    If you are using K8s 1.18 or later you can take advantage of pathType

    ingress:
      hosts:
        - host: "example.com"
          paths:
            - path: /api
              pathType: Prefix
    
    Login or Signup to reply.
  2. I think you are searching for regex alternatives?

    ingress:
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$2
      hosts:
        - host: "example.com"
          paths:
            - path: /api(/|$)(.*)
    

    Either after /api there’s another / with whatever (captured by the $2) or there is the end of the line, which will make /api be rewritten to /.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search