nginx-ingress support prefix to multiple routes

8/13/2019

I have a service which exposed on port 8888 and I'm trying to create a ingress object for my service with only path to support all hosts (without mention host). My issue is that my service has no base url and it has some other routes for the static files.

my ingress file:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/auth-realm: Authentication Required
    nginx.ingress.kubernetes.io/auth-secret: ingress-basic-auth-secret
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
  name: ingress-myapp
  namespace: default
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: myapp-service
          servicePort: 8888
        path: /myapp/?(.*)

with the above ingress object I have an issue because I can access to my web service on: <host ip>/myapp

but some of my static files like logos are not working because my app is using other routes and my browser is getting urls like: <host ip>/static/logo50x50.png and I need <host ip>/myapp/static/logo50x50.png

any way to tell the nginx-ingress to do a prefix or re write to my route so the nginx-ingress will convert the url: from: <host ip>/static/logo50x50.png --> to: <host ip>/myapp/static/logo50x50.png

so my client (chrome) will get: <host ip>/myapp/static/logo50x50.png

Remark: I need to use only 1 http rule path and not multiple http path rules in my ingress object, so another path like path: /static/?(.*) in the nginx-ingress is not help me, because I have more pods that has the same route /static/...

-- dsaydon
kubernetes
kubernetes-ingress
nginx
nginx-ingress

1 Answer

8/19/2019

Try to change your location rule to - path: /(myapp)?/?(.*) and rewrite annotation to: nginx.ingress.kubernetes.io/rewrite-target: /$2 So it will looks like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/auth-realm: Authentication Required
    nginx.ingress.kubernetes.io/auth-secret: ingress-basic-auth-secret
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
  name: ingress-myapp
  namespace: default
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: myapp-service
          servicePort: 8888
        path: /(myapp)?/?(.*)

Path rule will now match (almost) anything. Please look for official documentation of NGINX Ingress Controller.

-- muscat
Source: StackOverflow