在将请求转发到服务之前删除匹配后的前缀
Remove prefix after match before forwarding request to service
我有这个底座urlapi.example.com
因此,ingress-nginx
将收到 api.example.com
的请求,并且它应该执行以下操作。
转发api.example.com/customer
到customer-srv
它不起作用,它将整个 mtach 转发给 customer-srv 即 /customer/requested_url
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: api.example.in
http:
paths:
- path: /customer/?(.*)
pathType: Prefix
backend:
service:
name: customer-srv
port:
number: 3000
我尝试使用重写注释,但这也不起作用,但这有效,但这不是我想要实现的。
paths:
- path: /?(.*)
pathType: Prefix
backend:
service:
name: customer-srv
port:
number: 3000
例如,
api.example.com/customer
应该去 http://localhost:3000
而不是 http://localhost:3000/customer
这是我使用的 yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: api.example.in
http:
paths:
- path: /customer/?(.*)
pathType: Prefix
backend:
service:
name: customer-srv
port:
number: 3000
出于测试目的,我创建了一个回显服务器:
kubectl run --image mendhak/http-https-echo customer
然后是服务:
kubectl expose po customer --name customer-srv --port 3000 --target-port 80
我检查了出口ip:
$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-service <none> api.example.in 192.168.39.254 80 3m43s
然后我做了一个卷曲来检查它:
curl 192.168.39.254/customer/asd -H "Host: api.example.in"
{
"path": "/asd",
"headers": {
"host": "api.example.in",
...
}
请注意,回显服务器回显了它接收到的路径,并且因为它接收到的路径已从 /customer/asd
重写为 /asd
,所以它显示了这个正确的路径 (/asd)。
如您所见,这确实有效。
我有这个底座urlapi.example.com
因此,ingress-nginx
将收到 api.example.com
的请求,并且它应该执行以下操作。
转发api.example.com/customer
到customer-srv
它不起作用,它将整个 mtach 转发给 customer-srv 即 /customer/requested_url
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: api.example.in
http:
paths:
- path: /customer/?(.*)
pathType: Prefix
backend:
service:
name: customer-srv
port:
number: 3000
我尝试使用重写注释,但这也不起作用,但这有效,但这不是我想要实现的。
paths:
- path: /?(.*)
pathType: Prefix
backend:
service:
name: customer-srv
port:
number: 3000
例如,
api.example.com/customer
应该去 http://localhost:3000
而不是 http://localhost:3000/customer
这是我使用的 yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: api.example.in
http:
paths:
- path: /customer/?(.*)
pathType: Prefix
backend:
service:
name: customer-srv
port:
number: 3000
出于测试目的,我创建了一个回显服务器:
kubectl run --image mendhak/http-https-echo customer
然后是服务:
kubectl expose po customer --name customer-srv --port 3000 --target-port 80
我检查了出口ip:
$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-service <none> api.example.in 192.168.39.254 80 3m43s
然后我做了一个卷曲来检查它:
curl 192.168.39.254/customer/asd -H "Host: api.example.in"
{
"path": "/asd",
"headers": {
"host": "api.example.in",
...
}
请注意,回显服务器回显了它接收到的路径,并且因为它接收到的路径已从 /customer/asd
重写为 /asd
,所以它显示了这个正确的路径 (/asd)。
如您所见,这确实有效。