Reputation: 2297
I am migrating legacy websites to Kubernetes, which were working on URLs like www.app1.com, www.app2.com,.
As all are getting deployed in one K8s cluster so I want to use URLs like www.myapp.com/app1
and so on. But when I access www.myapp.com/app1 it routes to www.myapp.com/login rather than www.myapp.com/app1/login
Try1:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
name: rewrite
spec:
rules:
- host: www.myapp.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /app1/?(.*)
I tried path: /app1(.*) but it always routes this way:
Try2:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/configuration-snippet: rewrite ^(/app1)$ $1/ permanent;
name: rewrite
spec:
rules:
- host: www.myapp.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /app1(/|$)(.*)
This lands me to the login page but all URLs on the page still have URLs like www.myapp.com/page1 or www.myapp.com/page2 rather than www.myapp.com/app1/page1 or www.myapp.com/app1/page2.
It looks like I have to do something in my app, but can't we do something within ingress without changes in the app code?
Update1: ingress logs: In browser when i access www.myapp.com/app1 it prints following logs
[13/Aug/2020:21:19:25 +0000] "GET /app1 HTTP/2.0" 303 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36" 503 0.005 [my-ns-http-svc-80] [] x.x.x.x:80 5 0.005 303 d5da4ff09ee26c83fe67519c98f5eb50
and in browser it gives 404 error and in URL-bar URL is www.myapp.com/login
Upvotes: 1
Views: 6710
Reputation: 61699
Looks like the following redirect is happening in your app:
www.myapp.com/app1
ā”ļø www.myapp.com/app1/login
So if you'd like the same behavior in the Ingress resource you will have to remove the nginx.ingress.kubernetes.io/rewrite-target:
annotation.
Then on your paths, you can have something like this:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: rewrite
spec:
rules:
- host: www.myapp.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /app1 š Handles the initial request
- backend:
serviceName: http-svc
servicePort: 80
path: /app1/.* š Handles the redirect your app is doing.
āļø
Upvotes: 1