Search code examples
javascriptnginxsame-origin-policy

nginx No 'Access-Control-Allow-Origin' header is present on the requested resource


I found that there is lot of such questions but I found no one making me solve my problem.

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

    console.log('get time');

}, 'json');

I am getting in chrome console this:

XMLHttpRequest cannot load http://example.com/api_offer/get_time_left. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '[my local domain]' is therefore not allowed access.

In the nginx site config there is set such settings:

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 am using Vagrant if that matters. But I get same error if I try to run this it unix server, not my localhost computer.

Tried looking at error logs of nginx and of and of this site domain but found nothing useful.

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


Solution

  • I was editing wrong part of config.

    There were 2 server {} blocks.

    One of them was for ssl on, and another was without ssl.

    I was editing with ssl part

    server {
        liten 443;
        ssl on;
    
    
        ....
    
    }
    

    But I was calling non ssl part of the site and it was not reacting.

    The only question could be - how could I detect it sooner. I spent few hours on this.

    So for future visitors who can have such problem - check carefully, make sure you are editing the right block, or the right file of sites-enabled