0

I have just joined an ongoing project which is using a Spring Boot app. After moving the app on k8s I still have an issue as the app always redirects to /login without taking into account the ingress prefix.

My ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
  name: health-app-core
  namespace: dev
spec:
  ingressClassName: traefik
  rules:
  - http:
      paths:
      - backend:
          service:
            name: health-app-core
            port:
              number: 8080
        path: /core/dev
        pathType: Prefix
status:
  loadBalancer:
    ingress:
    - ip: <MY_IP>

From a pod inside the cluster I am able to call the service directly:

root@testpod:/# curl health-app-core.dev.svc.cluster.local:8080/login -v
*   Trying 10.43.12.178:8080...
* Connected to health-app-core.dev.svc.cluster.local (10.43.12.178) port 8080 (#0)
> GET /login HTTP/1.1
> Host: health-app-core.dev.svc.cluster.local:8080
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 200
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< Content-Type: text/html;charset=UTF-8

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Please sign in</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <link href="https://getbootstrap.com/docs/4.0/examples/signin/signin.css" rel="stylesheet" crossorigin="anonymous"/>
  </head>
  <body>
     <div class="container">
      <form class="form-signin" method="post" action="/login">
        <h2 class="form-signin-heading">Please sign in</h2>
        <p>
          <label for="username" class="sr-only">Username</label>
          <input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus>
        </p>
        <p>
          <label for="password" class="sr-only">Password</label>
          <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
        </p>
<p><input type='checkbox' name='remember-me'/> Remember me on this computer.</p>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
      </form>
</div>
* Connection #0 to host health-app-core.dev.svc.cluster.local left intact

but when calling from outside it redirects to <MY_IP>/login instead of <MY_IP>/core/dev/login

curl http://<MY_IP>/core/dev/login -v -L
*   Trying <MY_IP>:80...
* Connected to <MY_IP> (<MY_IP>) port 80 (#0)
> GET /core/dev/login HTTP/1.1
> Host: <MY_IP>
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 302 Found
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Content-Length: 0
< Date: Mon, 24 Jul 2023 07:27:16 GMT
< Expires: 0
< Location: http://<MY_IP>/login
< Pragma: no-cache
< Set-Cookie: JSESSIONID=3213418BD55DA8E002246E8B9107DBDA; Path=/; HttpOnly
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< X-Content-Type-Options: nosniff
< X-Xss-Protection: 1; mode=block
<
* Connection #0 to host <MY_IP> left intact
* Issue another request to this URL: 'http://<MY_IP>/login'
* Found bundle for host: 0x600003748840 [serially]
* Can not multiplex, even if we wanted to
* Re-using existing connection #0 with host <MY_IP>
> GET /login HTTP/1.1
> Host: <MY_IP>
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff

404 page not found
Mr.Sparkle
  • 173
  • 1
  • 9
  • Have you tried using [rewrite annotation](https://www.appsloveworld.com/springboot/100/41/kubernetes-nginx-redirect-to-user-specified-url#google_vignette)? Have you checked this [thread](https://stackoverflow.com/questions/32927937/how-to-set-base-url-for-rest-in-spring-boot). – Sai Chandra Gadde Jul 25 '23 at 05:10
  • Actually I have solved the issue: - I have added a Traefik middleware stripping the path (and putting in X-Forward-Path header) - Used server.forward-headers-strategy=FRAMEWORK in the spring app to make it aware of the new header.Going to post the full solution soon – Mr.Sparkle Jul 25 '23 at 19:29
  • If this solved your issue, can you please provide the resolution steps you have followed and provide it as answer and accept it for the greater visibility of community. – Sai Chandra Gadde Jul 26 '23 at 11:37

0 Answers0