Kubernetes入口路由到Nextjs应用程序的问题 [英] Issue with Kubernetes ingress routing to Nextjs applications
问题描述
所以我有一个有趣的用例.我正在Kubernetes集群上运行多个微服务.我的应用程序使用NextJS进行内部调用_next路由.
我的问题来自以下事实:我需要一种区分服务和它们对_next文件的请求的方法.因此,我实现了NextJS的assetPrefix功能,该功能在开发中非常有效,将我的前缀附加到_next的前面,因此请求看起来像.../${PREFIX}/_next/...
.这样,我可以设置一个入口并将基于前缀的文件路由到群集上适当的服务.我按照此指南设置了Kubernetes Ingress控制器: https://akomljen.com/kubernetes- nginx-ingress-controller/
我的入口配置是:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: dev-ingress
spec:
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: 80
这就是问题所在.现在一切都已设置,正确部署,并且入口正在按照上述指南并使用上述规则运行,我的服务正在尝试向.../_next/...
而不是.../${PREFIX}/_next/...
发出请求,以便它们找不到正确的文件而且没有任何效果.我似乎无法弄清楚发生了什么.有人有想法么?预先感谢!
很遗憾,您正在使用内置的NGINX Ingress Controller,但它没有此类功能.
我的建议是,如果可以的话,使用 NGINX Plus入口控制器注释功能.
您可以在此处找到官方示例.>
示例:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
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/abc
So 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/v1beta1
kind: Ingress
metadata:
name: dev-ingress
spec:
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: 80
So 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/v1beta1
kind: Ingress
metadata:
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
Below 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 -> /abc
Below 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应用程序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!