kubernetesnginxnginx-ingress

rewrite URI with ingress for different k8s services


Basically I need to achieve the workflow as below. I've already deployed the official nginx helm-chart without any custom-values.
The flow I'm trying to achieve:
https://test-api.foo.com/ >>> http://k8s-service-A/
https://test-api.foo.com/bar >>> http://k8s-service-B/bar
https://test-api.foo.com/sos >>> http://k8s-service-C/sos

Here is my service-A-ingress.yaml configuration:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/proxy-ssl-verify: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.org/client-max-body-size: 1024m
    nginx.org/proxy-connect-timeout: 350s
    nginx.org/proxy-read-timeout: 4260s
  name: service-A-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: test-api.foo.com
    http:
      paths:
      - backend:
          service:
            name: service-A
            port:
              number: 3001
        path: /
        pathType: ImplementationSpecific
      - backend:
          service:
            name: Service-B
            port:
              number: 3002
        path: /bar
        pathType: ImplementationSpecific
      - backend:
          service:
            name: Service-C
            port:
              number: 3003
        path: /sos
        pathType: ImplementationSpecific

Assume that all 3 services and their respective deployments are already there working fine. For all 3 services I'm getting response as below:

https://test-api.foo.com/ >>> http://k8s-service-A/ (working fine)
https://test-api.foo.com/bar >>> http://k8s-service-B/bar (Got 404)
https://test-api.foo.com/sos >>> http://k8s-service-C/sos (Got 404)

I'm not an nginx expert but what it looks like is, `rewrite-target' annotation in the ingress doesn't work.

Also let me know if I'm doing something wrong or understanding it differently. Any help would be appreciated.


Solution

  • Let me clarify and here are the details to help those who are using the official Nginx Ingress Controller. And how I overcame this challenge:

    First, the ingress controller that I'm using is completely different then the one supported by the Kubernetes. So ingress annotation starting with nginx.ingress.kubernetes.io/... won't work in this case.

    After adding this annotation to Ingress, it starts working as described above.

      annotations:
        nginx.org/rewrites: "serviceName=service-A rewrite=/;serviceName=Service-B rewrite=/bar/;serviceName=Service-C rewrite=/sos/"
    

    Checkout more with examples...