我有一個可怕的時間試圖讓我的服務器接受來自另一臺服務器(本地,但在我的主機文件中給出一個域名的請求)的請求,而沒有觸發可怕的訪問控制允許源服務器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-Origin
到Request header field Authorization is not allowed by Access-Control-Allow-Headers.
)添加只是第一個沒有改變我的反應稍微但加入第二行剛復原的錯誤原來的一個而我仍然受阻。
在這一點上,我不知道還有什麼要嘗試。
更新:
啓動Chrome與標誌 - 禁用網絡的安全性可以讓我測試,和我的網站和代碼在Chrome中工作正常。
但是,這揭示了另一個奇怪的問題,即如果我嘗試將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 /api
到location = /api/*
得到它的工作!但是,僅適用於Safari和Chrome,FF現在甚至不嘗試GET請求,網絡面板中沒有任何條目。
更新與臭蟲和齒輪的牙齒和拔毛髮 Safari和Chrome間歇性地失敗,關於原始不允許的原始錯誤,即使他們工作正常,並沒有對服務器配置進行任何更改。我會喝大量今晚...
你的nginx的配置看起來是正確的。請更新您的問題,包括你在瀏覽器中看到準確的錯誤信息添加'add_header訪問控制允許報頭Authorization'行之後devtools控制檯。 – sideshowbarker
就像我上面所說的,它只是回覆到原始的「Origin not allowed」消息。 – Stephen
https://enable-cors.org/server_nginx.html展示了一個完整CORS配置設置爲nginx的應該是什麼樣子。你可能想要儘可能接近地嘗試。有不幸的是沒有足夠的細節在你的問題才能夠知道,但它似乎像什麼可能發生的是在響應回來OPTIONS請求的瀏覽器會做出應有的授權頭在你的請求中存在的一些問題。 (假設有在Authorization頭的請求 - 不能肯定,因爲你的問題是,是給沒有詳細說明有關的請求......) – sideshowbarker