title: NGINX Reverse Proxy and Access-Control-Allow-Origin issue
body: <p>I'm configuring an <code>NGINX Reverse Proxy</code>.</p>
  
  <p>On the browser I go to:<br />
  <strong>client url:</strong> <a href="https://www.hollywood.com" rel="noreferrer">https://www.hollywood.com</a></p>
  
  <p>Then the web page above needs to do requests to:<br />
  <strong>server url:</strong> <a href="https://server.hollywood.com/api/auth/login" rel="noreferrer">https://server.hollywood.com/api/auth/login</a></p>
  
  <p>This is the configuration corresponding to: <code>server.hollywood.com</code>:</p>
  
  <pre><code>server {
      listen 443 ssl;
      server_name             server.hollywood.com;
      # add_header 'Access-Control-Allow-Origin' "https://www.hollywood.com" always;
      add_header 'Access-Control-Allow-Credentials' 'true' always;
      add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
      add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
      ssl_certificate         ../ssl/lets-encrypt/hollywood.com.crt;
      ssl_certificate_key     ../ssl/lets-encrypt/hollywood.com.key;
      location /
      {
          proxy_pass http://localhost:9201;
          include "../proxy_params.conf";
      }
  }
  </code></pre>
  
  <p><strong>Experiment 1:</strong></p>
  
  <p>With the <code>Access-Control-Allow-Origin</code> line commented out, when I access to:<br />
  <strong>client url:</strong> <a href="https://www.hollywood.com" rel="noreferrer">https://www.hollywood.com</a></p>
  
  <p>I get the following error on the browser console (Chrome in my case):</p>
  
  <pre><code>POST https://server.hollywood.com/api/auth/login/local 502 (Bad Gateway)
  (index):1 Failed to load https://server.hollywood.com/api/auth/login/local: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.hollywood.com' is therefore not allowed access. The response had HTTP status code 502.
  </code></pre>
  
  <p><strong>Experiment 2:</strong></p>
  
  <p>If I enable the <code>Access-Control-Allow-Origin</code> line above, then I get on the browser terminal:</p>
  
  <pre><code>Failed to load https://server.hollywood.com/api/auth/login/local: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, https://www.hollywood.com', but only one is allowed. Origin 'https://www.hollywood.com' is therefore not allowed access.
  </code></pre>
  
  <p>I don't know why multiple when before that header was not present???</p>
  
  <p><strong>Experiment 3:</strong></p>
  
  <p>In the other hand, if I go directly on the browser to the:<br />
  <strong>server url:</strong> <a href="https://server.hollywood.com/api/auth/login" rel="noreferrer">https://server.hollywood.com/api/auth/login</a>
  with the <code>Access-Control-Allow-Origin</code> line commented out, I get the following (on the Network section):</p>
  
  <pre><code>Response Headers:
  
  Access-Control-Allow-Credentials: true
  Access-Control-Allow-Headers: Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With
  Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
  Access-Control-Allow-Origin: *
  Connection: keep-alive
  Content-Length: 139
  Content-Security-Policy: default-src 'self'
  Content-Type: text/html; charset=utf-8
  Date: Sat, 09 Jun 2018 06:34:00 GMT
  Server: nginx/1.13.12
  X-Content-Type-Options: nosniff
  </code></pre>
  
  <p>Before I got: "No 'Access-Control-Allow-Origin' header is present on the requested resource." but now I see above that field is in there on the Response Headers.</p>
  
  <p><strong>Experiment 4:</strong></p>
  
  <p>If I enable again the <code>Access-Control-Allow-Origin</code> line above, then I get the following (on the Network section):</p>
  
  <pre><code>Response Headers:
  
  Access-Control-Allow-Credentials: true
  Access-Control-Allow-Headers: Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With
  Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
  Access-Control-Allow-Origin: https://www.hollywood.com
  Access-Control-Allow-Origin: *
  Connection: keep-alive
  Content-Length: 139
  Content-Security-Policy: default-src 'self'
  Content-Type: text/html; charset=utf-8
  Date: Sat, 09 Jun 2018 06:34:58 GMT
  Server: nginx/1.13.12
  X-Content-Type-Options: nosniff
  </code></pre>
  
  <p>Now I get two times the field: <code>Access-Control-Allow-Origin</code>.</p>
  
  <p>Do you have any idea why my first 2 experiments are failing getting the errors relative to: <code>Access-Control-Allow-Origin</code>?</p>
  
  <p>Thanks!</p>