gpt4 book ai didi

nginx - socket.io 在 https 上的问题,使用 nginx 作为代理

转载 作者:行者123 更新时间:2023-12-05 04:52:37 25 4
gpt4 key购买 nike

我在使用 socket.io 时遇到了一个非常奇怪的问题,我希望有人能帮助我。

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

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

我创建了一个 super 简单的 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;
}
}

客户端.js:

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

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

服务器.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只有以下描述:

Added in: v15.0.0
A crypto operation failed for an otherwise unspecified reason.

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

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

非常感谢任何帮助。

最佳答案

node-apn 似乎出现了类似的错误。我的解决方案是降级到 nodejs v14。也许试试看?

关于nginx - socket.io 在 https 上的问题,使用 nginx 作为代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66439880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com