Issue with Kubernetes ingress routing to Nextjs applications

7/18/2018

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!

-- L. Norman
kubernetes
kubernetes-ingress
next.js

1 Answer

7/19/2018

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
-- VKR
Source: StackOverflow