2016-08-04 7 views
0

私はhapiJS APIを持つノードサーバーを持っており、別のノードサーバーからリクエストを送信しようとしています。Nginx + HapiJS CORSリクエスト(GETではない)

すべては開発では問題なく動作しますが、サーバでは問題なく動作しますが、基本認証はサーバで有効になっています。

特に、要求がGET要求であれば正常に動作しますが、その他の要求はすべて失敗します。 I 容疑者これはOPTIONSの事前リクエストフライトチェックが失敗したためです。私の限られた理解からGETリクエストでは送信されません。

私が得る正確なエラーメッセージは次のとおりです。

いいえ「アクセス制御 - 許可 - 起源」ヘッダが要求されたリソース上に存在します。 Origin 'https://site.example.com'はアクセスできません。応答は、HTTPステータスコード401を持っていた

私のnginxの設定ファイル:HapiJSため

server { 
    listen 80; 
    server_name https://api.example.com; 
    return 301 https://$server_name$request_uri; ## Permanently redirect all http traffic to https 
} 
server { 

    #SSL INIT 
    listen 443; 
    ssl on; 
    ssl_certificate /var/vcap/jobs/nginx/config/site.bundle.crt; 
    ssl_certificate_key /var/vcap/jobs/nginx/config/site.key; 

    server_name https://api.example.com; 

    location/{ 
    auth_basic "Restricted Content"; 
    auth_basic_user_file .htpasswd; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-NginX-Proxy true; 
    proxy_pass_request_headers on; 
    proxy_pass https://api.example.com; 
    } 

} 

そして、私のCORS設定(私は無関係なものを除きます)。

connections: 
    routes: 
    cors: 
     origin: ["https://api.example.com"] 
     credentials: true 

私は無駄に、https://www.npmjs.com/package/hapi-cors-headers HAPI-CORS-ヘッダのプラグインを試してみました(代わりに、上記の設定。)私はそれらのほとんどはhttp://enable-cors.org/server_nginx.htmlから得私は考えることができnginxの上のすべての単一のCORSの事を(有効に試してみました)

どのように設定を調整したとしても、2つのことが起こります。多くのことを試しました。 1)これは何であっても上記のメッセージを表示し続けます。2)私はそれをnginxとhapijsの両方に同時に置くと、見出しは2つあります。

GET要求を除いて、それは機能しません。

(剣道で使用される)私が使用しているAPIにPOSTのAJAX呼び出しの例:

$.ajax({ 
    url: api_address + '/vendors', 
    method: 'POST', 
    contentType: 'application/json', 
    processData: false, 
    data: JSON.stringify(this.vendor), 
    xhrFields: { 
     withCredentials: true 
    } 
    success: function(data) { 

     vm.set('vendor', data); 
     notification.show('Vendor created successfully', 'success'); 
     vm.set('has_changes', false); 
    }, 
    error: function() { 
     notification.show('Error creating vendor', 'error'); 
    } 
}); 

上記api_addressは次のとおりです。

https://username:[email protected] 

なぜこれがために働いていますPOST/PUT /などではなくGET要求ですか?

答えて

0

CORSを扱うようにnginxを変更してください。心配する必要はありません。撮影と修正もちろんhttp://enable-cors.org/server_nginx.html

location/{ 
    add_header 'Access-Control-Allow-Origin' '*'; 
    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 = 'OPTIONS') { 
     # 
     # Tell client that this pre-flight info is valid for 20 days 
     # 
     add_header 'Access-Control-Max-Age' 1728000; 
     add_header 'Content-Type' 'text/plain charset=UTF-8'; 
     add_header 'Content-Length' 0; 
     return 204; 
    } 
    auth_basic "Restricted Content"; 
    auth_basic_user_file .htpasswd; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-NginX-Proxy true; 
    proxy_pass_request_headers on; 
    proxy_pass https://api.example.com; 
} 

からあなたはPUTやDELETEサポートしている場合は、より多くの作業が必要になります。

関連する問題