二、生成ssl密钥对
cd /usr/local/nginx/conf
yum install -y openssl
2.1 RSA算法生成key
[[email protected]03 conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
..........................+++
.................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:
Verifying - Enter pass phrase for tmp.key:
生成一个rsa类型的密钥,且长度为2048,但是我们有发现,让我们设置密码,如果每次有人访问我们的站点,都需要输入密码,这样太麻烦。
所以我们要转换私钥,取消密码(其实tmp.key与zhdy.key密钥内容是一样的,只不过一个有密码一个没有):
openssl rsa -in tmp.key -out zhdy.key
rm -f tmp.key
2.2 创建证书申请
[[email protected] conf]# openssl req -new -key zhdy.key -out zhdy.csr
然后后面会要求输入很多信息,例如国籍,户籍,公司,邮箱等!也就是别人在访问你的站点时显示的信息。
2.3 创建自签名的证书
[[email protected] conf]# openssl x509 -req -days 365 -in zhdy.csr -signkey zhdy.key -out zhdy.crt
Signature ok
subject=/C=cn/ST=js/L=sz/O=tc/OU=tcid/CN=zhdy/[email protected]
Getting Private key
365为过期时间;zhdy.crt为公钥
三、Nginx 配置ssl
3.1 配置文件:
vim /usr/local/nginx/conf/vhost/ssl.conf
加入如下配置文件:
server
{
listen 443;
server_name haha.com;
index index.html index.php;
root /data/wwwroot/www.haha.com;
ssl on;
ssl_certificate zhdy.crt;
ssl_certificate_key zhdy.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
ssl on; 代表着打开ssl
ssl_certificate zhdy.crt; 公钥
ssl_certificate_key zhdy.key; 私钥
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 三种不同的协议
3.2 测试:
[[email protected] vhost]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[[email protected]03 vhost]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx
出现错误,意思是:不晓得ssl,由于我们在编译的时候没有加入ssl。我们需要重新编译去解决这个问题;
[[email protected]03 vhost]# cd /usr/local/src/nginx-1.12.1/
[[email protected]03 nginx-1.12.1]# ./configure --help | grep -i ssl
--with-http_ssl_module
[[email protected]03 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[[email protected]03 nginx-1.12.1]# make && make install
然后再次去检测:
[root@zhdy-03 vhost]# /usr/local/nginx/sbin/nginx -t
[root@zhdy-03 nginx-1.12.1]# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ OK ]
[root@zhdy-03 nginx-1.12.1]# netstat -lntp
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 8051/nginx: master
由于已经设置了443,所以我们使用curl -x 是不生效的。
我们修改下/etc/hosts为;
192.168.230.137 www.haha.com
再次测试:
[[email protected]03 www.haha.com]# curl https://haha.com
curl: (60) Peer's Certificate has expired.
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
报错显示为“此证书非安全证书”,但是ssl是已经成功配置了,或许我们使用外部的机器去测试:
已经成功使用https访问。