通过 https 到服务器的 Nginx 代理 - 证书上的主机名与 Host 标头不同

izr*_*rik 7 ssl proxy nginx

我想https://example.com在服务器 1 上接收流量。然后我想通过 https 将该流量代理到服务器 2。服务器 2 的 Nginx 设置了与服务器 1 完全相同的 tls 证书和密钥,因此理论上它应该能够提供服务请求。但是,当服务器 2 上的 Nginx 尝试将请求代理到服务器 2 时,它会将其发送到server2.example.com,这与证书上的通用名称不同,只是example.com.

有没有办法将 nginx 配置为期望主机提供的 tls 证书上的名称(在 tls 握手期间),它代理的请求与其代理的主机地址不同?

服务器 1 上的示例配置:

server {
    listen        443 ssl;
    server_name   example.com;

    ssl_certificate        /srv/tls/example.com.crt;
    ssl_certificate_key    /srv/tls/example.com.key;

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

服务器 2 上的示例配置:

server {
    listen        443 ssl;
    server_name   example.com;

    ssl_certificate        /srv/tls/example.com.crt;
    ssl_certificate_key    /srv/tls/example.com.key;

    location / {
        proxy_pass http://localhost:12345;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
}
Run Code Online (Sandbox Code Playgroud)

来自服务器 1 的示例 curl:

$ curl https://server2.example.com/chat -H "Host: example.com"
curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.
Run Code Online (Sandbox Code Playgroud)

如果需要,我可以生成一个新的自签名证书并在服务器 2 上使用它。但是,我认为只更改 Nginx 配置会更快。如果无法更改配置,我将创建一个新证书。

Ric*_*ith 9

您可以使用该proxy_ssl_name指令来指定代理主机证书的服务器名称。

例如:

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

有关详细信息,请参阅此文档