4

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

5
  • I can share the deployment and service file as well, the image is also deployed on the docker hub, I am willing to provide every detail. Mar 10, 2022 at 13:32
  • 1
    Well, /link1/ is not a prefix of /link1 because a prefix must match the beginning of the target string. The target string /link1 is simply missing the ending / that the prefix /link1/ has.
    – Lukman
    Mar 10, 2022 at 13:58
  • @Lukman But in kubernetes.io/docs/concepts/services-networking/ingress/… if you scroll down a bit, you will see the documentation mentions Prefix /foo/ /foo, /foo/ Yes Where Yes is for matches Mar 10, 2022 at 14:40
  • @Lukman If you scroll down a bit further you will see Prefix /aaa/bbb/ /aaa/bbb Yes, ignores trailing slash, the documentation explicitly mentions about ignoring trailing slash. Mar 10, 2022 at 14:57
  • @Lukman Do you have some time? it would be really appreciated. Mar 13, 2022 at 12:46

1 Answer 1

4
+50

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(/|$)
6
  • 1
    But in kubernetes.io/docs/concepts/services-networking/ingress/… if you scroll down a bit, you will see the documentation mentions Prefix /foo/ /foo, /foo/ Yes Where Yes is for matches Mar 10, 2022 at 14:39
  • 1
    If you scroll even down further you will see Prefix /aaa/bbb/ /aaa/bbb Yes, ignores trailing slash Mar 10, 2022 at 14:56
  • Can you please explain how is this happening? Mar 10, 2022 at 18:29
  • Do you have some time? it will be really appreciated. Mar 13, 2022 at 12:46
  • 1
    You are a legend for helping me with this, I really appreciate it, thanks a lot man. Mar 16, 2022 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.