【问题标题】:CORS - Chrome canceling simple GET requestCORS - Chrome 取消简单的 GET 请求
【发布时间】:2013-08-31 18:19:50
【问题描述】:

我正在尝试设置 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 一起使用?

【问题讨论】:

  • 您使用的是自签名证书吗?你见过这个:code.google.com/p/chromium/issues/detail?id=141839 吗?
  • 嗨@RayNicholus - 不使用自签名证书或类似的东西。
  • @RayNicholus:如果你投了反对票,请告诉我我能做些什么来纠正问题并修正反对票。

标签: nginx cors


【解决方案1】:

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

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

.. 然后在 Nginx 端用 CORS 做任何时髦的事情,根本

从 Nginx 配置中删除所有出现的 add_header Access-Control-Allow 已经成功了。

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 2013-02-13
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2019-12-26
    • 2015-07-25
    相关资源
    最近更新 更多