具有混合位置的 Kubernetes Ingress
我有一个 kubernetes 入口,它有一个利用 nginx.ingress.kubernetes.io/rewrite-target 的服务。我有另一条路径,service1/query
,它需要重定向到 rewrite-target
指向之外的其他位置。假设它应该充当代理并将请求转发到 google.com 并返回结果。因为rewrite-target
适用于定义的所有路径,所以我不确定应该如何继续区分service1/query
的路径。
有什么想法吗?
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myApp
namespace: myApp
annotations:
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, OPTIONS"
nginx.ingress.kubernetes.io/cors-allow-origin: '$http_origin'
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
ingressClassName: nginx
tls:
- hosts:
- myhost.org
secretName: ingress-nginx-tls-cert
rules:
- host: myhost.org
http:
paths:
- path: /service1(/|$)(.*)
pathType: Prefix
backend:
service:
name: service1
port:
number: 80
- path: /service1/query
pathType: Prefix
I have a kubernetes ingress that has a service taking advantage of nginx.ingress.kubernetes.io/rewrite-target
. I have another path, service1/query
that needs to redirect somewhere other than what the rewrite-target
is pointing to. Let's say it should act as a proxy and forward the request to google.com and return the result. Because the rewrite-target
is for all paths defined, I'm not sure how I should proceed to differentiate the path for service1/query
.
Any ideas?
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myApp
namespace: myApp
annotations:
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, OPTIONS"
nginx.ingress.kubernetes.io/cors-allow-origin: '$http_origin'
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
ingressClassName: nginx
tls:
- hosts:
- myhost.org
secretName: ingress-nginx-tls-cert
rules:
- host: myhost.org
http:
paths:
- path: /service1(/|$)(.*)
pathType: Prefix
backend:
service:
name: service1
port:
number: 80
- path: /service1/query
pathType: Prefix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你已经快到了,你只需要改变路径的顺序。 入口路径匹配
当您添加
时/service1(/|$)(.*)
到第一行,/service1/query
是一个死代码;因为它进入了第一行。但是,如果您将
/service1/query
放在第一个; nginx 会首先查看它。You are almost there, you just need to change the paths' order. Ingress Path Matching
When you add
/service1(/|$)(.*)
to the first line, the/service1/query
is a dead code; because it enters the first line.However, if you put
/service1/query
to the first; nginx will look at it first.