ingress nginx 配置rewrite实现前后端分离
原创
©著作权归作者所有:来自51CTO博客作者小杨同学THY的原创作品,请联系作者获取转载授权,否则将追究法律责任
官网地址
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 -