<p>I'm trying to achieve a header routing ingress rule with nginx. Why ? Because <em>the same path</em> should go to <em>different backend</em> based on <em>headers</em>. Here what i've tried:</p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: api-mutli-back
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $dataflag 0;

      if ( $http_content_type ~ "multipart\/form-data.*" ){
      set $dataflag 1;
      }

      if ( $dataflag = 1 ){
      set $service_name "backend-data";
      }

spec:
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          serviceName: backend-default
          servicePort: 80
        path: /api
</code></pre>

<p>But the logs of nginx output this error:</p>

<pre><code>unknown directive "set $service_name backend-data" in /tmp/nginx-cfg864446123:1237
</code></pre>

<p>which seems unlogic to me... If I check the configuration generated by nginx, each rule generate a location with something like this at the begining:</p>

<pre><code>[...]
       location ~* "^/api" {

            set $namespace      "my-namespace";
            set $ingress_name   "api-multi-back";
            set $service_name   "backend-default";
[...]
</code></pre>

<p>What am I doing wrong ? Isn't it possible to redefine <strong>service_name</strong> variable with annotation <strong>configuration-snippet</strong> ? Is there any other method ?</p>

<p>Edit: My error on nginx side was due to the lack of exact spaces between <em>set $service_name</em> and <em>backend-data</em>. Then nginx generated correctly the configuration but it still not routing to another kubernetes service.</p>

Kelindil
<p>You got bitten by a YAML-ism:</p>

<p>The indentation of your 2nd <code>if</code> block isn't the same as the indentation of the others, and thus YAML thinks you are starting a new key under <code>annotations:</code></p>

<p>You have</p>

<pre class="lang-yaml prettyprint-override"><code>metadata:
  name: api-mutli-back
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $dataflag 0;

      if ( $http_content_type ~ "multipart\/form-data.*" ){
      set $dataflag 1;
      }

     if ( $dataflag = 1 ){
     set $service_name "backend-data"
     }
</code></pre>

<p>but you should have:</p>

<pre><code>metadata:
  name: api-mutli-back
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $dataflag 0;

      if ( $http_content_type ~ "multipart\/form-data.*" ){
      set $dataflag 1;
      }

      if ( $dataflag = 1 ){
      set $service_name "backend-data"
      }
</code></pre>
