title: Nginx Access-Control-Allow-Origin not working
body: <p>Trying to access my API via Ajax and I'm getting this error:</p>
  
  <blockquote>
    <p>No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost:9090" rel="noreferrer">http://localhost:9090</a>' is therefore not allowed access. The response had HTTP status code 404.</p>
  </blockquote>
  
  <p>My NGINX configuration looks like this and I'm using Varnish too.</p>
  
  <pre><code>server {
      listen 127.0.0.1:8080;
      server_name api.example.cc;
  
      access_log /var/log/nginx/api.access.log combined;
      error_log /var/log/nginx/api.error.log;
  
      root /home/spark/api.example.cc/web;
      #index index.php;
      try_files $uri /index.php;
  
      set $cache_uri $request_uri;
      location / {
              add_header 'Access-Control-Allow-Origin' 'http://localhost:9090';
              add_header 'Access-Control-Allow-Credentials' 'true';
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
              add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
      }
  }
  </code></pre>
  
  <p>curl -X OPTIONS -i <a href="http://api.example.cc" rel="noreferrer">http://api.example.cc</a> results:</p>
  
  <pre><code>HTTP/1.1 204 No Content
  Server: nginx/1.8.0
  Date: Wed, 30 Dec 2015 20:14:27 GMT
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Credentials: true
  Access-Control-Allow-Methods: GET, POST, OPTIONS
  Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization
  Access-Control-Max-Age: 1728000
  Content-Type: text/plain charset=UTF-8
  Content-Length: 0
  X-Varnish: 65550
  Age: 0
  Via: 1.1 varnish-v4
  Connection: keep-alive
  </code></pre>
  
  <p>curl -X GET/POST -i <a href="http://api.example.cc" rel="noreferrer">http://api.example.cc</a> results:</p>
  
  <pre><code>HTTP/1.1 403 Forbidden
  Server: nginx/1.8.0
  Date: Wed, 30 Dec 2015 20:23:17 GMT
  Content-Type: text/html
  Content-Length: 168
  X-Varnish: 32823
  Age: 0
  Via: 1.1 varnish-v4
  Connection: keep-alive
  
  &lt;html&gt;
  &lt;head&gt;&lt;title&gt;403 Forbidden&lt;/title&gt;&lt;/head&gt;
  &lt;body bgcolor="white"&gt;
  &lt;center&gt;&lt;h1&gt;403 Forbidden&lt;/h1&gt;&lt;/center&gt;
  &lt;hr&gt;&lt;center&gt;nginx/1.8.0&lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>