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 {
    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_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?


Solved my own problem. Replaced





|improve this answer|||||
  • Use add_header inside if clause. – CppLearner Aug 24 '14 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.