6

I deployed 2 front-end applications that are based on angular. I use ingress-nginx (k8s.gcr.io/ingress-nginx/controller:v0.44.0) with the following configurations to route the requests to these applications:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: portal-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - http:
      paths:
      - path: /?(.*)
        backend:
          serviceName: app1
          servicePort: 80
      - path: /app2
        backend:
          serviceName: app2
          servicePort: 80

When I hit the <ip-address>/ it is routing to app1, but when I hit <ip-address>/app2 it is routing back to app1.

aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
niranjan pb
  • 1,125
  • 11
  • 14

1 Answers1

11

According to the ingress-nginx documentation, the first step it follows is to order the paths in descending length, then it transforms these paths into nginx location blocks. nginx follows a first-match policy on these blocks.

In your case, you can provide both paths, and as /app2 is longer than /, it will be written first in the nginx configuration. Meaning /app2 will have a chance to be matched first (and send the traffic to app2), while everything else will go to app1. You don't need regexes to achieve this.

Code:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: portal-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: app1
          servicePort: 80
      - path: /app2
        backend:
          serviceName: app2
          servicePort: 80
aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50