title: How supress Nginx CORS headers coming from backend server
body: <p>I'm using Nginx to both serve Angular 2 client and route rest API to a backend server.
  On Angular development mode I use the regular 'ng server' and then I should add CORS support to Nginx which is this way requested only for rest API as an external server.</p>
  
  <p>My Nginx conf is:</p>
  
  <pre><code>server {
      listen 80;
      resolver 127.0.0.1 valid=30s;
      root /path/dist-nginx;
      index index.html index.htm;
  
  
      location /prometheus/prometheus/ {
        if ($request_method = 'OPTIONS') {
          add_header 'Access-Control-Allow-Origin' $http_origin;
          add_header 'Access-Control-Allow-Credentials' 'true';
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
          add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          return 204;
        }
        if ($request_method = 'GET') {
          add_header 'Access-Control-Allow-Origin' $http_origin;
          add_header 'Access-Control-Allow-Credentials' 'true';
          add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
          add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        #proxy_set_header X-Real-IP  $remote_addr;
        #proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://prometheus:9090/;
      }
  
      location / {
          try_files $uri $uri/ /index.html;
          #try_files $uri $uri/ /index.html =404;
      }
  }
  </code></pre>
  
  <ul>
  <li>location /prometheus/prometheus is for api </li>
  <li>location / to serve Angular client</li>
  </ul>
  
  <p>It's work perfectly with the first request:</p>
  
  <pre><code>http://localhost:3330/prometheus/prometheus/metrics
  </code></pre>
  
  <p>showing that at least the CORS settings work well.</p>
  
  <p>but when the client executes the following request:</p>
  
  <pre><code>http://localhost:3330/prometheus/prometheus/api/v1/query?query=http_request_size_bytes
  </code></pre>
  
  <p>I got this issue in the browser:</p>
  
  <pre><code> XMLHttpRequest cannot load
   http://local.appcelerator.io:3330/prometheus/prometheus/api/v1/query?query=http_request_size_bytes.
   The 'Access-Control-Allow-Origin' header contains multiple values '*,
   http://localhost:4200', but only one is allowed.  Origin
   'http://localhost:4200' is therefore not allowed access.
  </code></pre>
  
  <p>Looking at the header answer of the request i got:</p>
  
  <pre><code>Access-Control-Allow-Credentials:true
  Access-Control-Allow-Headers:Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
  Access-Control-Allow-Headers:Accept, Authorization, Content-Type, Origin
  Access-Control-Allow-Methods:GET, OPTIONS
  Access-Control-Allow-Methods:GET, OPTIONS
  Access-Control-Allow-Origin:http://localhost:4200
  Access-Control-Allow-Origin:*
  Access-Control-Expose-Headers:Date
  Connection:keep-alive
  Content-Encoding:gzip
  Content-Length:657
  Content-Type:application/json
  Date:Sun, 11 Jun 2017 05:39:34 GMT
  Server:nginx/1.13.1
  </code></pre>
  
  <p>So 3 CORS headers fields have been added in addition to mine:</p>
  
  <pre><code>Access-Control-Allow-Origin:*
  Access-Control-Allow-Headers:Accept, Authorization, Content-Type, Origin
  Access-Control-Allow-Methods:GET, OPTIONS
  </code></pre>
  
  <p>Angular client requests directly Nginx, there is no one in the middle.</p>
  
  <p>Direct curl of these two requests on nginx work perfectly.</p>
  
  <p>if i execute the fist request :</p>
  
  <pre><code>http://localhost:3330/prometheus/prometheus/metrics
  </code></pre>
  
  <p>a second time after the one witch failed, i got no error, header fields are ok.</p>
  
  <p>Seems that the issue is link to this request especially:</p>
  
  <pre><code>http://localhost:3330/prometheus/prometheus/api/v1/query?query=http_request_size_bytes
  </code></pre>
  
  <p>Seems that the backend server here prometheus adds itself some CORS headers when it answers to api requests on the url /api/v1</p>
  
  <p>how i can get ride of them? </p>
  
  <p>Thank you for any ideas which could help to understand what's happening there.</p>