在项目中需要使用基于证书的nginx双向认证,这里记录下配置方法。
备注:O=公司名称 OU=公司名称
openssl genrsa -out ca-key.pem 1024
openssl req -new -key ca-key.pem -out ca-req.csr -subj "/C=CN/ST=SZ/L=SZ/O=gongsi/OU=gongsi/CN=CA"
openssl x509 -req -in ca-req.csr -out ca-cert.pem -signkey ca-key.pem -days 3650
openssl x509 -req -in ca-req.csr -out ca-cert.cer -signkey ca-key.pem -CAcreateserial -days 3650
openssl genrsa -out client-key.pem 1024
openssl req -new -out client-req.csr -key client-key.pem -subj "/C=CN/ST=SZ/L=SZ/O=gongsi/OU=gongsi/CN=xx.com"
备注:CN=域名
openssl x509 -req -in client-req.csr -out client-cert.pem -signkey client-key.pem -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -days 3650
openssl x509 -req -in client-req.csr -out client-cert.cer -signkey client-key.pem -CA ca-cert.cer -CAkey ca-key.pem -CAcreateserial -days 3650
openssl verify -CAfile ca-cert.pem client-cert.pem
openssl pkcs12 -export -clcerts -in client-cert.cer -inkey client-key.pem -out client.p12
server {
listen 443 ssl;
server_name xx.com;
ssl_certificate /home/opt/ssl/3150613_xx.pem; ## 可以采用CA办法的证书
ssl_certificate_key /home/opt/ssl/3150613_xx.key;#可以采用CA办法的证书
ssl_client_certificate /home/opt/ht/ca-cert.pem; #自制根级证书公钥
ssl_verify_client on; #开启客户端证书验证
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
}
浏览器导入P12可以使用
本文为Lokie.Wang原创文章,转载无需和我联系,但请注明来自lokie博客http://lokie.wang