Kubernetes 的Ingress的Rewrite annotations
  TEZNKK3IfmPf 2023年11月14日 17 0

Kubernetes 的Ingress的Rewrite annotations

2.实验

!!! attention Starting in Version 0.22.0, ingress definitions using the annotation nginx.ingress.kubernetes.io/rewrite-target are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a capture group.

!!! note Captured groups are saved in numbered placeholders, chronologically, in the form $1, $2 … $n. These placeholders can be used as parameters in the rewrite-target annotation.

Create an Ingress rule with a rewrite annotation:

2.1官方使用

echo '
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- path: /something(/|$)(.*)
pathType: Prefix
backend:
service:
name: http-svc
port:
number: 80
' | kubectl create -f -

In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.

For example, the ingress definition above will result in the following rewrites:

rewrite.bar.com/something rewrites to rewrite.bar.com/
rewrite.bar.com/something/ rewrites to rewrite.bar.com/
rewrite.bar.com/something/new rewrites to rewrite.bar.com/new

2.2我的使用

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-lys
namespace: d1ad16990e484ab7838f5922e1d4b78d
annotations:
nginx.ingress.kubernetes.io/rewrite-target: "/$1"
spec:
rules:
- host: foo.lys.com
http:
paths:
- path: /test/(.*)$
pathType: Prefix
backend:
service:
name: lys-test-service
port:
number: 8081
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月14日   35   0   0 kubernetes
TEZNKK3IfmPf