Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite only specific route with nginx Ingress

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.

like image 997
officer Avatar asked Aug 13 '18 10:08

officer


People also ask

What is the use of rewrite in Nginx?

NGINX rewrite rules are used to change entire or a part of the URL requested by a client. The main motive for changing an URL is to inform the clients that the resources they are looking for have changed its location apart from controlling the flow of executing pages in NGINX. The return and rewrite directives in NGINX are used to rewrite URL.

How to create an internal route from the Nginx Plus ingress controller?

To create an internal route from the NGINX Plus Ingress Controller to the legacy target Service, we need to create an Ingress resource with the annotation nsm.nginx.com/internal-route: "true". For this tutorial, the legacy Service is deployed in Kubernetes so the host name of the Ingress resource is the Kubernetes DNS name.

What is ingress in Nginx?

The Ingress resource only allows you to use basic NGINX features – host and path-based routing and TLS termination. Thus, advanced features like rewriting the request URI or inserting additional response headers are not available. In addition to using advanced features, often it is necessary to customize or fine tune NGINX behavior.

How to enable default egress route in Nginx?

kubectl logs -f <EGRESS_DRIVER_POD> -c nginx-mesh-sidecar | grep "Enabling default egress route" The egress-driver should have been continually sending traffic, which will now be routed through NGINX Plus Ingress Controller.


1 Answers

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".

like image 110
Neekoy Avatar answered Oct 23 '22 14:10

Neekoy