我正在将旧网站迁移到Kubernetes,这些网站使用的URL包括www.app1.com、.app2.com、。
由于所有这些都部署在一个K8s集群中,所以我想使用www.myapp.com/app1
等URL。但当我访问www.myapp.com/app1时,它会路由到www.myapp.com/login,而不是http://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/?(.*)
我尝试了路径:/app1(.*(,但它总是这样路由:
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(/|$)(.*)
这让我进入登录页面,但页面上的所有URL仍然有类似www.myapp.com/page1或www.myapp.com/spage2的URL,而不是www.myapp.microsoft.com/app1/page1或www.yapp.com/app1/page2的URL。
看起来我必须在我的应用程序中做一些事情,但如果不更改应用程序代码,我们就不能在ingress中做一些什么吗?
更新1:入口日志:在浏览器中,当我访问www.myapp.com/app1时,它会打印以下日志
[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
在浏览器中,它给出404错误,在URL栏中,URL为www.myapp.com/login
看起来以下重定向正在您的应用程序中发生:
www.myapp.com/app1
➡️www.myapp.com/app1/login
因此,如果您希望在Ingress资源中有相同的行为,则必须删除nginx.ingress.kubernetes.io/rewrite-target:
注释。
然后在你的道路上,你可以有这样的东西:
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.
✌️