title: How to enable CORS in Nginx proxy server?
body: <p>As my title, here is the config file located in conf.d/api-server.conf</p>
  
  <pre><code>server {
    listen 80;
    server_name api.localhost;
  
    location / {
      add_header 'Access-Control-Allow-Origin' 'http://api.localhost';
      add_header 'Access-Control-Allow_Credentials' 'true';
      add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
      add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
  
      if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
      }
  
      proxy_redirect off;
      proxy_set_header host $host;
      proxy_set_header X-real-ip $remote_addr;
      proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
      proxy_pass http://127.0.0.1:3000;
    }
  }
  </code></pre>
  
  <p>The nginx.conf file stay the same as default.</p>
  
  <p>After I send request to api.localhost (api.localhost/admin/login), I still receive 405 error:</p>
  
  <pre><code>XMLHttpRequest cannot load http://api.localhost/admin/login. Response 
  to preflight request doesn't pass access control check: No 'Access-
  Control-Allow-Origin' header is present on the requested resource. 
  Origin 'http://admin.localhost:3000' is therefore not allowed access. 
  The response had HTTP status code 405.
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/oMcPZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oMcPZ.png" alt="That is the response from server"></a>
  <a href="https://i.stack.imgur.com/Vg96v.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Vg96v.png" alt="That&#39;s the request looks like"></a></p>