HTTP nginx 到 HTTPS proxy_pass 返回 504 BAD 网关

Ron*_*las 1 nginx nginx-reverse-proxy

我正在尝试创建一个 https 地址的 proxy_pass (我的 nginx 使用纯 HTTP 协议在 80 下运行)。

这是我在conf文件中的声明:

   location /viacep/ {
                  proxy_pass https://viacep.com.br/;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_set_header Host $host;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
Run Code Online (Sandbox Code Playgroud)

好吧,这个想法是,当我输入时,localhost/viacep/ws/09340400/json我在引擎盖下得到了以下地址解析: https: //viacep.com.br/ws/09340400/json。但我在 error.log 文件中收到以下错误:

2021/12/28 09:32:59 [error] 34664#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /viacep/ws/09540400/json HTTP/1.1", upstream: "https://165.227.126.241:443/ws/09540400/json", host: "localhost"
Run Code Online (Sandbox Code Playgroud)

我想这个错误是由于地址解析(https://165.227.126.241:443/ws/09540400/json)而发生的,使用IP而不是DNS查看它。

编辑1

我尝试添加proxy_ssl_server_name on;但同样的错误。

Tim*_*ark 5

NGINX 始终会将 DNS 名称解析为 IP 地址。

问题可能出在后端服务器 SNI 上。鉴于此服务器上托管有多个站点,并且该服务器支持 SNI,您应该在 NGINX 配置中server name使用它来发送。proxy_ssl_server_name on;

我刚刚在我的 NGINX 版本上配置了它1.20

server {
  listen 80;

  location / {
    proxy_pass https://viacep.com.br/;
    proxy_set_header Host viacep.com.br;
    proxy_set_header X-Forwarded-Proto https;
    proxy_ssl_server_name on;

  }
}

Run Code Online (Sandbox Code Playgroud)

确保您发送正确的Host标头。在您的配置中,您将发送localhostHost上游服务器。正如您所注意到的那样,这不会起作用。