Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure nginx.ingress.kubernetes.io/rewrite-target and spec.rules.http.paths.path to satisfy the following URI patterns

How can I configure nginx.ingress.kubernetes.io/rewrite-target and spec.rules.http.paths.path to satisfy the following URI patterns?

/aa/bb-aa/coolapp
/aa/bb-aa/coolapp/cc

Legend:

  • a = Any letter between a-z. Lowercase. Exactly 2 letters - no more, no less.
  • b = Any letter between a-z. Lowercase. Exactly 2 letters - no more, no less.
  • c = any valid URI character. Lowercase. Of variable length - think slug.

Example URI:s that should match the above pattern:

/us/en-us/coolapp
/us/en-us/coolapp/faq
/us/en-us/coolapp/privacy-policy

Attention

Starting in Version 0.22.0, ingress definitions using the annotation nginx.ingress.kubernetes.io/rewrite-target are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a capture group.

Note

Captured groups are saved in numbered placeholders, chronologically, in the form $1, $2 ... $n. These placeholders can be used as parameters in the rewrite-target annotation.

References:

  1. https://kubernetes.github.io/ingress-nginx/examples/rewrite/
  2. https://github.com/kubernetes/ingress-nginx/pull/3174
like image 781
PussInBoots Avatar asked May 23 '19 14:05

PussInBoots


People also ask

What does nginx ingress kubernetes IO rewrite target mean?

nginx.ingress.kubernetes.io/rewrite-target. Target URI where the traffic must be redirected. string. nginx.ingress.kubernetes.io/ssl-redirect. Indicates if the location section is only accessible via SSL (defaults to True when Ingress contains a Certificate)

What is nginx ingress kubernetes IO configuration snippet?

The configuration-snippet is to add configs to locations. If you want to add a custom location to the server context, you should use the server-snippet instead: Using the annotation nginx.ingress.kubernetes.io/server-snippet it is possible to add custom configuration in the server configuration block.

What is kubernetes IO ingress class nginx?

In Kubernetes, an Ingress is an object that allows access to your Kubernetes services from outside the Kubernetes cluster. You configure access by creating a collection of rules that define which inbound connections reach which services. This lets you consolidate your routing rules into a single resource.


1 Answers

The nginx.ingress.kubernetes.io/rewrite-target annotation is used to indicate the target URI where the traffic must be redirected. As per how I understand your question, you only want to match the URI patterns that you specified without redirecting the traffic. In order to achieve this, you can set the nginx.ingress.kubernetes.io/use-regex annotation to true, thus enabling regular expressions in the spec.rules.http.paths.path field.

Let's now take a look at the regex that you will need to match your URI patterns. First of all, the regex engine used by ingress-nginx doesn't support backreferences, therefore a regex like this one will not work. This is not a problem as you can match the /aa-bb/aa part without forcing the two aas to be equal since you will —presumably— still have to check the correctness of the URI later on in your service (e.g /us/en-us may be accepted whereas /ab/cd-ab may not).

You can use this regex to match the specified URI patterns:

/[a-z]{2}/[a-z]{2}-[a-z]{2}/coolapp(/.*)?

If you want to only match URL slugs in the cc part of the pattern you specified, you can use this regex instead:

/[a-z]{2}/[a-z]{2}-[a-z]{2}/coolapp(/[a-z0-9]+([a-z0-9]+)*)?

Lastly, as the nginx.ingress.kubernetes.io/use-regex enforces case insensitive regex, using [A-Z] instead of [a-z] would lead to the same result.


Follows an ingress example definition using the use-regex annotation:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-regex
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - host: test.com
    http:
      paths:
      - path: /[a-z]{2}/[a-z]{2}-[a-z]{2}/coolapp(/.*)?
        backend:
          serviceName: test
          servicePort: 80

You can find more information about Ingress Path Matching in the official user guide.

like image 169
Gilgames Avatar answered Sep 23 '22 14:09

Gilgames