Kubernetes Ingress path not finding resources

12/5/2018

I'm having some issues when using a path to point to a different Kubernetes service.

I'm pointing to a secondary service using the path /secondary-app and I can see through my logs that I am correctly reaching that service.

My issue is that any included resource on the site, let's say /css/main.css for example, end up not found resulting in a 404.

Here's a slimmed down version of my ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: 50m
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: my-app
spec:
  rules:
  - host: my-app.example.com
    http:
      paths:
      - backend:
        path: /
          serviceName: my-app
          servicePort: http
      - backend:
        path: /secondary-app        
          serviceName: secondary-app
          servicePort: http

I've tried a few things and haven't yet been able to make it work. Do I maybe need to do some apache rewrites?

Any help would be appreciated.

Edit - Solution

Thanks to some help from @mk_sta I was able to get my secondary service application working by using the nginx.ingress.kubernetes.io/configuration-snippet annotation like so:

  nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($request_uri = '/?%secondary-app') { rewrite /(.*) secondary-app/$1 break; }

It still needs a bit of tweaking for my specific app but that worked exactly how I needed it to.

-- Beans
apache
kubernetes
kubernetes-ingress

1 Answer

12/5/2018

I guess nginx.ingress.kubernetes.io/rewrite-target: / annotation in your Ingress configuration doesn't bring any success for multipath rewrite target paths, read more here. However, you can consider to use Nginx Plus Ingress controller, shipped with nginx.org/rewrites: annotation and can be used for pointing URI paths to multiple services as described in this example.

You can also think about using nginx.ingress.kubernetes.io/configuration-snippet annotation for the existing Ingress, which can adjust rewrite rules to Nginx location, something like:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: 50m
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite /first-app/(.*) $1 break;
      rewrite /secondary-app/(.*) /$1 break;
  name: my-app
spec:
  rules:
  - host: my-app.example.com
    http:
      paths:
      - backend:
        path: /first-app
          serviceName: my-app
          servicePort: http
      - backend:
        path: /secondary-app        
          serviceName: secondary-app
          servicePort: http
-- mk_sta
Source: StackOverflow