4

I have a ReactJS front end, Spring boot backend app deployed on a baremetal Kubernetes cluster which is running Kubernetes Ingress and requests are proxied to it by HAProxy. When visiting the URL of the app, I can see it loads the index.html of the app but all other requests to static assets are not done properly.

The ingress resource of my app:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: app
name: app-ingress
annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$3
spec:
rules:
- host: devops
    http:
    paths:
    - path: /dev/app1(/|$)(.*)
        backend:
        serviceName: app1
        servicePort: 80

When inspecting the page which loads using Chrome Developer tools, I see that there are 6 outgoing calls to the static assets. The call that retrieves the index.html of the app completes succesfully but the calls that retrieve the static assets (ex: http://devops/dev/app1/static/js/4.2761693d.chunk.js) does not work properly as it retrieves the index.html page as well. (only the index.html page is served by all calls basically)

I had a feeling it is because of the nginx.ingress.kubernetes.io/rewrite-target annotation but removing it causes a 404 even on the index.html page.

I am using nginx ingress controller 0.25.1

EDIT:

This is the output when I exec into the container and run curl localhost:8080/dev/app1/static/js/4.2761693d.chunk.js (error fallback page)

enter image description here

This is the output when I run curl localhost:8080/tatic/js/4.2761693d.chunk.js (correctly loads the css)

enter image description here

user3583252
  • 493
  • 1
  • 5
  • 15
  • Is your Spring app serving static assets dir on `/static/` route ? – Ankit Deshpande Aug 16 '19 at 11:26
  • @AnkitDeshpande on a "regular" deploy the Spring app will serve static assets on the route defined in the `homepage` field in the `package.json` file. For this deployment I have entered `http://devops/dev/app1` as the value for the `homepage` field – user3583252 Aug 16 '19 at 11:38
  • Also, by regular deploy, I meant deploying it manually, i.e. building jar and `java -jar jar_name.jar` – user3583252 Aug 16 '19 at 11:38
  • 1
    Try connecting to pod and run `curl localhost/dev/app1/static/js/4.2761693d.chunk.js`, what does this return ? – Ankit Deshpande Aug 16 '19 at 13:42
  • Could you please check the edit in which I posted screenshots of the curl commands. Apologize for the delay – user3583252 Aug 16 '19 at 18:36
  • 1
    check if this is helpful: https://stackoverflow.com/a/47860209/3968921 – Ankit Deshpande Aug 16 '19 at 20:48
  • Hi thanks for the link. Could you check my answer below? I got it to work, idk how it is working, would like an explanation if possible – user3583252 Aug 17 '19 at 20:45

3 Answers3

1

Somehow, when I change the rewrite annotation to this, it works:

nginx.ingress.kubernetes.io/rewrite-target: /$2

I didnt change anything else.

Now the application is accessible at devops/dev/app1/ (but it does not work without the / at the end)

I am not sure how this works. I had no logic behind it, I was just changing values in the ingress file to see if anything works.

Can someone explain why it works?

user3583252
  • 493
  • 1
  • 5
  • 15
  • The line "nginx.ingress.kubernetes.io/rewrite-target: /$3" was telling the ingress to use the values from the 3rd group, when you only had 2 groups in the path: "/dev/app1(/|$)(.*)". I.e.(/|$) -> $1 and (.*) -> $2 – Mark Mar 04 '21 at 02:53
0

You are most likely using rewrite target for wrong reasons. In this case all elements should start with /dev/app1 including inner calls. What you are doing is working for index page because you wrote it to have "/dev/app1" in before and ingress redirected it to "/" but inner call just called "/static/js/4.2761693d.chunk.js" . This caused the problem basically, because ingress didn't know the route therefore your service never get called to get the js file.

Akin Ozer
  • 1,001
  • 6
  • 14
  • Hi thanks for the answer. That's what I thought too, but then if I added `nginx.ingress.kubernetes.io/rewrite-target: /dev/app1/$3`, shouldn't that solve the problem sort of? Also if I remove the annotation altogether, even the `index.html` page does not load – user3583252 Aug 16 '19 at 18:38
  • Apologies for the delay in responding – user3583252 Aug 16 '19 at 18:38
  • If without `rewrite-target`, it didn't work, how did you come up with using `rewrite-target` ? – Ankit Deshpande Aug 16 '19 at 20:49
0

Im using kubernetes 1.25.2 along with Nginx-Ingress v2.4.1 https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/

Just us the below 2...the Path should have / at the end.

annotations: nginx.org/rewrites: "serviceName=app1-svc rewrite=/;serviceName=app2-svc rewrite=/"

spec: path: /app1/

below is my Ingress resource file manifest...

apiVersion: networking.k8s.io/v1 
kind: Ingress 
metadata:
  name: relouat-ingress
  namespace: relouat
  annotations:    
    #nginx.ingress.kubernetes.io/rewrite-target: /$2
     nginx.org/rewrites: "serviceName=app1-svc rewrite=/;serviceName=app2-svc rewrite=/"     
spec:
  ingressClassName: nginx  
  rules:
  - host: uat.relo.com
    http:
      paths:
      - backend:
          service:
            name: app1-svc
            port:
              number: 2041
        path: /app1/
        pathType: Prefix
      - backend:
          service:
            name: app2-svc
            port:
              number: 2042
        path: /app2/
        pathType: Prefix