LNMP架构——HTTPS原理及Ngnix配置

ssl原理

LNMP架构——HTTPS原理及Ngnix配置_第1张图片

1, 客户端向服务器发送https请求;
2, 服务器上存储了一套数字证书,其实质为一对公私钥。数字证书可以自己制作,也可以向组织申请。前者在客户端访问时需要验证才能继续访问;后者不会弹出验证提示;
3, 服务器将公钥传输给客户端;
4,客户端验证公钥是否合法:无效(自己制作的)会弹出警告,有效的则生成一串随机数,用此随机数加密公钥;
5, 客户端将加密后的字符串传输给服务器 服务器收到字符串后,先使用私钥进行解密,获取加密使用的随机数,并以此随机数加密传输的数据(对称机密);
6, 服务器将加密后的数据传输给客户端; 客户端收到数据后,使用自己的私钥(即随机字符串)进行解密。
7,服务器将加密后的数据传输给客户端;
8,客户端收到数据后,使用自己的私钥(即随机字符串)进行解密。

说明:对称加密:将数据和私钥(随机字符串)通过某种算法混合在一起,除非知道私钥,否则无法解密。


生成SSL密钥对

创建私钥Key

[root@dl-001 ~]# cd /usr/local/nginx/conf

// 创建私钥key文件,必须输入密码,否则无法生成key文件
[root@localhost 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:

转换key,取消密码

[root@dl-001 conf]# openssl rsa -in tmp.key -out test.key
Enter pass phrase for tmp.key:
writing RSA key

[root@dl-001 conf]# rm -f tmp.key 

生成证书

[root@dl-001 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]:CN    
State or Province Name (full name) []:ZheJiang
Locality Name (eg, city) [Default City]:QuZhou
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

// 需要使用csr文件与私钥一起生成.crt文件
[root@dl-001 conf]# openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
Signature ok
subject=/C=CN/ST=ZheJiang/L=QuZhou/O=Default Company Ltd
Getting Private key

Nginx配置SSL

创建新虚拟主机配置文件

[root@dl-001 conf]#vim /usr/local/nginx/conf/vhost/ssl.conf
server
{
    listen 443;
    server_name test.com;
    index index.html index.php;
    root /data/www/test.com;
    ssl on;
    ssl_certificate test.crt;
    ssl_certificate_key test.key;
    ssl_protocols TLSv1 TLS1.1 TLS1.2;
}

创建对应目录及文件

[root@dl-001 conf]# mkdir -p /data/www/test.com
[root@dl-001 conf]# vim /data/www/test.com/index.php
ssl test page.

重载服务

[root@dl-001 conf]# /usr/local/nginx/sbin/nginx -t
[root@dl-001 conf]# /usr/local/nginx/sbin/nginx -s reload

设置时报错 – unknown directive “ssl”

这时由于一开始编译时未将http_ssl_module模块编译进nginx,需要重新编译安装

[root@dl-001 conf]# cd /usr/local/src/nginx-1.12.2/
[root@dl-001 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@dl-001 nginx-1.12.2]# make && make install

说明:重新编译后将导致之前配置的虚拟主机配置文件丢失,最后在重新编译前对有用的nginx虚拟主机文件进行备份


编译完成后查看

[root@dl-001 conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx/ --with-http_ssl_module

重启Nginx服务

// 重新编译后的nginx必须使用/etc/init.d/nginx脚本进行重启
[root@dl-001 conf]# /etc/init.d/nginx restart
Restarting nginx (via systemctl):                          [  确定  ]

// 查看443端口是否开放
[root@dl-001 conf]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1354/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2116/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      4953/nginx: master  
tcp6       0      0 :::3306                 :::*                    LISTEN      2156/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1354/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2116/master         

测试

1,使用curl

// 如果不想使用-x指定ip,可以在/etc/hosts内添加如下代码
[root@dl-001 conf]# vim /etc/hosts
127.0.0.1 test.com

// curl测试
[root@dl-001 conf]# curl https://test.com
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
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.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

2,使用浏览器

  • 如果使用浏览器需要在物理机上的hosts中添加ip 和域名,才能进行访问。
  • 同时要检查服务器端的防火墙是否开放443端口,这里为了测试方便,直接清空了iptables规则表
[root@dl-001 conf]# iptables -F
  • 由于证书是自己创建的,所以会显示无效的证书,点击“仍要继续”即可访问。但是并不能达到安全的效果,需要购买证书。

你可能感兴趣的:(LNMP架构,ssl,加密)