node.js - Socket.io + Nginx + AWS Application Load Balancer 连接在建立之前关闭

标签 node.js amazon-web-services sockets nginx

我想达到什么目的?

在我的 ec2 实例上设置 socket.io,使用 Nginx 作为 Web 服务器,使用 AWS Application Load Balancer 和解析 DNS 以指向我的 Application Load Balancer 的 Route53。

以下是我在网上找到并尝试过的解决方案列表:

  1. 在应用程序级别使用网络负载平衡器代替 TCP 80、TCP 443 和 SSL。客户端套接字连接 url 为 https://mydomain:9000 并且我不断收到错误消息 websocket connection closed before connection could be established.。目标群体已启用粘性。

  2. 使用 Application Load Balancer,并为路径 /socket.io 添加一个基于路径的规则到我的监听器,并将它转发到启用粘性的不同目标组:现在我尝试了两个 443 协议(protocol)和 9000 端口与 HTTPS 协议(protocol)。我得到了相同的结果。

  3. 使用我的负载均衡器的公共(public) DNS 作为套接字客户端连接的 url。结果还是一样。

这是我的 nginx 配置文件:

    # SSL configuration

     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;

    ssl_certificate  /path/to/ssl.crt;
    ssl_certificate_key /path/to/private_key.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    if ($ssl_protocol = "") {
  rewrite ^   https://$server_name$request_uri? permanent;
}

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:9000;

    }

现在,我应该让你知道我已经强制套接字客户端使用 websocket 传输,xhr-polling 给我以下错误,我又没有想法如何处理它。

Failed to load https://my_domain/socket.io/?EIO=3&transport=polling&t=M3lhVWi: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4444' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

如果有人可以帮助我解决建立套接字连接的任何一种情况,那么我将非常感激。另外,请询问您是否需要任何其他相关信息。

编辑 这是我设置 socket.io 的方式

const app = express();

const server = http.createServer(app);

const socketio = io(server, {
    serveClient: false,
    pingInterval: 10000,
    pingTimeout: 5000,
    cookie: false,
    transport: ['websocket', 'xhr-polling']
});

socketConfig(socketio);

//and here's socketConfig

export function socketConfig(socketio) {
    let localSockets = [];
    socketio.on('connection', connectConfig(socketio, localSockets))
}

更新:我犯了一个愚蠢的错误,虽然这没有解决我的问题,但客户端连接 url 将是 https://mydomain,因为 socket. io 和我的 nodejs 服务器正在监听同一个端口。我现在正在使用 Application Load Balancer,但收到 502 错误。

更新:我检查了我的 Nginx 错误日志并发现了这个:

upstream prematurely closed connection while reading response header from upstream, client: 172.31.11.251, server: _, request: "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1", upstream: "http://127.0.0.1:9000/socket.io/?EIO=3&transport=websocket", host: "mydomain"

最佳答案

好的,我修复了它,这是我犯的一个非常愚蠢的错误:

我的代码中有这个:

app.listen(config.port, config.ip, () => {
    console.log(`express server listening on port ${config.port} in ${config.env} mode`);
});

代替:

server.listen(config.port, config.ip, () => {
    console.log(`express server listening on port ${config.port} in ${config.env} mode`);
});

关于node.js - Socket.io + Nginx + AWS Application Load Balancer 连接在建立之前关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48277687/

相关文章:

node.js - 检查来自顶级模型的关联模型,其中查询

amazon-web-services - 在标签中找到注释值 21。这大于类(class)数量

amazon-web-services - 使用 Elastic Beanstalk .ebextensions 指定 RDS 数据库

c# - Windows Phone 8 - 保持套接字打开并接收未知长度的数据

node.js - 如何使用命令行安装kendo grid

node.js - *捕获*错误后,node.js(socket.io)停止与客户端通信

iphone - GCD Dispatch Sources 和 select() 有什么区别?

ruby - ENOENT 在 Ruby 中创建 UNIX 套接字时

node.js - 如何从 Node.js 内部获取 Firebase 中的默认存储桶名称?

amazon-web-services - 如何在 YAML CloudFormation 模板中的特定 VPC 中启动 Amazon EC2