Nginx – Node.js + Nginx Access-Control-Allow-Origin not working

nginxnode.js

I have an api hosted on api.mysite.com and a client on client.mysite.com. Now from client I get

XMLHttpRequest cannot load
http://api.mysite.com/settings/find.
Origin http://client.mysite.com is not allowed by
Access-Control-Allow-Origin.

The api is build on node.js (sailsjs framework) and the client on angular.js. The webserver is a nginx machine. My vhost config:

server {
    listen 0.0.0.0:80;
    server_name api.mysite.com;

    location / {
        add_header 'Access-Control-Allow-Origin' 'http://api.mysite.com';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';

        proxy_pass         http://127.0.0.1:1337/;
        proxy_redirect     off;

        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

So in browser I can access my api very well, however when I access it from client I get above error. As you can see I added relevant headers to the vhost, however I still get the error. Any ideas whats wrong?

Best Answer

Solved my own problem. Replaced

add_header

with

proxy_set_header

:)