个人环境
(1)Centos 7.0
(2)Nginx-1.14.0
我的nginx版本较老,其他也可以。但前提是你的nginx是用源码安装的
1.http与https的区别
简单的来说:
(1)http是超文本传输协议,而且信息是明文传输,https则是具有安全性的ssl加密传输协议。
(2)https协议需要到ca申请证书,并且这个是需要付费的。
但是,我们在线下做测试啊,等等情况需要用到,你总不能因为这个就去买一个吧 ,要不然你还来上班干嘛呀,是不是?🤭
2.使用openssl生成证书
注意:在配置之前先安装好http,因为使用的pki认证工具基于apache。
[root@localhost ~]# cd /etc/pki/CA (若没有该目录,则安装一下apache)
[root@localhost CA]# ls
certs crl newcerts private
2.1 生成私钥
[root@localhost CA]# openssl genrsa -out private/cakey.pem 2048
Generating RSA private key, 2048 bit long modulus
.......+++
....................................+++
e is 65537 (0x10001)
[root@localhost CA]# ll private/cakey.pem
-rw-r--r-- 1 root root 1675 3月 18 19:00 private/cakey.pem
2.2 生成自签名证书
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365 //有效期一年
Country Name (2 letter code) [XX]:CN //单位所在国家,为两位数的国家缩写,如: CN 就是中国
State or Province Name (full name) []:SX //单位所在州或省
Locality Name (eg, city) [Default City]:XIAN //单位所在城市 / 或县区
Organization Name (eg, company) [Default Company Ltd]:xiaoming //此网站的单位名称;
Organizational Unit Name (eg, section) []:yunwei //下属部门名称;
Common Name (eg, your name or your server's hostname) []:www.xiaozhang.com //网站的域名;
Email Address []:xiaozhang@163.com //邮箱地址
2.3 生成证书索引数据库文件及证书序列号文件
[root@localhost CA]# touch index.txt
[root@localhost CA]# echo 01 > serial
以上部分和下面步骤没有直接联系,如果安装错误了,这样方便判断出错误地方。
2.4 为nginx生成证书
[root@localhost CA]# cd /opt/data/nginx/ //nginx的安装目录
[root@localhost nginx]# mkdir certs
[root@localhost nginx]# cd certs/
2.5 生成私钥文件
[root@localhost certs]# (umask 077;openssl genrsa -out nginx.key 2048)
Generating RSA private key, 2048 bit long modulus
..................................+++
..........+++
e is 65537 (0x10001)
[root@localhost certs]# ll
总用量 4
-rw------- 1 root root 1675 3月 18 19:18 nginx.key
2.6 生成证书签署请求
[root@localhost certs]# openssl req -new -key nginx.key -out nginx.csr
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:SX
Locality Name (eg, city) [Default City]:XIAN
Organization Name (eg, company) [Default Company Ltd]:www.xiaoming.com
Organizational Unit Name (eg, section) []:yunwei
Common Name (eg, your name or your server's hostname) []:192.168.239.3 //需要和你的服务器名称相同
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
可以看到剩下的我都没填,其实填不填看心情,都可以的。但是我注释的这个一定要写对,不然会出错。
2.7 创建签署证书存放的目录
[root@localhost certs]# cd /etc/nginx/
[root@localhost nginx]# mkdir certs
2.8 签署证书
[root@localhost certs]# openssl ca -in nginx.csr -out /etc/nginx/certs/nginx.crt
到此,证书就生成完成。
3.nginx配置https
[root@www CA]# cd /etc/nginx/conf.d/
[root@www conf.d]# ls
xingyun.conf
[root@www conf.d]# vim xingyun.conf
4.1浏览器访问https
访问成功,至此,nginx配置SSL模块结束。
若要实现http与https的跳转,在配置https的基础上加以下命令