如何设置expressjs的请求以正确识别与https nginx服务器的TLS连接,以便我可以通过getPeerCertificate
执行身份验证?
这是我的nginx配置,用于将请求转移到expressjs api
location /api {
proxy_pass http://10.88.132.14:4337/api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
答案 0 :(得分:0)
您需要传递SSL令牌,然后手动对其进行解码。您可以通过将X-SSL-CERT
与$ssl_client_escaped_cert
相加来传递它。确保您使用的是Nginx 1.13或更高版本,因为$ssl_client_escaped_cert
在1.12中不存在。
location /api {
proxy_pass http://10.88.132.14:4337/api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-SSL-CERT $ssl_client_escaped_cert;
}
现在您不能使用getPeerCertifice()
,因为这需要完整的SSL连接。相反,您可以使用x-ssl-cert
包从上方解码x509
标头:
let cert = req.headers['x-ssl-cert'];
try {
cert = decodeURIComponent(cert);
console.log(x509.getSubject(cert));
} catch (error) {
console.log('Bad SSL-certificate?', error);
}