gpt4 book ai didi

kubernetes - 用Ingress在Kubernetes中重写URL

转载 作者:行者123 更新时间:2023-12-02 12:10:42 26 4
gpt4 key购买 nike

我正在将旧版网站迁移到Kubernetes,后者正在使用 www.app1.com www.app2.com和这样的URL。
由于所有这些都已部署在一个K8s集群中,因此我想使用www.myapp.com/app1等URL。但是,当我访问 www.myapp.com/app1 时,它将路由到 www.myapp.com/login 而不是 www.myapp.com/app1/login
试用1:

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/page2 之类的URL,而不是 www.myapp.com/app1/page1 www.myapp.com/app1/page2
看起来我必须在应用程序中执行某些操作,但是如果不更改应用程序代码就不能在入口中执行某些操作吗?
Update1:​​
入口日志:
在浏览器中,当我访问 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.
✌️

关于kubernetes - 用Ingress在Kubernetes中重写URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63399078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com