Nginx 代理传递到外部 API - 502 错误网关

问题描述

问题:我在仅具有私有 IP 的 ubuntu 服务器中安装了 Nginx 反向代理。此反向代理的目的是将传入请求路由到各种 3rd 方 Web 套接字和 RestAPI。此外,为了分配负载,我在 Nginx 代理服务器后面安装了一个 http 负载均衡器。

所以这就是它在技术上的样子:
IncomingRequest --> InternalLoadBalancer(Port:80) --> NginxReverseProxyServer(80) --> ThirdParyAPIs(Port:443) & WebSockets(443)

我遇到的问题是,Nginx 没有正确反向代理到 RestAPI 并给出 502 错误,但它确实适用于 Web 套接字。
以下是我的 /etc/Nginx/sites-available/default 配置文件:(其他地方未做任何更改)

map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}
server {
  listen 80;
  location /binance-ws/ {
      # Web Socket Connection
      ####################### THIS WORKS FINE
      proxy_pass https://stream.binance.com:9443/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Host $host;
  }
  location /binance-api/ {
      # Rest API Connection
      ##################### THIS FAILS WITH 502 ERROR
      proxy_pass https://api.binance.com/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Host $host;
  }  
} 

我什至尝试添加 https://api.binance.com:443/ 但没有成功。

websocket 连接工作正常:
wscat -c ws://LOADBALANCER-DNS/binance-ws/ws/btcusdt@aggTrade

然而,下面的一个失败了:
curl http://LOADBALANCER-DNS/binance-api/api/v3/time

当我看到 502 错误Nginx 日志时,我看到的是:
[error] 14339#14339: *20 SSL_do_handshake() Failed (SSL: error:1408F10B:SSL routines:ssl3_get_record:wrong version number) while SSL handshaking to upstream,client: 10.5.2.187,server:,request: "GET /binance-api/api/v3/time HTTP/1.1",upstream: "https://52.84.150.34:443/api/v3/time",host: "internal-prod-Nginx-proxy-xxxxxx.xxxxx.elb.amazonaws.com"

这是我试图从 Nginx 模拟的实际 RestAPI 调用
curl https://api.binance.com/api/v3/time

我浏览了许多几乎相似的帖子,但无法找到我哪里出错了。感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)