In Kubernetes ingress, when use-regex is true, what's the correct way to define a route for /

9/8/2020

I need to define the following routes on my Kubernetes ingress :

  • my-website.com/* => web client
  • my-website.com/api/* => graphql api

I've tried to set the following but only the /api route works:

kind: Ingress
metadata:
  name: "my-ingress"
  annotations:
    "kubernetes.io/ingress.class": nginx
    "nginx.ingress.kubernetes.io/affinity": cookie
    "nginx.ingress.kubernetes.io/rewrite-target": "/$2"
    "nginx.ingress.kubernetes.io/use-regex": "true"
  spec:
    rules:
      - host: my-website.com
        http:
          paths:
            - path: "/api(/|$)(.*)"
              backend:
                serviceName: web-client
                servicePort: 80
            - path: "/"
              backend:
                serviceName: graphql-server
                servicePort: 80

What's the problem with the / route?

I've tried to use / and `/(.)` paths but none redirects to the correct service.*

-- bbenoist
kubernetes
kubernetes-ingress

1 Answer

9/8/2020

Could you after replace "nginx.ingress.kubernetes.io/rewrite-target": "/$2" to "nginx.ingress.kubernetes.io/rewrite-target: /"

Also change path from /api(/|$)(.*) to /api/(.*)

I am assuming web-client and graphql-server services are running fine.

in case you still face the issue, please share your ingress controller logs for more details

-- Deepak
Source: StackOverflow