1SSL原理

SSL原理、生成SSL密钥对、Nginx配置SSL_linux

 

2生成ssl密钥对

2.1进入指定目录并生成私钥文件

[root@jimmylinux-001 ~]# cd /usr/local/nginx/conf
[root@jimmylinux-001 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:              输入密码abcd1234
Verifying - Enter pass phrase for tmp.key:  再次确认密码

2.2、转换key,取消密码。

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

2.3、删除tmp.key文件

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

2.4、生成证书请求文件,目的是为了和私钥一起生成公钥文件。

[root@jimmylinux-001 conf]# openssl req -new -key jimmy.key -out jimmy.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]:China
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:ShenZhen
Locality Name (eg, city) [Default City]:ShenZhen
Organization Name (eg, company) [Default Company Ltd]:jimmy
Organizational Unit Name (eg, section) []:jimmy
Common Name (eg, your name or your server's hostname) []:jimmylinux
Email Address []:joan2008.lms@gmail.com

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

2.5、用生成的证书请求文件jimmy.csr和之前的jimmy.key私钥文件一起生成公钥文件

[root@jimmylinux-001 conf]# openssl x509 -req -days 365 -in jimmy.csr -signkey jimmy.key -out jimmy.crt

SSL原理、生成SSL密钥对、Nginx配置SSL_SSL_02

SSL原理、生成SSL密钥对、Nginx配置SSL_SSL_03

这里的jimmy.key是私钥文件,jimmy.crt是公钥文件。

3Nginx配置ssl

已经有公钥和私钥,那么就可以配置Nginx的SSL了。

3.1、进入指定目录、然后新建一个配置文件。

[root@jimmylinux-001 vhost]# vim ssl.conf    新建配置文件

添加以下内容

server
{
    listen 443;                               监听端口
    server_name jimmy.com;
    index index.html index.php;
    root /data/wwwroot/jimmy.com;
    ssl on;                                   开启SSL支持HTTPS
    ssl_certificate jimmy.crt;                指定公钥
    ssl_certificate_key jimmy.key;            指定私钥
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;      SSL协议
}

SSL原理、生成SSL密钥对、Nginx配置SSL_nginx_04

3.2、-t检查配置文件的语法是否正确,如果出现以下报错,因为最早编辑Nginx的时候并没有指定支持SSL,这里需要重新编译nginx,加上--with-http_ssl_module

SSL原理、生成SSL密钥对、Nginx配置SSL_SSL_05

SSL原理、生成SSL密钥对、Nginx配置SSL_nginx_06

SSL原理、生成SSL密钥对、Nginx配置SSL_SSL_07

重新配置完以后,执行make和make install

[root@jimmylinux-001 nginx-1.12.1]# make

[root@jimmylinux-001 nginx-1.12.1]# make install

3.3、刚才编译完以后就会多一个支持HTTPS的SSL,重新-t检查配置文件语法也没有问题了。

SSL原理、生成SSL密钥对、Nginx配置SSL_nginx_08

重启Nginx并检查监听端口

[root@jimmylinux-001 nginx-1.12.1]# /etc/init.d/nginx restart  重启Nginx
Restarting nginx (via systemctl):                          [  确定  ]
[root@jimmylinux-001 nginx-1.12.1]# 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:80              0.0.0.0:*               LISTEN      4020/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      934/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1305/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      4020/nginx: master  多一个443的端口
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1310/php-fpm: maste 
tcp6       0      0 :::22                   :::*                    LISTEN      934/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1305/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1286/mysqld

3.4、进入指定目录,然后创建一个测试文件。

[root@jimmylinux-001 nginx-1.12.1]# cd /data/wwwroot/jimmy.com/

[root@jimmylinux-001 jimmy.com]# ls

[root@jimmylinux-001 jimmy.com]# vim 1.txt  新建测试文件

添加以下内容
This is SSL test page.

[root@jimmylinux-001 jimmy.com]# mv 1.txt index.html  重新更名成index.html文件

3.5、测试访问

[root@jimmylinux-001 jimmy.com]# curl -x127.0.0.1:443 https://jimmy.com/  如果直接使用curl方式访问,就会报400的错误状态码。
curl: (56) Received HTTP code 400 from proxy after CONNECT

需要修改hosts文件,然后再访问才可以。

SSL原理、生成SSL密钥对、Nginx配置SSL_nginx_09

SSL原理、生成SSL密钥对、Nginx配置SSL_nginx_10

可以编辑Windows的hosts文件,通过浏览器访问。

SSL原理、生成SSL密钥对、Nginx配置SSL_nginx_11

如果浏览器打开很慢无法访问,那么就要检查下防火墙了。

[root@jimmylinux-001 jimmy.com]# iptables -nvL  检查如果有防火墙

[root@jimmylinux-001 jimmy.com]# iptables -F    可以直接-F

刷新浏览器就可以访问了,如果不被浏览器认可的证书或者不合法的证书,https都会出现红色的显示。

SSL原理、生成SSL密钥对、Nginx配置SSL_linux_12

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: nginx