Rewrite only specific route with nginx Ingress

8/13/2018

I have three services running in my backend and the Ingress routing is defined like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myapp-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    certmanager.k8s.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - myapp.westeurope.cloudapp.azure.com
    secretName: acme-crt-secret
  rules:
  - host: myapp.westeurope.cloudapp.azure.com
    http:
      paths:
      - path: /
        backend:
          serviceName: myapp-mvc
          servicePort: 80
      - path: /api
        backend:
          serviceName: myapp-api
          servicePort: 80
      - path: /identity
        backend:
          serviceName: myapp-identity
          servicePort: 80

The problem is that myapp-api is already listening for requests to /api/v1/myresource. With the current configuration, the myapp-api service only serves requests to myapp.westeurope.cloudapp.azure.com/api/api/v1/myresource (please note the .../api/api/...).

Is it possible to serve requests to /api by the myapp-api service but rewriting these requests to / for the service without creating another Ingress? So, myapp-api should serve requests to myapp.westeurope.cloudapp.azure.com/api/v1/myresource.

-- officer
kubernetes
kubernetes-ingress
nginx
nginx-ingress

1 Answer

8/13/2018

You have two options:

a) Change the port of the API and have it serve / on that port.

b) Change your app so it will serve the API on "/v1/myresource" and give it the "api" part of the URL through the Ingress.

Either way, you'll have your resources at "myapp.westeurope.cloudapp.azure.com/api/v1/myresource".

-- Neekoy
Source: StackOverflow