在Nginx中向HTTPS服务器的代理HTTP请求

时间:2020-01-16 16:03:28

标签: ssl nginx https proxy reverse-proxy

我想设置一个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/"

此错误的原因是什么?我该如何解决它或设置代理?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 我在此stackoverflow answer

中发现了解决该问题的神奇措施

“尝试将proxy_ssl_server_name添加到;到您的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;
}

我可以再次享受生活

相关问题