SLL工作流程:
浏览器发送一个https的请求给服务器;
服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;
服务器会把公钥传输给客户端;
客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
客户端把加密后的随机字符串传输给服务器;
服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
服务器把加密后的数据传输给客户端;
客户端收到数据后,再用自己的私钥也就是那个随机字符串解密;
一、实验
配置SSL之前,需要检查nginx是否有该模块–with-http_ssl_module,如果没有该模块需要重新编译nginx,具体操作参考nginx编译安装文档,openssl该命令需要安装openssl包获得!!
1:生成私钥
[root@proxy conf ~]# openssl genrsa -des3 -out tmp.key 2048
2:转换私钥,取消密码
[root@proxy conf ~]# openssl rsa -in tmp.key -out test.key
3:删除原私钥文件
[root@proxy conf ~]# rm -f tmp.key
4:生成证书请求文件,需要拿这个文件和私钥一起生产公钥文件
[root@proxy conf ~]# openssl req -new -key test.key -out test.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
Country Name (2 letter code) [XX]:CA
State or Province Name (full name) []:CA
Locality Name (eg, city) [Default City]:CA
Organization Name (eg, company) [Default Company Ltd]:CA
Organizational Unit Name (eg, section) []:CA
Common Name (eg, your name or your server’s hostname) []:test
Email Address []:CA
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
5:自己签发证书
[root@proxy conf ~]# openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
Signature ok
subject=/C=CA/ST=CA/L=CA/O=CA/OU=CA/CN=test/emailAddress=CA
Getting Private key
6:生成之后,配置nginx配置文件
[root@proxy vhosts ~]# vim test.conf
server
{
listen 443; ##开启https监听的443端口
server_name www.test.com;
index index.html index.php;
ssl on; ##on表示开启SSL,off关闭。
ssl_certificate test.crt; ##填写证书的名称
ssl_certificate_key test.key; ##填写秘钥的名称
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location /{
proxy_pass http:///192.168.1.10:8088;
proxy_set_header Host KaTeX parse error: Expected 'EOF', got '}' at position 13: proxy_Host;
}̲
}
说明:如果以上配置访问只… https://www.test.com/$1 permanent; ##永久重定向,访问网页强制跳转到https
location /{
proxy_pass http:///192.168.1.10:8088;
proxy_set_header Host $proxy_Host;
}
}
server{
listen 443 ssl;
server_name www.test.com;
ssl_certificate test.crt;
ssl_certificate_key test.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location /{
proxy_pass http://192.168.1.10:8088;
proxy_set_header Host $proxy_Host;
}
}
来源:CSDN
作者:沉淀的流星
链接:https://blog.csdn.net/ChenDianDeLiuXing/article/details/103455910