比如我想起吊 url 中的 /a

1
2
3
4
curl test.example.com/a/b/xxx/xxx

# 实际请求到后端的路径:
/b/xxx/xxx

方法一、使用 Nginx 配置片段

1
2
3
location /a/b/ {
proxy_pass http://a.default/b/;
}

但是这种方式需要修改 ingress nginx 开启支持 nginx 配置的功能,详情见 ingress-nginx 使用自定义的nginx配置

方法二、使用 ingress nginx 原生配置

需要去除 context path 的路径,创建专门的 ingress,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: a
namespace: default
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
ingressClassName: private-nginx
rules:
- host: test.example.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: a
port:
number: 80

---
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: b
namespace: default
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
nginx.ingress.kubernetes.io/rewrite-target: /b/$2
spec:
ingressClassName: private-nginx
rules:
- host: test.example.com
http:
paths:
- path: /a/b(/|$)(.*)
pathType: ImplementationSpecific
backend:
service:
name: b
port:
number: 80