前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx 配置ssl

Nginx 配置ssl

作者头像
老七Linux
发布2018-05-09 16:18:36
4.9K0
发布2018-05-09 16:18:36
举报

一、ssl原理:

  1. 浏览器发送一个https的请求给服务器;
  2. 服务器要有一套数字证书,可以自己制作(后面的操作就是自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出提示页面,这套证书其实就是一对公钥和私钥;
  3. 服务器会把公钥传输给客户端;
  4. 客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
  5. 客户端把加密后的随机字符串传输给服务器;
  6. 服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
  7. 服务器把加密后的数据传输给客户端;
  8. 客户端收到数据后,再用自己的私钥也就是那个随机字符串解密;
原理图:
mark
mark

二、生成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是已经成功配置了,或许我们使用外部的机器去测试:

mark
mark

已经成功使用https访问。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016/08/15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、ssl原理:
    • 原理图:
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档