【问题标题】:Proxy HTTP requests to an HTTPS server in nginx代理 HTTP 请求到 nginx 中的 HTTPS 服务器
【发布时间】:2020-05-03 12:40:00
【问题描述】:

我想设置一个 nginx 实例,它将从 /api 开始的 HTTP 请求代理到 HTTPS 服务器,但 /api URL 段应该省略。 例如,实例应该监听 localhost:9817。如果它收到对http://localhost:9817/api/auth 的请求,它应该将其代理为https://api.com:8443/auth

这是我的 nginx 配置:

events {
    worker_connections  1024;
}

http {
    server {
        listen       9817;
        server_name  localhost;

        location /api {
            rewrite  ^/api(/.*) $1 break;
            proxy_pass https://api.com:8443;
            proxy_set_header Host $host;
            proxy_http_version 1.1;
        }

        location / {
            root  "D:/Project/dist";
            try_files $uri $uri/ /index.html;
        }
    }
}

但是,我在 error.log 中收到以下错误:

2020/01/16 17:54:22 [error] 420512#426216: *31 peer closed connection in SSL handshake (10054: An existing connection was forcibly closed by the remote host) while SSL handshaking to upstream, client: 127.0.0.1, server: localhost, request: "POST /api/auth HTTP/1.1", upstream: "https://16.53.35.38:8443/auth", host: "localhost:9817", referrer: "http://localhost:9817/"

这个错误的原因是什么?如何修复它或根据需要设置代理?

【问题讨论】:

  • 您好,我遇到了同样的错误。谷歌不知道这个错误。你修复了这个错误吗?
  • 您好!不,我没有。我的项目不再需要这种代理,因此问题变得无关紧要。

标签: ssl nginx https proxy reverse-proxy


【解决方案1】:

我遇到了同样的问题。 我在stackoverflow answer 中找到了修复它的神奇操作@

“尝试将 proxy_ssl_server_name on; 添加到您的 proxy_pass 块中,看看是否有帮助”

它在我的 nginx.conf 中

   location / {
    proxy_pass https://$http_x_forwarded_to; 
}

修改后变成了

   location / {
    proxy_pass https://$http_x_forwarded_to; 
    proxy_ssl_server_name on;
}

我又可以享受生活了

【讨论】:

  • 我刚刚尝试重现我在问题中描述的情况。它以某种方式起作用!没有错误。即使没有您的解决方案。问题可能是由于 nginx 配置以外的其他原因。无论如何,感谢您的参与!
  • 该错误仅针对特定地址的请求 (bla-bla.api.kz)。一切都适用于其他地址,无需更改。问题服务器的异常是什么 - 我不明白
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 2017-05-21
  • 2022-06-16
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2020-03-23
相关资源
最近更新 更多