【问题标题】:Socket connection not getting established when using https使用 https 时未建立套接字连接
【发布时间】:2021-12-21 15:09:39
【问题描述】:

我正在尝试与反应前端建立套接字连接。当我使用 http API 进行连接时,它工作得非常好,但是当我尝试与 https 服务器连接时,我得到了连接失败的错误。

这是我要连接的反应代码:

import io from "socket.io-client";


const CONNECTION_PORT = 'https://socket.abcd.com/api'         // (I did reverse proxy in nginx for /api endpoint) which is not connecting. I also have SSL certificate purchased for this API. Whereas,

const CONNECTION_PORT = 'http://localhost:5000'.    // this works perfectly fine.

useEffect(() => {
    socket = io(CONNECTION_PORT, {
      transports: ["websocket"],
    });
  });

有人遇到同样的问题吗?请让我知道有什么办法可以解决这个问题。

【问题讨论】:

  • 这完全取决于您定义代理 (nginx) 的方式

标签: node.js reactjs socket.io


【解决方案1】:

这完全取决于您定义代理 (nginx) 的方式

您是否遵循了这些建议?

https://socket.io/docs/v3/reverse-proxy/#NginX

/etc/nginx/nginx.conf的内容:

http {
  server {
    listen 80;
    server_name example.com;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;

      proxy_pass http://localhost:3000;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }
}

【讨论】:

  • 我应该用这个替换它还是在文件中添加这个内容,因为我已经有一些默认配置??
  • 你至少需要让它适应你的域/端口
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
相关资源
最近更新 更多