简体   繁体   English

Socket.io Letsencrypt和CORS上的安全连接失败

[英]Socket.io Letsencrypt and a secure connection failing on CORS

I have a simple node.js setup running socket.io >1.0.0; 我有一个简单的node.js设置运行socket.io > 1.0.0;

fs = require('/home/node/bin/node_modules/file-system');

        // Options for socket.io > 1.0.0
        var options = {
                allowUpgrades: true,
                transports: [ 'polling', 'websocket' ],
                pingTimeout: 6000,
                pingInterval: 3000,
                cookie: 'nom-nom',
                httpCompression: true,
                key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
                cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem'),
                origins: '*:*'
        };

        io = require('/home/node/bin/node_modules/socket.io')(8000, options);

When a client connects from a page served via http they are able to connect as expected and the socket connection is allowed. 当客户端通过http服务的页面连接时,它们能够按预期连接并允许套接字连接。 If a client tries to connect from a page served over https I get the following error; 如果客户端尝试从通过https提供的页面进行连接, https出现以下错误;

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://example.com:8000/socket.io/?EIO=3&transport=polling&t=MfVDWxD. (Reason: CORS request did not succeed).

Given my CORS policy is already set for *:* which should allow anything I don't understand why it's failing. 鉴于我的CORS策略已经设置为*:*这应该允许任何我不明白为什么它失败。

I have tried adding {secure: true} to the client connection, and I've also tried forcing a wss:// and https:// url for the connection string - all result in the same CORS error. 我尝试将{secure: true}添加到客户端连接,并且我还尝试强制连接字符串的wss://https:// url - 所有这些都导致相同的CORS错误。

This is a bare socket implementation, not using any framework like express or even http. 这是一个裸套接字实现,不使用任何框架,如express或甚至http。

Can someone point me in the right direction so that I can allow my clients to connect via https or ideally http and https . 有人可以指出我正确的方向,以便我可以允许我的客户通过https或理想的httphttps

you need to install cors, you can do that with npm or any other package manager 你需要安装cors,你可以用npm或任何其他包经理来做

then in your nodejs app: 然后在你的nodejs app中:

// require cors
const cors = require("cors");
// Use Cors As Middleware
app.use(cors());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM