使用上游 SSL 将 Nginx 配置为反向代理

Ale*_*Flo 49 ssl nginx reverse-proxy

我尝试将 Nginx 服务器配置为反向代理,以便它从客户端收到的 https 请求也通过 https 转发到上游服务器。

这是我使用的配置:

http {

    # enable reverse proxy
    proxy_redirect              off;
    proxy_set_header            Host            $http_host;
    proxy_set_header            X-Real-IP       $remote_addr;
    proxy_set_header            X-Forwared-For  $proxy_add_x_forwarded_for;

    upstream streaming_example_com 
    {
          server WEBSERVER_IP:443; 
    }

    server 
    {
        listen      443 default ssl;
        server_name streaming.example.com;
        access_log  /tmp/nginx_reverse_access.log;
        error_log   /tmp/nginx_reverse_error.log;
        root        /usr/local/nginx/html;
        index       index.html;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_certificate /etc/nginx/ssl/example.com.crt;
        ssl_certificate_key /etc/nginx/ssl/example.com.key;
        ssl_verify_client off;
        ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers RC4:HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;


        location /
        {
            proxy_pass  https://streaming_example_com;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

无论如何,当我尝试使用反向代理访问文件时,这是我在反向代理日志中得到的错误:

2014/03/20 12:09:07 [错误] 4113079#0:*1 SSL_do_handshake() 失败(SSL:错误:1408E0F4:SSL 例程:SSL3_GET_MESSAGE:意外消息),而 SSL 与上游握手,客户端:191.2,168服务器:streaming.example.com,请求:“GET /publishers/0/645/_teaser.jpg HTTP/1.1”,上游:“ https://MYSERVER.COM:443/publishers/0/645/_teaser.jpg ” ,主持人:“streaming.example.com”

知道我做错了什么吗?

Ale*_*Flo 36

我发现了错误是什么,我需要添加 proxy_ssl_session_reuse off;


iBu*_*Bug 20

就我而言,我试图反向代理 Cloudflare 背后的网站。我在/var/log/nginx/error.log. 我尝试了很多解决方案,这个对我有用:

proxy_ssl_server_name on;
Run Code Online (Sandbox Code Playgroud)

是的,即使是 2019 年,现在某些服务仍然需要 SNI 来区分托管站点。


小智 5

这完全解决了我的问题:

location / {
    proxy_pass https://server2.example.com;
    proxy_set_header Host $host;
    proxy_ssl_name $host;
    proxy_ssl_server_name on;
    proxy_ssl_session_reuse off;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我还必须添加proxy_ssl_name以确保 nginx 知道要传递给上游 https 服务器的名称。