Home > other >  Why URL re-writing is not working when I do not use slash at the end?
Why URL re-writing is not working when I do not use slash at the end?

Time:03-16

I have a simple ingress configuration file-

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /link2/link3/
  name: tut-ingress
  namespace: default
spec:
  rules:
    - host: tutorial.com
      http:
        paths:
          - path: /link1/
            pathType: Prefix
            backend:
              service:
                name: nginx-ingress-tut-service
                port:
                  number: 8080

in which requests coming to /link1 or /link1/ are rewritten to /link2/link3/. When I access it using http://tutorial.com/link1/ I am shown the correct result but when I access it using http://tutorial.com/link1, I get a 404 not found. The nginx-ingress-tut-service has the following endpoints-

  • /
  • /link1
  • /link2/link3

I am a beginner in the web domain, any help will be appreciated.

When I change it to-

- path: /link1

it starts working fine, but can anybody tell why is it not working with /link1/.

Helpful resources - https://kubernetes.io/docs/concepts/services-networking/ingress/#examples

https://kubernetes.github.io/ingress-nginx/examples/rewrite/

Edit- Please also explain what happens when you write a full HTTP link in nginx.ingress.kubernetes.io/rewrite-target

CodePudding user response:

The answer is posted in the comment:

Well, /link1/ is not a prefix of /link1 because a prefix must be the same length or longer than the target string

If you have

- path: /link1/

the string to match will have to have a / character at the end of the path. Everything works correctly. In this situation if you try to access by the link http://tutorial.com/link1 you will get 404 error, because ingress was expecting http://tutorial.com/link1/.

For more you can see examples of rewrite rule and documentation about path types:

Each path in an Ingress is required to have a corresponding path type. Paths that do not include an explicit pathType will fail validation. There are three supported path types:

  • ImplementationSpecific: With this path type, matching is up to the IngressClass. Implementations can treat this as a separate pathType or treat it identically to Prefix or Exact path types.

  • Exact: Matches the URL path exactly and with case sensitivity.

  • Prefix: Matches based on a URL path prefix split by /. Matching is case sensitive and done on a path element by element basis. A path element refers to the list of labels in the path split by the / separator. A request is a match for path p if every p is an element-wise prefix of p of the request path.

EDIT: Based on documentation this should work, but it looks like there is a fresh problem with nginx ingress. The problem is still unresolved. You can use workaround posted in this topic or try to change your you similar to this:

- path: /link1(/|$)
  • Related