skip to Main Content

Ingress Nginx controller is returning 404 Not Found for the React application. I narrowed it down to the React app because if I try to hit posts.com/posts, it actually returns the JSON list of existing posts, but for the frontend app it keeps showing
GET http://posts.com/ 404 (Not Found)

I looked to some other stackoverflow questions, to no avail unfortunately.

ingress-srv.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-srv
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "use"
spec:
  rules:
    - host: posts.com
      http:
        paths:
          - path: /posts/create
            pathType: Prefix
            backend:
              service:
                name: posts-clusterip-srv
                port:
                  number: 4000
          - path: /posts
            pathType: Prefix
            backend:
              service:
                name: query-srv
                port:
                  number: 4002
          - path: /posts/?(.*)/comments
            pathType: Prefix
            backend:
              service:
                name: comments-srv
                port:
                  number: 4001
          - path: /?(.*)
            pathType: Prefix
            backend:
              service:
                name: client-srv
                port:
                  number: 3000

client-depl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: client-depl
spec: 
  replicas: 1
  selector:
    matchLabels:
      app: client
  template:
    metadata:
      labels:
        app: client
    spec:
      containers:
        - name: client
          image: brachikaa/client
          imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: client-srv
spec:
  selector: 
    app: client
  ports: 
    - name: client
      protocol: TCP
      port: 3000
      targetPort: 3000
          

frontend Dockerfile

FROM node:alpine

ENV CI=true

WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./ 

CMD ["npm", "start"]

Logging the pod:

Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  11m   default-scheduler  Successfully assigned default/client-depl-f7cf996cf-cvh6m to minikube
  Normal  Pulling    11m   kubelet            Pulling image "brachikaa/client"
  Normal  Pulled     11m   kubelet            Successfully pulled image "brachikaa/client" in 42.832431635s
  Normal  Created    11m   kubelet            Created container client
  Normal  Started    11m   kubelet            Started container client

If you need any other logs, I will gladly provide them. Thanks.

3

Answers


  1. Chosen as BEST ANSWER

    so the issue was a really dumb one - I actually set nginx.ingress.kubernetes.io/use-regex: "use" instead of nginx.ingress.kubernetes.io/use-regex: "true"... After three days of checking through the documentation, I finally found it. If anyone encounters a similar problem - there you have it.


  2. In your yamls, there is a path "/?…" for handling the query parameters but this path will not receive traffic from "/" path as there is no prefix match. So you have to create a path "/" with type prefix to solve the issue. Then you can ignore current "/?…" path as it will match prefix with "/" path.

    Please try this:

    ingress-srv.yaml
    __________________
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-srv
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/use-regex: "use"
    spec:
      rules:
        - host: posts.com
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: client-srv
                    port:
                      number: 3000
              - path: /posts/create
                pathType: Prefix
                backend:
                  service:
                    name: posts-clusterip-srv
                    port:
                      number: 4000
              - path: /posts
                pathType: Prefix
                backend:
                  service:
                    name: query-srv
                    port:
                      number: 4002
              - path: /posts/?(.*)/comments
                pathType: Prefix
                backend:
                  service:
                    name: comments-srv
                    port:
                      number: 4001
              - path: /?(.*)
                pathType: Prefix
                backend:
                  service:
                    name: client-srv
                    port:
                      number: 3000
    
    Login or Signup to reply.
  3. You have to add following rules in your spec

              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: client-srv
                    port:
                      number: 3000
    

    This matches all paths.

    Reference – https://kubernetes.io/docs/concepts/services-networking/ingress/#examples

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search