2016-08-04 20 views
0

我有一個hapiJS API的節點服務器,並試圖從單獨的節點服務器發送請求。Nginx + HapiJS CORS請求(不是GET)

在開發中一切正常,但不在服務器上,並且問題是服務器上啓用的基本身份驗證。

具體而言,如果請求是GET請求,但所有其他請求均失敗,則請求將正常工作。我嫌疑人這是由於選擇預先請求的飛行檢查失敗,這是不是從我有限的理解GET請求發送。

確切的錯誤消息我得到的是:

否「訪問控制允許來源」標頭出現在所請求的資源。因此不允許訪問原產地'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-頭插件,無果(而不是上面的設置)。我試圖使上nginx的我能想到的每一個CORS的事情(其中大部分來自http://enable-cors.org/server_nginx.html得到)

無論我如何調整配置 - 我嘗試了很多東西: 1)它會繼續提供上述消息,不管它是什麼 2)它抱怨標題在那裏兩次(如果我同時把它放在nginx和hapijs中)。

它在任何情況下都不起作用(GET請求除外)。

一個POST Ajax調用,我使用的API(與劍道使用)的例子:

$.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] 

這是爲什麼工作GET請求但不是POST/PUT/etc?

回答

0

改變你的nginx來處理CORS,並且沒有hapi擔心它。截斷並從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則需要更多的工作進行修改。