入口路由api前缀问题 [英] ingress routing api prefix issue
问题描述
paths:
- backend:
serviceName: booknotes-front-end-service
servicePort: 80
path: /
- backend:
serviceName: booknotes-back-end-service
servicePort: 3000
path: /api
这是我的ingres-nginx资源中的一条规则.我尝试将所有从/api
开始的流量定向到我的后端服务,该服务正常运行,但是如果后端中的某些路由类似于/api/users,则该路由不起作用,则找不到后端发送响应,当我在本地运行该路线时,该路线即可正常运行.另外,我尝试从我的koa路由中删除/api前缀,并将其更改为/users
,然后又将path: /api
更改为path: /users
,并且这些东西可以正常工作.我该如何解决?如果您需要其他信息,请告诉我!
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 !
推荐答案
您使用的是哪个版本的nginx-ingress? 他们改变了定义路径的方式.
Which version of nginx-ingress are you using? They changed the way for defining a path.
https://kubernetes.github.io/ingress-nginx/examples/rewrite /
从0.22.0版开始,使用注解nginx.ingress.kubernetes.io/rewrite-target的入口定义与以前的版本不向后兼容.在0.22.0及更高版本中,必须在捕获组中显式定义请求URI中需要传递到重写路径的所有子字符串.
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
这篇关于入口路由api前缀问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!