Issue with Kubernetes ingress routing to Nextjs applications

0 votes

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 

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

The problem is 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.

Sep 11, 2018 in Kubernetes by lina
• 8,110 points
227 views

1 answer to this question.

0 votes

You’re using nginx ingress controller which does not hav e this facility. You have to use nginx plus ingress controller for this, Its just the upgraded version with additional features.

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
answered Sep 11, 2018 by Kalgi
• 42,330 points

Related Questions In Kubernetes

0 votes
1 answer

Kubernetes Ingress Path only works with /

Have you changed the context path in ...READ MORE

answered Sep 7, 2018 in Kubernetes by Kalgi
• 42,330 points
284 views
0 votes
1 answer

Kubernetes ingress IP is set to docker0 ip

The issue here was the kubelet configuration. ...READ MORE

answered Sep 10, 2018 in Kubernetes by Kalgi
• 42,330 points
41 views
0 votes
2 answers

Not able to expose port 80 on the host, kubernetes ingress

I was facing the same error. The nginix ...READ MORE

answered Sep 11, 2018 in Kubernetes by Kalgi
• 42,330 points
86 views
0 votes
1 answer

Kubernetes nginx-ingress TLS issue

You have to create a secret named test-secret. ➜ ...READ MORE

answered Sep 11, 2018 in Kubernetes by Kalgi
• 42,330 points
555 views
0 votes
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17 in Others by anonymous
2,984 views
+3 votes
1 answer
0 votes
2 answers

Deny access to some specific paths while using kubernetes ingress

Try to create two Ingresses first by default ...READ MORE

answered Sep 21, 2018 in Kubernetes by Nilesh
• 6,880 points
1,344 views
0 votes
1 answer

Customize the routing logic in kubernetes ingress controller

Try building your own customized image based on ...READ MORE

answered Sep 7, 2018 in Kubernetes by Kalgi
• 42,330 points
170 views