首页 > 系统相关 >SSL原理、生成SSL密钥对、Nginx配置SSL

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

时间:2023-08-19 10:32:48浏览次数:34  
标签:jimmylinux jimmy Nginx 0.0 SSL 001 密钥 key root

1SSL原理

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

 

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 []:[email protected]

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_nginx_02

SSL原理、生成SSL密钥对、Nginx配置SSL_linux_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_linux_04

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

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

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

SSL原理、生成SSL密钥对、Nginx配置SSL_nginx_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_linux_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_SSL_09

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

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

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

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

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

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

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

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

 

标签:jimmylinux,jimmy,Nginx,0.0,SSL,001,密钥,key,root
From: https://blog.51cto.com/u_15867943/7147154

相关文章

  • Nginx
    Nginx安装#安装Nginx所需要的依赖yuminstallpcre-develzlibzlib-developensslopenssl-devel#解压Nginx#到nginx目录./configuremakeinstallcd/usr/local/nginx#启动nginxcdsbin./nginx#关闭./nginx-squit/stopquit停止外面请求,然后将nginx......
  • Nginx的安装以及相关代理配置
    前言什么是NginxNginx是一个高性能的HTTP和反向代理Web服务器,同时也提供IMAP/POP3/SMTP服务。Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3/SMTP)代理服务器。Nginx的特点是:占有内存少,并发能力强。Nginx专门为性能优化而开发,性能是最重要的考量,非常注重效率......
  • nginx根据ip的地理位置进行转发代理(GeoIP2)
    nginx要获取到ip地理位置,需要在nginx引用第三方ngx_http_geoip2_module模块,而ngx_http_geoip2_module模块依赖libmaxminddb;另外ip对应的地理位置离线的需要从GeoIP2站点上下载下来;最后在nginx.conf文件中引用ngx_http_geoip2_module模块,配置离线数据库才可以获取地理位置nginx......
  • 深入探究 Nginx 负载均衡与反向代理
    在现代Web应用中,处理高并发和确保可用性是至关重要的。Nginx是一个强大的开源服务器,可以作为负载均衡器和反向代理来提供性能优化和高可用性。本文将深入探讨Nginx的负载均衡和反向代理原理,以及如何在后端架构中应用它。负载均衡的原理负载均衡是将传入的请求分发到多个服务器......
  • nginx报错: nginx: [error] open() "/opt/nginx/nginx.pid" failed (2: No such file
    出现故障的原因:nginx:[error]open()"/opt/nginx/nginx.pid"failed(2:Nosuchfileordirectory)   服务器重启后,重新启动nginx报错nginx.pid这个文件找不到了!       因为每次重新启动系统,nginx.pid被自动删除。解决方案:    重新生......
  • python3添加ssl模块
    1.安装opensslwget https://www.openssl.org/source/openssl-1.1.1v.tar.gztar openssl-1.1.1v.tar.gzcd openssl-1.1.1v./config--prefix=/usr/local/opensslsharedzlib测试/usr/local/openssl/bin/openssl 若报错:/usr/local/openssl/bin/openssl:errorwhileloa......
  • 为远程群晖NAS的自定义域名免费申请SSL证书
    概述ERP系统对于企业来说重要性不言而喻,不管是财务、生产、销售还是采购,都需要用到ERP系统来协助。但ERP中这些重要数据属于企业机密文档,往往需要本地化管理,只能部署在企业内网之下。有时候我们会遇到在外需要远程登录ERP临时处理紧急事务,我们可以通过内网穿透来解决,将ERP服务端端......
  • #yyds干货盘点#FastDFS配置Nginx访问
    下载相关依赖软件包yum-yinstallwgetmakezlibzlib-develgcc-c++libtoolopensslopenssl-develwgethttp://nginx.org/download/nginx-1.10.2.tar.gztar-xzvfnginx-1.10.2.tar.gz安装Nginxcdnginx-1.10.2./configure--prefix=/data/apps/nginx-download\--p......
  • 安装SSL证书还需要注意什么?
    在保护网站数据安全和用户隐私方面,SSL证书发挥着重要的作用。然而,仅仅申请SSL证书还不足以确保网站安全,正确的证书安装也是至关重要的。本文将介绍安装SSL证书时需要注意的关键事项,以确保顺利进行和正确配置。一、选择合适的SSL证书:在安装SSL证书之前,首先要选择适合您网站需求的SSL......
  • nginx 过滤相关的referer 和 origin
    1.相关配置如下location/{#set$allow_cors0;##判断不为空#if($http_origin){#set$allow_cors1;#}#set$flag0;valid_referers10.800;......