Kubernetes Ingress configuration rewrite problem

4/11/2019

I'm creating a configuration to host some apps in a Kubernetes cluster on AWS. I have two different apps, with separate service/pod/selector but I want to expose them with a single ingress for the moment.

So I created the following ingress controller

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /foo
        backend:
          serviceName: foo
          servicePort: 8080
      - path: /bar
        backend:
          serviceName: bar
          servicePort: 8080 

and the ingress obtain the ELB from AWS without any problem, but when I try to browse the app (Java application using Tomcat appserver) I always receive the following page

Tomcat

It's the classic old Tomcat welcome page but every request always returns the index.html (no css/img loaded) and also if I try to use the correct context path for the application I receive this page.

If I expose the apps using a Service (LoadBalancer) I can use it without these problems, so I think there is something wrong with ingress configuration.

Any ideas?


UPDATE

If I use an ingress with a single path like this

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: foo
          servicePort: 8080

Using INGRESSHOST url I can see the Tomcat home with img/css and if I browse to INGRESSHOST/APPCONTEXT I can use the app without problem

-- Federico Paparoni
amazon-eks
kubernetes
kubernetes-ingress
nginx-ingress
tomcat

1 Answer

4/11/2019

If you have recently changed the version of your nginx-ingress controller then maybe the cause can be a recent change done to it. Now it uses regex rewrite rules and maybe your rewrite target is just always being rewritten to "/". I think the changes were introduced in version 0.22 in January.

The new correct syntax for your ingress would be:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - http:
      paths:
      - path: /foo(.*)
        backend:
          serviceName: foo
          servicePort: 8080
      - path: /bar(.*)
        backend:
          serviceName: bar
          servicePort: 8080 
-- Yervand Aghababyan
Source: StackOverflow