使用 SSL 客户端证书身份验证的 Nginx 代理到后端

Bas*_*974 18 nginx proxy ssl-certificate

我有两台服务器,都有 nginx。服务器 A 正在侦听 443 并配置为使用客户端 SSL 证书进行身份验证。

服务器 B 有一个内部进程,需要通过 nginx 与服务器 A 通信。

我想在服务器 B 上配置 Nginx,它将侦听 8080(没有加密,因为它都是本地通信)和 proxy_pass 到 ServerA:443。

问题是如何注入客户端证书?我没有找到任何可以做到这一点的 proxy_xxxx 函数。

我确实知道如何使用 socat 进行等效,但我的要求是使用 nginx。

小智 26

传递客户端证书详细信息是否足够?

你可以加

proxy_set_header X-SSL-CERT $ssl_client_escaped_cert;
Run Code Online (Sandbox Code Playgroud)

到您的配置,然后证书信息可通过 X-SSL-Cert 标头提供给服务器 B。

  • 根据 [nginx 的文档](http://nginx.org/en/docs/http/ngx_http_ssl_module.html#var_ssl_client_cert),不推荐使用 `$ssl_client_cert` 变量;应该改用`$ssl_client_escaped_cert` 变量。 (5认同)
  • @dubek 很好,在这种情况下,我会直接更新答案。 (2认同)

小智 5

显然,这就是您要查找的内容:http : //nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_certificate 自 1.7.8 版起可用。

location / {
    ...
    proxy_pass     the_other_nginx;
    proxy_ssl_certificate  the_certificate.pem;
    ...
}
Run Code Online (Sandbox Code Playgroud)


Wol*_*ahl 5

这个问题似乎主要是版本依赖。在 Ubuntu 14.04 LTS 上,默认的 nginx 是一个过时的 1.4。首先你需要安装一个基于 PPA 的版本

https://leftshift.io/upgrading-nginx-to-the-latest-version-on-ubuntu-servers

展示了如何做到这一点:

sudo add-apt-repository ppa:nginx/stable
sudo aptitude safe-upgrade
Run Code Online (Sandbox Code Playgroud)

你应该得到:

nginx -v
nginx version: nginx/1.8.0
Run Code Online (Sandbox Code Playgroud)

@xatr0z 回答https://serverfault.com/a/636455/162693指向 http://www.senginx.org/en/index.php/Proxy_HTTPS_Client_Certificate的配置 不起作用:

非工作提案

backend {
    server some-ip:443;
}

server {
    listen 80;


    location / {
        proxy_ssl_certificate        certs/client.crt;
        proxy_ssl_certificate_key    certs/client.key;


        proxy_pass https://backend;
    }
}
Run Code Online (Sandbox Code Playgroud)

不适用于 1.8.0 开箱即用。它可能只是作为一个提示,而不是用作配置文件本身或依赖于另一个版本。

我正在使用启用了 SSL 和自签名客户端证书的基于 apache2 的后端服务器 A 进行测试。Apache 配置 SSLOptions 设置为:

SSLOptions +ExportCertData +FakeBasicAuth + StdEnvVars
Run Code Online (Sandbox Code Playgroud)

这使得调试情况更容易,因为后端的 phpinfo() 脚本将显示服务器和客户端信息。

为了验证这一点,我使用了:

https://backend/test/phpinfo

在浏览器中安装 SSL 证书后,我得到如下部分:服务器证书的 SSL_SERVER_S_DN_CN 和客户端证书的 SSL_CLIENT_S_DN_CN。

作为第一次开始,我使用(填写括号中的部分)在前端服务器 B 上配置 nginx:

server {
  listen 8080;
  server_name <frontend>;

  location / {
    proxy_buffering off;
    proxy_pass https://<backend>;
    #proxy_ssl_certificate      certs/<SSL Client Certificate>.crt;
    #proxy_ssl_certificate_key  certs/<SSL Client Certificate>.key;
  }
}
Run Code Online (Sandbox Code Playgroud)

取消注释 SSL 客户端证书特定部分只是为了检查反向代理本身是否有效。

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
service nginx restart
nginx stop/waiting
nginx start/running, process 8931
Run Code Online (Sandbox Code Playgroud)

现在http://frontend:8080/test/phpinfo.php工作了

显示服务器证书的 SSL_SERVER_S_DN_CN 而客户端证书的 SSL_CLIENT_S_DN_CN尚未显示

现在取消注释后:

server {
  listen 8080;
  server_name <frontend>;

  location / {
    proxy_buffering off;
    proxy_pass https://<backend>;
    proxy_ssl_certificate      certs/<SSL Client Certificate>.crt;
    proxy_ssl_certificate_key  certs/<SSL Client Certificate>.key;
  }
}
Run Code Online (Sandbox Code Playgroud)

和检查/重新启动

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
service nginx restart
nginx stop/waiting
nginx start/running, process 8931
Run Code Online (Sandbox Code Playgroud)

http://frontend:8080/test/phpinfo.php工作和

SSL_SERVER_S_DN_CN服务器证书显示和客户端证书SSL_CLIENT_S_DN_CN显示

所以现在我们按要求工作了。

请注意错误https://trac.nginx.org/nginx/ticket/872#ticket