Kubernetes Ingress with mixed locations

Viewed 352

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
1 Answers

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.

Related