1

我正在尝试设置 CORS 以使用 Node.js 中的 Socket IO。不幸的是,请求不断被 Chrome 取消:

GET https://example.com/zebra/8601/socket.io/1/?key=example123&t=1377831596484 HTTP/1.1
Origin: https://example.io
Referer: https://example.io/example
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36

如果我在新选项卡中打开 URL,我会得到更好的响应:

HTTP/1.1 200 OK
Server: nginx/1.4.1
Date: Fri, 30 Aug 2013 03:00:12 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS
Access-Control-Allow-Headers: Authorization,Content-Type,Accept,Origin,Referer,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since

虽然这是一个实际的跨域请求,但它只是失败了。

Nginx 配置如下:

location ~ ^/zebra/(\d+)(?:/(.*))?$ {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Credentials true;
    add_header Access-Control-Allow-Methods "GET, PUT, POST, OPTIONS";
    add_header Access-Control-Allow-Headers "Authorization,Content-Type,Accept,Origin,Referer,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since";
}

如何配置东西以使其与 CORS 一起使用?

4

1 回答 1

1

所以解决方案是确保我们在 Node JS 端正确配置了这个 Socket IO 设置:

io.configure(function() {
  io.set('origins', '*:*');
});

.. 然后在 Nginx 端完全不用 CORS 做任何时髦事情

add_header Access-Control-Allow从 Nginx 配置中删除所有匹配项就可以了。

于 2013-08-31T10:02:23.060 回答