nginx ingress sub path redirection

10/31/2018

I have an ingress controller and ingress resource running with all /devops mapped to devopsservice in the backend. When I try to hit "http://hostname/devops" things work and I get a page (although without CSS and styles) with a set of hyperlinks for e.g one of them is "logs".

When I click on the "logs" hyperlink, it is redirecting me to http://hostname/logs whereas I need it to be http://hostname/devops/logs.

Any idea what I can do?

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: my-ingress
 namespace: ingress-nginx
 annotations:
   kubernetes.io/ingress.class: nginx
   nginx.ingress.kubernetes.io/rewrite-target: /
   nginx.ingress.kubernetes.io/add-base-url : "true"
spec:
 rules:
 - host: master1.dev.local
   http:
     paths:
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops
-- user1722908
kubernetes
kubernetes-ingress
nginx
nginx-ingress

2 Answers

10/31/2018

Looks like your ingress is not serving anything /devops/*. Try adding another path /devops/* with the same backend. Basically this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: my-ingress
 namespace: ingress-nginx
 annotations:
   kubernetes.io/ingress.class: nginx
   nginx.ingress.kubernetes.io/rewrite-target: /
   nginx.ingress.kubernetes.io/add-base-url : "true"
spec:
 rules:
 - host: master1.dev.local
   http:
     paths:
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops/*
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops

Update: the above has been deprecated in favor of something like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: master1.dev.local
    http:
      paths:
      - backend:
          serviceName: devops1
          servicePort: 10311
        path: /devops(/|$)(.*)
-- Rico
Source: StackOverflow

11/1/2018

If you access http://hostname/devops/logs directly from your browser, certainly you will get what you want. But since you click the hyperlink in the homepage, then you can only get http://hostname/logs, which will be certainly failed.

So, you need /logs backend configured in your ingress yaml to get it processed, and configure nginx.ingress.kubernetes.io/configuration-snippet to ensure /logs not get rewrote, like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: my-ingress
 namespace: ingress-nginx
 annotations:
   kubernetes.io/ingress.class: nginx
   nginx.ingress.kubernetes.io/rewrite-target: /
   nginx.ingress.kubernetes.io/add-base-url : "true"
   nginx.ingress.kubernetes.io/configuration-snippet: |
     rewrite ^/logs /logs break;
spec:
 rules:
 - host: master1.dev.local
   http:
     paths:
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /logs
     - backend:
         serviceName: devops1
         servicePort: 10311
       path: /devops
-- Kun Li
Source: StackOverflow