Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 684
user1722908 Avatar asked Oct 31 '18 17:10

user1722908


3 Answers

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(/|$)(.*)
like image 114
Rico Avatar answered Oct 20 '22 17:10

Rico


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
like image 39
Kun Li Avatar answered Oct 20 '22 17:10

Kun Li


I met the similar issue recently.

Assuming the "logs" hyperlink in your html uses a relative path, which means the hyperlink doesn't start with '/', then I think you can try to hit the page at http://hostname/devops/ instead of http://hostname/devops. (Note a / suffix in the first url.).

Then the "logs" hyperlink will be formed as http://hostname/devops/logs.

I think it's about how the browser identifies the 'base' url. With accessing 'http://hostname/devops/, if there is no 'base' tag in the html header, the 'base' url will be figured out to be http://hostname/devops; while with 'http://hostname/devops', the base url will be http://hostname.

If it's not the case, there is discussion at https://github.com/kubernetes/ingress-nginx/issues/4149. It's suggested a workaround to use the nginx directive subs_filter with a configuration-snippet to make the href attributes relative and also add the base tag in the html header.

The ingress used is as below,

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(/|$)(.*)
like image 2
SZ Yang Avatar answered Oct 20 '22 15:10

SZ Yang