3

I have a kubernetes ingress that has a service taking advantage of nginx.ingress.kubernetes.io/rewrite-target. I have another path, service1/query that needs to redirect somewhere other than what the rewrite-target is pointing to. Let's say it should act as a proxy and forward the request to google.com and return the result. Because the rewrite-target is for all paths defined, I'm not sure how I should proceed to differentiate the path for service1/query.

Any ideas?

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myApp
  namespace: myApp
  annotations:
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, OPTIONS"
    nginx.ingress.kubernetes.io/cors-allow-origin: '$http_origin'
    nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    cert-manager.io/cluster-issuer: letsencrypt-prod

spec:
  ingressClassName: nginx
  tls:
  - hosts:
      - myhost.org
    secretName: ingress-nginx-tls-cert
  rules:
  - host: myhost.org
    http:
      paths:
      - path: /service1(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
      - path: /service1/query
        pathType: Prefix
Thomas
  • 720
  • 9
  • 22
  • Have you already tried any solutions? What version of Ingress-NGINX are you using? – Andrew Skorkin Mar 04 '22 at 17:17
  • I'm actually not sure what solutions are out there for this, given that `rewrite-target` is for *all* paths defined. NGINX 1.19 – Thomas Mar 04 '22 at 17:57
  • You can update the path for your second service as `path: /service1/query(/|$)(.*)` Does this solve your problem? – Andrew Skorkin Mar 11 '22 at 10:26
  • You can create another ingress resource almost identical, but excluding `rewrite-target` and `path: /service1(/|$)(.*)`. The location matching priority of Nginx should automatically route only requests for `/service1/query` to your new Ingress and everything else to the one above. https://stackoverflow.com/questions/5238377/nginx-location-priority – D-S Mar 12 '22 at 08:28

1 Answers1

0

You are almost there, you just need to change the paths' order. Ingress Path Matching

paths:
  - path: /service1/query
    pathType: Prefix
  - path: /service1(/|$)(.*)
    pathType: Prefix
    backend:
      service:
        name: service1
        port:
          number: 80

When you add /service1(/|$)(.*) to the first line, the /service1/query is a dead code; because it enters the first line.

However, if you put /service1/query to the first; nginx will look at it first.

stuck
  • 1,477
  • 2
  • 14
  • 30