Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of the numbered placeholders in ingress rewrite-target?

For example, the following manifest(from https://kubernetes.github.io/ingress-nginx/examples/rewrite/), I know that any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation, but why it is $2, not $1 and $3? I can't find anything explaining the difference on the Internet.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)
like image 832
xczzhh Avatar asked Oct 27 '25 10:10

xczzhh


1 Answers

In a regex, every part of it in parentheses () is considered a group and each is numbered. Group 0 is the entire match, group 1 the first group, etc.

So, in /something(/|$)(.*), whatever matches (/|$) is group 1 and whatever matches (.*) is group 2. In your example, $2 just refers to group 2 of whatever was matched for it and will be replaced by that.

like image 182
Grismar Avatar answered Oct 30 '25 00:10

Grismar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!