Nginx No "Access-Control-Allow-Origin" header present on the requested resource

I found that there are many such questions, but I did not find that no one is forcing me to solve my problem.

jQuery.post( 'http://example.com/api_offer/get_time_left', function(data) {

    console.log('get time');

}, 'json');

      

I am getting chrome console:

XMLHttpRequest cannot load http://example.com/api_offer/get_time_left . The requested resource does not have an "Access-Control-Allow-Origin" header. Therefore Origin '[my local domain]' is not allowed.

The following settings are set in the nginx site configuration:

location ~ \.php$ {
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index index.php;
              include fastcgi_params;
              fastcgi_read_timeout 300;

              add_header Access-Control-Allow-Origin *;

              proxy_set_header Access-Control-Allow-Origin $http_origin;

        }

      

I use vagrant if it matters. But I get the same error if I try to run it on a unix server and not on my local machine.

Tried searching the error logs of nginx and this domain and didn't find anything useful.

How it can't work if I set * to allow any domain? What else can I do about this?

+3


source to share


1 answer


I edited the wrong part of the config.

There were 2 blocks {} servers.

One was for ssl on and the other was without ssl.

I edited with ssl part



server {
    liten 443;
    ssl on;


    ....

}

      

But I was calling the non ssl part of the site and it didn't respond.

The only question is how I could have found it earlier. I spent several hours on this.

So, for future visitors who may have this problem - check carefully, make sure you are editing the right block or right file with site support

0


source







All Articles