官网地址

​https://kubernetes.github.io/ingress-nginx/examples/rewrite/​

创建带有重写注解的 Ingress 规则:

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

在此入口定义中,捕获的任何字符​​(.*)​​​都将分配给占位符​​$2​​​,然后将其用作​​rewrite-target​​注释中的参数。

例如,上面的入口定义将导致以下重写:

  • ​rewrite.bar.com/something​​改写为​​rewrite.bar.com/​
  • ​rewrite.bar.com/something/​​改写为​​rewrite.bar.com/​
  • ​rewrite.bar.com/something/new​​改写为​​rewrite.bar.com/new​

应用根目录

$ echo "
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/app-root: /app1
name: approot
namespace: default
spec:
ingressClassName: nginx
rules:
- host: approot.bar.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: http-svc
port:
number: 80
" | kubectl create -f -