是否可以以任何方式将主机路径重定向到后端的子路径?类似于子路径对卷的工作方式。
入口如下所示:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: jupyter-notebook-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: jptrntbk.MYDOMAIN.com
http:
paths:
- path: /
backend:
serviceName: jupyter-notebook-service
servicePort: 8888
subPath: /lab
导航到jptrntbk.MYDOMAIN.com
将重定向到后端的/lab
,并且所有其他父路径都不可用。
使用应用根注释创建入口规则:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/app-root: /app1
name: approot
namespace: default
spec:
rules:
- host: approot.bar.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /
检查重写是否正常工作
$ curl -I -k http://approot.bar.com/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.11.10
Date: Mon, 13 Mar 2017 14:57:15 GMT
Content-Type: text/html
Content-Length: 162
Location: http://approot.bar.com/app1
Connection: keep-alive
或者是否可以使用重写注释创建入口规则:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /something(/|$)(.*)
在此入口定义中,(.*)
捕获的任何字符都将分配给占位符$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
来源: https://kubernetes.github.io/ingress-nginx/examples/rewrite/