nginxkuberneteskubernetes-ingressnginx-ingress

nginx ingress sub path redirection


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

Solution

  • 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