1

I have two backend api-services:

  1. api-service-v1
  2. api-service-v2

Both respond on "/api/" path

I have this configuration running fine as a docker-compose setup where nginx service serves as a proxy with the following config file

/etc/nginx/conf.d/default.conf:

server {
    ...

    location /apiv1/ {
        proxy_pass http://api-service-v1/api/;
    }
    ...

    location /apiv2/ {
        proxy_pass http://api-service-v2/api/;
    }
    ...
}

Now I'd like to deploy the setup to k8s cluster. I got stuck with ingress configuration. ingress.yml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dev-ingress
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"

spec:
  rules:
    - host: demo.com
      http:
        paths:
          - pathType: Prefix
            path: "/"
            backend:
              service:
                name: frontend-service
                port:
                  number: 80

          - pathType: Prefix
            path: "/apiv1/"
            backend:
              service:
                name: api-service-v1
                port:
                  number: 80

          - pathType: Prefix
            path: "/apiv2/"
            backend:
              service:
                name: api-service-v2
                port:
                  number: 80

Path "/" works fine. But paths "/apiv1/" and "/apiv2/" do not work.

Please help.

Thanks!

1 Answers1

0

The ingress will just forward the entire path to the service.

E.g. your api-service-v1 will get hit with urls such as https://yourhost/apiv1/asdfq

If you want the ingress to modify the path used to hit the service, you need to tell it to rewrite, as described in: https://kubernetes.github.io/ingress-nginx/examples/rewrite/

Change path of api-service paths to /apiv1(/|$)(.*) and /apiv2(/|$)(.*) respectively.

Then add nginx.ingress.kubernetes.io/rewrite-target: /api$1$2 annotation.

Unfortunately that also means your / needs to be in another ingress otherwise requests for frontend will also get rewritten to hit /api/ in your frontend-service.