示例:apiVersion: extensions/v1beta1kind: Ingressmetadata: name: cafe-ingress annotations: nginx.org/rewrites: "serviceName=tea-svc rewrite=/;serviceName=coffee-svc rewrite=/beans/"spec: rules: - host: cafe.example.com http: paths: - path: /tea/ backend: serviceName: tea-svc servicePort: 80 - path: /coffee/ backend: serviceName: coffee-svc servicePort: 80下面是如何重写对tea-svc的请求的URI的示例(请注意,将/tea请求重定向到/tea/)./tea/ -> //tea/abc -> /abc下面是如何重写对coffee-svc的请求的URI的示例(请注意,将/coffee请求重定向到/coffee/)./coffee/ -> /beans//coffee/abc -> /beans/abcSo I have an interesting use case. I am running multiple micro-services on my Kubernetes cluster. My applications use NextJS which make internal calls to _next routes.My issue came from the fact that I needed a way to differentiate between services and their requests to the _next files. So I implemented NextJS's assetPrefix feature which works perfectly in development, appending my prefix in front of _next so the requests look like .../${PREFIX}/_next/.... That way I could set up an ingress and route files base on the prefix to the appropriate service on my cluster. I set up a Kubernetes Ingress controller following this guide: https://akomljen.com/kubernetes-nginx-ingress-controller/My ingress config is: apiVersion: extensions/v1beta1kind: Ingressmetadata: name: dev-ingressspec: rules: - host: baseurl.com http: paths: - path: /auth backend: serviceName: auth-svc servicePort: 80 - path: /static/auth backend: serviceName: auth-svc servicePort: 80 - path: /login backend: serviceName: auth-svc servicePort: 80 - path: /settings backend: serviceName: auth-svc servicePort: 80 - path: /artwork backend: serviceName: artwork-svc servicePort: 80 - path: /static/artwork backend: serviceName: artwork-svc servicePort: 80So here is the problem. Now that everything is set up, properly deployed, and the ingress is running following the above guide and using the above rules, my services are trying to make requests to .../_next/... instead of .../${PREFIX}/_next/... so they can't find the proper files and nothing is working. I cannot seem to figure out what is going on. Anyone have any ideas? Thanks in advance! 解决方案 You are using built-in NGINX Ingress Controller that, unfortunately, has no such functionality.My advice is to use NGINX Plus Ingress Controller annotation functionality if you can afford it.You can find official example here.Example:apiVersion: extensions/v1beta1kind: Ingressmetadata: name: cafe-ingress annotations: nginx.org/rewrites: "serviceName=tea-svc rewrite=/;serviceName=coffee-svc rewrite=/beans/"spec: rules: - host: cafe.example.com http: paths: - path: /tea/ backend: serviceName: tea-svc servicePort: 80 - path: /coffee/ backend: serviceName: coffee-svc servicePort: 80Below are the examples of how the URI of requests to the tea-svc are rewritten (Note that the /tea requests are redirected to /tea/)./tea/ -> //tea/abc -> /abcBelow are the examples of how the URI of requests to the coffee-svc are rewritten (Note that the /coffee requests are redirected to /coffee/)./coffee/ -> /beans//coffee/abc -> /beans/abc 这篇关于Kubernetes入口路由到Nextjs应用程序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!