2017-10-27 6 views
0

私が間違っていることはわかりませんが、静的なhtmlサイトを取得することができません。CORS、nginx via vagrant

私の静的なサイトは127.0.0.1:4000で提供されていて、example.loc/api/api.php?querystuffからJSONデータを取得しようとしています。

var url ='http://example.loc/api/api.php?querystuff' 
var xhr = new XMLHttpRequest(); 
if (!('withCredentials' in xhr)) xhr = new XDomainRequest(); // fix IE8/9 
xhr.open('GET', url); 
xhr.onload = success; 
xhr.send(); 

そして、これは、応答クロムが私に与えているです:

そのサイトの

私のnginxの設定は

server { 
    listen  80; 
    listen  443 ssl; 

    server_name example.loc ~^example\.\d+\.\d+\.\d+\.\d+\.xip\.io$; 

    # Tells nginx which directory the files for this domain are located 
    root   /srv/www/example/htdocs/current; 

    rewrite ^/(.*)/$ /$1 permanent; 

    location /api/api.php { 
     if ($request_method = 'OPTIONS') { 
      add_header 'Access-Control-Allow-Origin' $http_origin; 
      add_header 'Access-Control-Allow-Credentials' 'true'; 
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 

      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
      add_header 'Access-Control-Max-Age' 1728000; 
      add_header 'Content-Type' 'text/plain charset=UTF-8'; 
      add_header 'Content-Length' 0; 
      return 204; 
     } 
     if ($request_method = 'POST') { 
      add_header 'Access-Control-Allow-Origin' $http_origin; 
      add_header 'Access-Control-Allow-Credentials' 'true'; 
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
     } 
     if ($request_method = 'GET') { 
      add_header 'Access-Control-Allow-Origin' $http_origin; 
      add_header 'Access-Control-Allow-Credentials' 'true'; 
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
     } 

    ... 

    } 
    ... 
} 
私の静的なサイトがでエンドポイントに当たっている

を持っています読み込みに失敗しました http://example.loc/api/api.php?api=line_status&line=red:いいえ 'Access-Control-Allow-Origin' h eaderは要求された リソースに存在します。したがって、オリジン 'http://127.0.0.1:4000'は許可されません。

私は何が欠けていますか?

+0

だから、問題はあなたのnginxの設定は '場所を/持っていないだけということではありませんapi/api.php'(これはあなたが必要とする実際のパスです)?その代わりに 'location/lib/api.php'があります。 – sideshowbarker

+0

ええ、それらはタイプミスで、パスは/lib/api.phpでしたが、すべてを/api/api.phpに変更しました。 ロケーションブロックを 'location /api/api.php {}'に変更しても、それはまだ動作しません。私はちょうど私のnginx設定でそれを変更し、それをテストするために 'nginx -t'を実行して、サービスを再起動し、私は同じエラーを取得します。 – rugbert

+0

レスポンスのHTTPステータスコードとは何ですか? nginxは、 'always'キーワードを' add header'ディレクティブに追加しない限り、非 '2xx'レスポンスにヘッダーを追加しません。 – sideshowbarker

答えて

1

良い夜の眠りの後、私は問題が何かを理解しました、それはブロックオーダーでした。私のnginx設定は、次のもので始まった別のロケーションブロックを持っていました:

location ~ \.php {} 

これは上記のロケーションブロックよりも先行しています。私は自分のロケーションブロックに等号を追加しただけです:

location = /api/api./php {} 

これで機能します。この問題を取得して他の誰のために

、次のように私を助け: