Issue with Kubernetes ingress routing to Nextjs applications

余生颓废 提交于 2019-12-01 11:10:53

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/51411662/issue-with-kubernetes-ingress-routing-to-nextjs-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!