简体   繁体   中英

REST URI with NGINX Ingress Controller

I'm trying to configure NGINX Ingress controller as the correct entry point to my Kubernetes cluster. Inside the cluster, I've created two REST Web services as well as frontend application. I'm trying to achieve the following scenario.

  • When the ingress IP is hit without any parameters it should be routed to the frontend app. Example: 192.168.1.20 should lead to frontend service on port 80.
  • When parameters are given, the request should be routed to correct REST service. Example: 192.168.1.20/first-rest/api/flower?id=1 should route the request to the first-rest service so that it could return the flower with id = 1.

I can correctly access the frontend application but when trying to access any REST service I'm getting 404 error or no response at all. First-rest, Second-rest and frontend are running correctly and are accessible when configured as LoadBalancer services. With Nginx, they are configured as ClusterIp services.
My ingress configuration ------------------------
 apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: main-routes annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/ssl-redirect: "false" nginx.ingress.kubernetes.io/use-regex: "true" nginx.ingress.kubernetes.io/rewrite-target: /$1 spec: rules: - http: paths: - path: /first-rest(/|$)(.*) backend: serviceName: first-rest servicePort: 8090 - path: /second-rest(/|$)(.*) backend: serviceName: second-rest servicePort: 9000 - path: /(.*) backend: serviceName: frontend servicePort: 80

It seems like NGINX is cutting short my URL parameters that are required for my REST API.
Is there any way to pass the right URL path so that `192.168.1.20/first-rest/api/flower?id=1` would be routed to `[first-rest add and port]/api/flower?id=1`?

You need to specify the ingress path type otherwise, depending on the ingress class specific implementation it will default to either exact or prefix (I assume in your case it is defaulting to exact)

So, you need to do something like

spec:
  rules:
  - http:
      paths:
      - path: /first-rest
        pathType: Prefix
        backend:
          serviceName: first-rest
          servicePort: 8090

see docs on ingress path here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM