<p>We would like to use annotation for redirecting requests to a different backend service based on url args (query)</p>
<p>Example:</p>
<p><a href="https://example.com/foo?differentQueryString=0" rel="nofollow noreferrer">https://example.com/foo?differentQueryString=0</a> -&gt; service-a</p>
<p><a href="https://example.com/foo/bar?queryString=0" rel="nofollow noreferrer">https://example.com/foo/bar?queryString=0</a> - service-b</p>
<ul>
<li>Notes: path does not matter, this can be either /foo/bar or /foo or /bar/foo</li>
</ul>
<p>We followed up on this</p>
<p><a href="https://stackoverflow.com/questions/66994472/kubernetes-nginx-ingress-controller-different-route-if-query-string-exists">Kubernetes NGINX Ingress controller - different route if query string exists</a></p>
<p>and this</p>
<p><a href="https://stackoverflow.com/questions/53858277/kubernetes-ingress-routes-with-url-parameter">Kubernetes ingress routes with url parameter</a></p>
<p>But we don't want to setup ConfigMap just for this and also we don't want to duplicate requests to the ingress but rewriting</p>
<p>This is what we tried</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
 annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($args ~ queryString=0){
            backend.service.name = service-b
          }
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-a
                port:
                  number: 80
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-b
                port:
                  number: 80

</code></pre>
<p>We were expecting to get the response but we got 502 from the Ingress Nginx</p>

Matan Baruch
<p>We managed to find a nice solution without rewriting and ConfigMap</p>
<p>Works great and also include Nginx Ingress metrics so we can do HPA accordingly</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
 annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($args ~ queryString=0){
            set $proxy_upstream_name &quot;default-service-b-80&quot;;
            set $proxy_host $proxy_upstream_name;
            set $service_name &quot;service-b&quot;; 
          }
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-a
                port:
                  number: 80
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-b
                port:
                  number: 80

</code></pre>
<p>$proxy_upsteam_name convention is NAMESPACE-SERVICE_NAME-PORT</p>
