In my k8s cluster, I have 2 web application,
- app1 running in app1-k8s-svc:80
- app2 running in app2-k8s-svc:9090
which has multiple rediection,
Note: i dont have any control to change the nginx proxy configuration of both the apps (app1 and app2)
I have nginx ingress configured for both the apps as below,
ingress configuration
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/add-base-url : "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /app1(/|$)(.*)
pathType: Prefix
backend:
service:
name: "app1-k8s-svc"
port:
number: 80
- path: /app2(/|$)(.*)
pathType: Prefix
backend:
service:
name: "app2-k8s-svc"
port:
number: 9090
with this configuration, app1 and app2 goes to right service on the first browser call, however on the subsequent url redirection, landing back to locahost not to that respective service, this causes the issue.
from http://localhost/app2 ==> forwards to http://app2-k8s-svc:9090/link1 subsequent redirection falls back to http://localhost/link1 itself which is wrong. It should fall back like http://localhost/app2/link1.
looks like I am missing some basics. could someone please help?
2
Answers
As far I found, ingress is just meant for routing, from URL prefix to the specific k8s service, if we have multiple re-direction, then it needs to be fixed at the application level.
in my case apps are Prometheus and OpenSearch-Dashboard, both of them support adding a baseURL or basepath, so that context is set for each app, and then on re-direction works fine.
Sample from OpenSearch-dashboard,
That is because the applications are not aware of the path prefixes
/app1/
and/app2/
.You need to pass the full paths including the prefixes to the respective applications by forgoing the
rewrite-target
annotation in the ingress. Of course you need to ensure both applications can handle the prefixes accordingly.