【问题标题】:Problems with socket.io over https, using nginx as proxy通过 https 的 socket.io 问题,使用 nginx 作为代理
【发布时间】:2021-03-02 12:58:06
【问题描述】:

我对@9​​87654322@ 有一个非常奇怪的问题,我希望有人可以帮助我。

由于某种原因,当我使用https 时,无论如何很少有客户端 无法连接到服务器。 我收到以下错误代码:ERR_CRYPTO_OPERATION_FAILED(请参阅下面的详细日志)

同样,大多数时候连接都很好,只有一些(随机)客户端似乎有这个问题。

我创建了一个超级简单的server.jsclient.js 以便于测试。

我正在使用socket.io@2.4.1socket.io-client@2.4.0

不幸的是,3.x.x 版本不是一个选项。

操作系统是 Ubuntu 18.04,无论是在服务器端还是在客户端。

Nginx:

server {
        listen 80;
        server_name example.domain.com;
        return 301 https://example.domain.com$request_uri;
}

server {
        listen 443 ssl http2;
        server_name example.domain.com;

        ssl_certificate /path/to/cert.pem;
        ssl_certificate_key /path/to/cert.key;
        ssl_protocols TLSv1.2 TLSv1.3;

        location /
        {
                proxy_pass http://127.0.0.1:8000;
                include /etc/nginx/proxy_params;
        }

        location /socket.io {
                proxy_http_version 1.1;
                proxy_buffering off;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_connect_timeout 30s;
                proxy_read_timeout 30s;
                proxy_send_timeout 30s;
                proxy_pass http://127.0.0.1:8000/socket.io;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
                internal;
        }
}

client.js:

const client = io.connect("https://example.domain.com", {
    origins: '*:*',
    transportOptions: {
        polling: {
            extraHeaders: {
                'Authorization': token
            }
        }
    },
});

尝试添加secure: truereconnect: truerejectUnauthorized : false,但没有区别。 此外,我使用不使用transportOptions对其进行了测试。

server.js:

const port = 5000;
const app = express();
const server = app.listen(port, () => {
    console.log(`Listening on port: ${port}`);
});

const io = socket(server);

io.on("connection", (socket) => {
    console.log("Client connected", socket.id);
});

当然,当我删除 nginx 中的重定向并使用普通的旧 http 进行连接时,一切都很好。

当我运行DEBUG=* node client.js 时,我得到以下信息:

socket.io-client:url parse https://example.domain.com/ +0ms
socket.io-client new io instance for https://example.domain.com/ +0ms
socket.io-client:manager readyState closed +0ms
socket.io-client:manager opening https://example.domain.com/ +1ms
engine.io-client:socket creating transport "polling" +0ms
engine.io-client:polling polling +0ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://example.domain.com/socket.io/?EIO=3&transport=polling&t=NVowV1t&b64=1 +2ms
engine.io-client:polling-xhr xhr data null +2ms
engine.io-client:socket setting transport polling +61ms
socket.io-client:manager connect attempt will timeout after 20000 +66ms
socket.io-client:manager readyState opening +3ms
engine.io-client:socket socket error {"type":"TransportError","description":{"code":"ERR_CRYPTO_OPERATION_FAILED"}} +12ms
socket.io-client:manager connect_error +9ms
socket.io-client:manager cleanup +1ms
socket.io-client:manager will wait 1459ms before reconnect attempt +3ms
engine.io-client:socket socket close with reason: "transport error" +6ms
engine.io-client:polling transport not open - deferring close +74ms
socket.io-client:manager attempting reconnect +1s
...

搜索ERR_CRYPTO_OPERATION_FAILED 错误,只会将我带到node.js errors page 其中只有以下描述:

添加于:v15.0.0
加密操作因其他未指明的原因而失败。

我正在使用 Let's Encrypt 证书。

我不明白。如果是 SSL 问题,为什么只有少数客户端会出现此错误? 也许我在nginx 中遗漏了什么?

非常感谢任何帮助。

【问题讨论】:

    标签: nginx https socket.io


    【解决方案1】:

    node-apn 似乎有类似的错误。我的解决方案是降级到 nodejs v14。不妨试试看?

    【讨论】:

      【解决方案2】:

      两步

      1. node的版本必须是14.x
      2. 连接时添加此配置 rejectUnauthorized: false

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-15
        • 1970-01-01
        • 2019-12-21
        • 1970-01-01
        • 1970-01-01
        • 2020-10-05
        • 1970-01-01
        • 2013-05-20
        相关资源
        最近更新 更多