2017-08-05 75 views
1

我有一個可怕的時間試圖讓我的服務器接受來自另一臺服務器(本地,但在我的主機文件中給出一個域名的請求)的請求,而沒有觸發可怕的訪問控制允許源服務器mods不起作用

XMLHttpRequest cannot load https://dev.mydomain.org/api/user?uid=1. Origin http://home.domain.org is not allowed by Access-Control-Allow-Origin.

我開發的服務器(互聯網)正在運行nginx的,我的家庭服務器(本地)正在運行的Apache。

我已經嘗試了在互聯網上找到的幾種解決方案,無濟於事。我已經嘗試修改nginx配置中的頭文件以允許我的home.mydomain.org服務器,我還在本地添加了htaccess規則以允許所有源(*)。

我的nginx服務器塊具有目前這些線路:

add_header Access-Control-Allow-Origin http://home.mydomain.org; 
    add_header Access-Control-Allow-Headers Authorization; 

(從簡單Origin not allowed by Access-Control-Allow-OriginRequest header field Authorization is not allowed by Access-Control-Allow-Headers.)添加只是第一個沒有改變我的反應稍微但加入第二行剛復原的錯誤原來的一個而我仍然受阻。

在這一點上,我不知道還有什麼要嘗試。

更新:

  1. 啓動Chrome與標誌 - 禁用網絡的安全性可以讓我測試,和我的網站和代碼在Chrome中工作正常。

  2. 但是,這揭示了另一個奇怪的問題,即如果我嘗試將add_header行添加到位置指令,我的無網絡安全Chrome和未修改的Safari都無法從我的api加載信息。所以現在我不確定服務器塊中的add_header指令是否正常工作。

如果有幫助的任何,這裏是我的客戶端代碼(包括東西我已經試過/註釋掉):

var xhr = new XMLHttpRequest(); 
var self = this; 
xhr.open('GET', apiURL + self.currentIssue); 
xhr.setRequestHeader('Access-Control-Allow-Origin','http://home.mydomain.org'); 
//xhr.setRequestHeader('Access-Control-Allow-Credentials', 'true'); 
xhr.withCredentials = true; 
//xhr.setRequestHeader('Access-Control-Request-Method','*'); 
xhr.setRequestHeader('Authorization','Bearer longstringoflettersandnumbers'); 
xhr.onload = function() { 
     self.posts = JSON.parse(xhr.responseText); 
     }; 
xhr.send(); 

另一個更新後嘗試開始SUGGESTION下圖: 一堆的反覆試驗後在客戶端和服務器端,我仍然陷入困境。這是我從使用curl服務器最新迴應(雖然我已經打開和關閉各種客戶端選項和喜歡的事情證書服務器,改變原產地正是礦山或*不得要領):

HTTP/1.1 204 No Content Server: nginx Date: Sun, 06 Aug 2017 10:11:57 GMT Connection: keep-alive Access-Control-Allow-Origin: http://home.mydomain.org Access-Control-Allow-Credentials: true Access-Control-Allow-Methods: GET, POST, OPTIONS Access-Control-Allow-Headers: Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range Access-Control-Max-Age: 1728000 Content-Type: text/plain; charset=utf-8 Content-Length: 0

和這裏是我的控制檯錯誤(Safari瀏覽器):

[Error] Origin http://home.mydomain.org is not allowed by Access-Control-Allow-Origin. 
[Error] Failed to load resource: Origin http://home.mydomain.org is not allowed by Access-Control-Allow-Origin. (actions, line 0) 
[Error] XMLHttpRequest cannot load https://dev.mydomain.org/api/user?uid=1 due to access control checks. 

,這裏是我的控制檯錯誤的Firefox:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://dev.mydomain.org/api/user?uid=1. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

而且在Firefox,這裏有從網絡面板的結果選項和GET:

Request URL: https://dev.mydomain.org/api/user?uid=1 
Request method: OPTIONS 
Status code: 204 No Content 
Version: HTTP/2.0 

Response headers (511 B)  
Server "nginx" 
Date "Sun, 06 Aug 2017 10:44:22 GMT" 
Access-Control-Allow-Origin "http://home.mydomain.org" 
access-control-allow-credentials "true" 
Access-Control-Allow-Methods "GET, POST, OPTIONS" 
Access-Control-Allow-Headers "Authorization,DNT,X-CustomHea…ent-Type,Content-Range,Range" 
Access-Control-Max-Age "1728000" 
Content-Type "text/plain; charset=utf-8" 
Content-Length "0" 
X-Firefox-Spdy "h2" 
Request headers (501 B) 
Host "dev.mydomain.org" 
User-Agent "Mozilla/5.0 (Macintosh; Intel… Gecko/20100101 Firefox/54.0" 
Accept "text/html,application/xhtml+x…lication/xml;q=0.9,*/*;q=0.8" 
Accept-Language "en-US,en;q=0.5" 
Accept-Encoding "gzip, deflate, br" 
Access-Control-Request-Method "GET" 
Access-Control-Request-Headers "authorization" 
Origin "http://home.mydomain.org" 
Connection "keep-alive" 
Cache-Control "max-age=0" 


