问题
paths:
- backend:
serviceName: booknotes-front-end-service
servicePort: 80
path: /
- backend:
serviceName: booknotes-back-end-service
servicePort: 3000
path: /api
Here is a rules in my ingres-nginx resource . I try to direct all traffic which starts from /api
to my back end service, which works properly, but if some route in my back end will be like /api/users it doesn't work , my back end send response not found , when I run it locally this route working properly. Also I've tried delete /api prefix from my koa routing and change it to /users
and then I've also changed path: /api
to path: /users
and this stuff are working properly. What should I do for fixing it ? If you need additional info , pls let me know !
回答1:
Which version of nginx-ingress are you using? They changed the way for defining a path.
https://kubernetes.github.io/ingress-nginx/examples/rewrite/
Starting in Version 0.22.0, ingress definitions using the annotation nginx.ingress.kubernetes.io/rewrite-target are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a capture group.
For example you can use a definition like this.
kind: Ingress
metadata:
name: some-ingress-name
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- path: /?(.*)
backend:
serviceName: booknotes-front-end-service
servicePort: 80
- path: /api/?(.*)
backend:
serviceName: booknotes-back-end-service
servicePort: 3000
回答2:
That's because it is searching for the file /api/users
, which probably doesn't exist.
Put a file in /api/users/
, in the backends of the service booknotes-back-end-service
, say user1
, and make the requeat explicitly to /api/users/user1
.
You should get 200 there.
回答3:
https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer , section step6 try replacing path /api with /api/* and / with /*
来源:https://stackoverflow.com/questions/58673051/ingress-routing-api-prefix-issue