Kubernetes 入口路由到 Nextjs 应用程序的问题 [英] Issue with Kubernetes ingress routing to Nextjs applications

查看:12
本文介绍了Kubernetes 入口路由到 Nextjs 应用程序的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个有趣的用例.我在我的 Kubernetes 集群上运行多个微服务.我的应用程序使用 NextJS 对 _next 路由进行内部调用.

我的问题来自于我需要一种方法来区分服务及其对 _next 文件的请求.所以我实现了 NextJS 的assetPrefix 功能,该功能在开发中完美运行,将我的前缀附加到 _next 前面,因此请求看起来像 .../${PREFIX}/_next/....这样,我可以根据集群上相应服务的前缀设置入口和路由文件.我按照本指南设置了 Kubernetes Ingress 控制器:https://akomljen.com/kubernetes-nginx-ingress-controller/

我的入口配置是:

apiVersion: extensions/v1beta1种类:入口元数据:名称:开发入口规格:规则:- 主机:baseurl.com网址:路径:- 路径:/auth后端:服务名称:auth-svc服务端口:80- 路径:/static/auth后端:服务名称:auth-svc服务端口:80- 路径:/登录后端:服务名称:auth-svc服务端口:80- 路径:/设置后端:服务名称:auth-svc服务端口:80- 路径:/艺术品后端:服务名称:artwork-svc服务端口:80- 路径:/静态/艺术品后端:服务名称:artwork-svc服务端口:80

所以这就是问题所在.现在一切都已设置,正确部署,并且入口正在按照上述指南并使用上述规则运行,我的服务正在尝试向 .../_next/... 发出请求的 .../${PREFIX}/_next/... 所以他们找不到正确的文件并且没有任何工作.我似乎无法弄清楚发生了什么.有人有想法么?提前致谢!

解决方案

您正在使用内置的 NGINX Ingress Controller,不幸的是,它没有这样的功能.

如果你能负担得起,我的建议是使用 NGINX Plus Ingress Controller 注释功能.

您可以在这里找到官方示例.p>

例子:

apiVersion: extensions/v1beta1种类:入口元数据:名称:咖啡厅入口注释:nginx.org/rewrites: "serviceName=tea-svc rewrite=/;serviceName=coffee-svc rewrite=/beans/"规格:规则:- 主机:cafe.example.com网址:路径:- 路径:/茶/后端:服务名称:tea-svc服务端口:80- 路径:/咖啡/后端:服务名称:咖啡-svc服务端口:80

以下是对 tea-svc 请求的 URI 如何重写的示例(注意 /tea 请求被重定向到 /tea/).

/tea/->//茶/abc->/abc

以下是对 coffee-svc 请求的 URI 如何重写的示例(注意 /coffee 请求被重定向到 /coffee/).

/coffee/->/豆子//咖啡/abc->/豆类/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