0

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.

Andrew
  • 124
  • 1
  • 12
  • In this case you do not need to `rewrite-target` so please remove it and test it again. Also, make sure that the service itself does not return a 404. – Xirehat Jan 05 '23 at 07:07
  • Thanks @xirehat for the reply. After removing `rewrite-target` its working fine now. Also fixed the service-B was returning the 404 error. – Andrew Jan 06 '23 at 04:50

1 Answers1

0

As @xirehat mentioned, the rewrite annotation can be removed. Because the request will be rewritten to match the URI that the associated services anticipate. Because occasionally the exposed URL for the backend service is different from the path indicated in the Ingress rule. Unless a request is rebuilt, it will always return 404.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/proxy-ssl-verify: "false"
    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

In case if still it doesn't work then try with pathType: Prefix. You can refer to these links to learn more about Rewrite annotation and Ingress annotations.