Request URL: https://dev.mydomain.org/api/user?uid=1 
Request method: GET 
Status code: 404 Not Found 
Version: HTTP/2.0 

Response headers (170 B)  
Server "nginx" 
Date "Sun, 06 Aug 2017 10:44:22 GMT" 
Content-Type "text/html" 
Vary "Accept-Encoding" 
Content-Encoding "gzip" 
X-Firefox-Spdy "h2" 
Request headers (723 B) 
Host "dev.mydomain.org" 
User-Agent "Mozilla/5.0 (Macintosh; Intel… Gecko/20100101 Firefox/54.0" 
Accept "*/*" 
Accept-Language "en-US,en;q=0.5" 
Accept-Encoding "gzip, deflate, br" 
Referer "http://home.mydomain.org/" 
Authorization "Bearer eyJ0eXAG…BRHmX9VmtYHQOvH7k-Y32wwyeCdk" 
Origin "http://home.mydomain.org" 
Connection "keep-alive" 
Cache-Control "max-age=0" 

與局部SUCESS UPDATE:

我想我找到了問題(部分):改變了我的位置指令中nginx的從location /apilocation = /api/*得到它的工作!但是,僅適用於Safari和Chrome,FF現在甚至不嘗試GET請求,網絡面板中沒有任何條目。

更新與臭蟲和齒輪的牙齒和拔毛髮 Safari和Chrome間歇性地失敗,關於原始不允許的原始錯誤,即使他們工作正常,並沒有對服務器配置進行任何更改。我會喝大量今晚...

+0

你的nginx的配置看起來是正確的。請更新您的問題,包括你在瀏覽器中看到準確的錯誤信息添加'add_header訪問控制允許報頭Authorization'行之後devtools控制檯。 – sideshowbarker

+0

就像我上面所說的,它只是回覆到原始的「Origin not allowed」消息。 – Stephen

+0

https://enable-cors.org/server_nginx.html展示了一個完整CORS配置設置爲nginx的應該是什麼樣子。你可能想要儘可能接近地嘗試。有不幸的是沒有足夠的細節在你的問題才能夠知道,但它似乎像什麼可能發生的是在響應回來OPTIONS請求的瀏覽器會做出應有的授權頭在你的請求中存在的一些問題。 (假設有在Authorization頭的請求 - 不能肯定,因爲你的問題是,是給沒有詳細說明有關的請求......) – sideshowbarker

回答

0

哇,是有史以來令人費解。在這裏發佈答案,以防其他WP用戶在這裏找到自己的方式。我一直得到不一致的結果(有時工作,有時不神祕),我的問題,並最終跟蹤到頭在服務器上的PHP代碼被設置,獨立的nginx的設置,有時相互矛盾它們(雖然從來沒有在可預測的方式,我可以看到)。所以,我需要解決的事情是:

  • 打消了我所有CORS聲明在我的nginx CONFIGS
  • 我也有我的服務器上的代碼驗證的身份驗證頭一個令牌,它是飛行前沒有按規定OPTIONS (它不應該檢查),所以我不得不添加一個if語句之前,讓它忽略一個OPTIONS調用(!$_SERVER['REQUEST_METHOD'] === "OPTIONS"
  • 因爲我從另一個使用UpdraftPlus插件克隆這個網站,我不得不去刪除我的遷移鍵,因爲它們的存在阻止了API調用的工作。一旦他們被刪除,我的通話再次開始工作。
  • 刪除並重新添加內置的WP過濾rest_pre_serve_request

我的過濾器代碼是在這裏:

add_action('rest_api_init', function() { 
/* unhook default function */ 
remove_filter('rest_pre_serve_request', 'rest_send_cors_headers'); 
/* then add your own filter */ 
add_filter('rest_pre_serve_request', function($value) { 
$origin = get_http_origin(); 
$my_sites = array($origin); // add array of accepted sites if you prefer 
if (in_array($origin, $my_sites)) { 
header('Access-Control-Allow-Origin: ' . esc_url_raw($origin)); 
} else { 
header('Access-Control-Allow-Origin: ' . esc_url_raw(site_url())); 
} 
header('Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, PATCH, DELETE'); 
header('Access-Control-Allow-Credentials: true'); 
header('Access-Control-Allow-Headers: Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Origin,Content-Type,X-Auth-Token,Content-Range,Range'); 
header('Access-Control-Expose-Headers: Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Origin,Content-Type,X-Auth-Token,Content-Range,Range'); 
header('Vary: Origin'); 
return $value; 
}); 
}, 15); 

如今終於,一切正常無處不在(在每個瀏覽器和捲曲太)!

相關問題