使用 nginx / chrome 的跨源资源共享 (CORS)

Joh*_*ter 14 nginx

我有一个具有以下细分的网站:

api.example.com 
developers.example.com 
example.com
Run Code Online (Sandbox Code Playgroud)

我想同时允许example.comdevelopers.example.com做出AJAX请求api.example.com

到目前为止api.example.com,我的 nginx 配置(由 unicorn 提供的 Rack 应用程序)如下所示:

upstream app_server {
  server unix:/tmp/api.example.com.sock fail_timeout=0;
}

server {
       listen 80;
       server_name api.example.com;
       access_log /home/nginx/api.example.com/log/access.log;
       error_log /home/nginx/api.example.com/log/error.log;
       location / {
         add_header 'Access-Control-Allow-Origin' 'http://example.com,http://developers.example.com';
         add_header 'Access-Control-Allow-Credentials' 'true';
         add_header 'Access-Control-Allow-Headers' 'Content-Type,Accept';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';

         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_redirect off;
         proxy_pass http://app_server;
       }

}
Run Code Online (Sandbox Code Playgroud)

根据我的阅读,这应该足以满足我正在尝试做的事情。

选项的回应:

HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Sat, 28 Apr 2012 17:20:08 GMT
Content-Type: application/json
Connection: close
Status: 200 OK
Content-Length: 0
Access-Control-Allow-Origin: http://developers.example.com,http://example.com
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type,Accept
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Run Code Online (Sandbox Code Playgroud)

但是当我在 Chrome 控制台中尝试以下操作时:

$.ajax("http://api.example.com", {
  type: 'get',
  contentType: "application/json",
  accept: "application/json"
}).success(function(data){
  console.log("success!", data);
}).fail(function(jqxhr, statusText){
  console.log("fail!", jqxhr, statusText);
})
Run Code Online (Sandbox Code Playgroud)

我懂了:

XMLHttpRequest cannot load http://api.example.com/. Origin
http://developers.example.com is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)

对于http://example.com 也是如此

我错过了什么?

如果我设置Access-Control-Allow-Origin*然后我看到:

HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Sat, 28 Apr 2012 17:28:41 GMT
Content-Type: application/json
Connection: close
Status: 200 OK
Content-Length: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type,Accept
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Run Code Online (Sandbox Code Playgroud)

但是 jQuery 请求仍然失败,chrome 还突出显示预飞行OPTIONS失败(即使它返回了200 OK)。

mgo*_*ven 18

根据CORS 规范,多个来源应该用空格分隔,而不是像您使用的那样用逗号分隔,因此请尝试发送此标头:

Access-Control-Allow-Origin: http://developers.example.com http://example.com
Run Code Online (Sandbox Code Playgroud)

Mozilla的文件没有提及多起源的,所以如果仍然无法正常工作只尝试发送:

Access-Control-Allow-Origin: http://developers.example.com
Run Code Online (Sandbox Code Playgroud)

如果可行,您需要配置 nginx 或您的应用程序服务器以返回一个Access-Control-Allow-Origin包含Origin客户端发送的标头值的标头(如果它与允许列表匹配)。类似以下(未经测试)的 nginx 配置可以做到这一点:

if ($http_origin ~ "^(http://developers.example.com|http://example.com)$") {
    add_header "Access-Control-Allow-Origin" $http_origin;
}
Run Code Online (Sandbox Code Playgroud)