I have the following deployment yaml files:
foo.yaml:
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: foo-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/proxy-read-timeout: "1800"
nginx.ingress.kubernetes.io/proxy-send-timeout: "1800"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^(/apps/foo)$ $1/ redirect;
spec:
rules:
- host: myhost
http:
paths:
- path: /apps/foo/(.*)
backend:
serviceName: foo-service
servicePort: 8080
bar.yaml:
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: bar-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/proxy-read-timeout: "1800"
nginx.ingress.kubernetes.io/proxy-send-timeout: "1800"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/configuration-snippet: |
rewrite ^(/apps/bar)$ $1/ redirect;
spec:
rules:
- host: myhost
http:
paths:
- path: /apps/bar/(.*)
backend:
serviceName: foo-service
servicePort: 8080
I applied each file with
kubectl apply -f foo.yaml
kubectl apply -f bar.yaml
The problem is that the redirection from /apps/foo to /apps/foo/ (adding the extra slash) works fine. The redirection from /apps/bar does not work. I get a 404.
I opened the generated nginx.conf in the ingress pod, and it's a bit of a mess. I have the following (removed irrelevant parts):
location ~* "^/apps/foo/(.*)" {
rewrite ^(/apps/foo)$ $1/ redirect;
}
location ~* "^/apps/bar/(.*)" {
rewrite ^(/apps/bar)$ $1/ redirect;
}
location ~* "^/" {
...
rewrite ^(/apps/foo)$ $1/ redirect;
}
As you can see, the redirection is added both under the / matching (where it works) and also in the more specific matching, where it makes no sense (as it will never get there, given that without a / the rule will not match).
What am I doing wrong in the overall approach? why is the ingress controller adding redirection only to one url?