Search code examples
spring-bootkubernetes-ingressnginx-ingresscustom-error-pages

Getting Spring boots 404 using Ingress: /api/user in Kubernetes


recently I deployed my Spring boot API in local kubernetes(K3D), all works fine until I specify an ingress with path: /api/user. That ingress path localhost:9080/api/user returns Spring boots 404, so not nginx 404. When reducing the path to /user it works.

not working ingress yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: user-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /api/user
        pathType: Prefix
        backend:
          service:
            name: user-service
            port:
              number: 8094

the yaml above gives the Whitelabel error page from Spring boot. So it does get through but not with the right path for Spring boot?

working ingress yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: user-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /user
        pathType: Prefix
        backend:
          service:
            name: user-service
            port:
              number: 8094

Above works.

Why does it break when adding /api in path?

I have searched for related problems/solutions but found none.

According to docs it should work:https://kubernetes.io/docs/concepts/services-networking/ingress/


Solution

  • Like @YvanG. said, there was something wrong with my configuration in Spring Boot. I had to set the request mapping to: RequestMapping("/api/user") in my controller. Then it worked.