<p>I have an Ingress configuration, I want to enable cors headers on some specific hosts!</p>
<p>I set the annotation in the ingress to</p>
<pre><code>nginx.ingress.kubernetes.io/configuration-snippet: |
        more_set_headers &quot;Access-Control-Allow-Origin: $http_origin&quot;;
nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
nginx.ingress.kubernetes.io/cors-allow-credentials: &quot;true&quot;
</code></pre>
<p>This works but, also will set Access-Control-Allow-Origin in some other hosts
For example:</p>
<pre><code>curl 'https://example.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:88.0) Gecko/20100101 Firefox/88.0' -H 'Accept: application/json' -H 'Accept-Language: en,en-US;q=0.7,en;q=0.3' --compressed -H 'Origin: https://hacker.org' -H 'Connection: keep-alive' -v
</code></pre>
<p>The result would be <code>Access-Control-Allow-Origin: hacker.org</code>
which is not what I expect!</p>
<p>I want to configure the ingress in a way that it sets the <code>Access-Control-Allow-Origin</code> only on some specific hosts!</p>
<p>I tried this annotation!</p>
<pre><code>    nginx.ingress.kubernetes.io/configuration-snippet: |
         if ($http_origin ~*  'https://example.com') {
              more_set_headers &quot;Access-Control-Allow-Origin: $http_origin&quot;;
         }
    nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
    nginx.ingress.kubernetes.io/cors-allow-credentials: &quot;true&quot;
</code></pre>
<p>But does not work!</p>
<p>I want the ingress configured to set Cors headers only on some specific hosts!</p>

Mohamed chiheb Ben jemaa
<p>This is due to the fact that you enabled cors <code>nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;</code> and with that the default for <code>nginx.ingress.kubernetes.io/cors-allow-origin</code> came into play which is <code>*</code>.
Simply configure it with the appropriate annotation like so:</p>
<pre class="lang-yaml prettyprint-override"><code>nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
nginx.ingress.kubernetes.io/cors-allow-origin: &quot;https://origin-site.com&quot;
</code></pre>
<p>More information can be found here:
<a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#enable-cors" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#enable-cors</a></p>
