具有多个值的Socket.io服务器响应

时间:2018-03-18 16:48:31

标签: node.js nginx socket.io socket.io-redis

我正在使用带有nodeJs v6后端的react前端和socket.io-redis pub sub方法。在这种情况下,浏览器拒绝连接 我有这个错误: Failed to load https://api.mydomain.com/socket.io-client/?EIO=3&transport=polling&t=M8vzD5C: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, *', but only one is allowed. Origin 'http://localhost:3000' is therefore not allowed access. 我正在前端运行反应,后端nodeJs,与pub子socket-redis连接 这是我的回复标题: Access-Control-Allow-Credentials: true Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since, Authorization Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS Access-Control-Allow-Origin: http://localhost:3000 Access-Control-Allow-Origin: * Connection: keep-alive Content-Length: 104 Content-Type: text/plain; charset=UTF-8 Date: Sun, 18 Mar 2018 16:19:12 GMT Server: nginx/1.10.3 (Ubuntu) 我可能知道我做错了什么 显然可以建立联系 但它立即中断并继续尝试重新连接循环(我知道为什么) 但为什么我的响应标题会有多个值?

1 个答案:

答案 0 :(得分:-1)

这是我在上面的代码中添加的内容:我注释掉了影响套接字请求的add_headers

set $cors '';
if ($request_uri !~ '^/socket.io-client') {
set $cors 'true';
}

if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}