Nginx中实现自签名SSL证书生成与配置

2024-06-04 3226阅读

文章目录

  • 一.相关介绍
    • 1.生成步骤
    • 2.相关名词介绍
    • 二.Nginx中实现自签名SSL证书生成与配置
      • 1.私钥生成
      • 2.公钥生成
      • 3.生成解密的私钥key
      • 4.签名生成证书
      • 5.配置证书并验证
      • 6.登录

        一.相关介绍

        1.生成步骤

        (1)生成私钥(Private Key):使用 OpenSSL 工具生成一个私钥文件,用于加密和解密传输的数据

        (2)生成证书签名请求(Certificate Signing Request,CSR):使用 OpenSSL 工具生成一个 CSR 文件,其中包含你的服务器公钥和相关的信息,以便用于生成证书。

        (3)自签名证书生成:使用 OpenSSL 工具根据 CSR 文件和私钥生成自签名的 SSL 证书文件。

        (4)Nginx 配置修改:在 Nginx 配置文件中进行相应的修改,包括指定 SSL 证书文件路径、私钥文件路径以及其他相关的 SSL 配置项。

        总:Nginx 就可以使用自签名 SSL 证书来启用 HTTPS,实现加密和安全的通信。需要注意的是,自签名 SSL 证书不会受到公信任的证书颁发机构(Certificate Authority)认可,因此浏览器会显示安全警告。在生产环境中,建议使用由受信任的证书颁发机构签发的证书来获得更高的安全性和可信度。
        

        2.相关名词介绍

        (1)私钥(Key)生成:使用 OpenSSL 工具的 “genrsa” 命令生成私钥文件,其中私钥是用于加密和解密数据的关键。

        (2)公钥(CSR)生成:使用私钥文件生成证书签名请求(Certificate Signing Request,CSR),其中包含公钥和其他相关信息。这个公钥将被用于生成证书,并在浏览器连接时进行身份验证。

        (3)证书(CRT)生成:证书由公钥(CSR)和签名组成。签名可以是自签名的,也可以是由受信任的证书颁发机构(CA)签名的。通过使用私钥(Key)与公钥(CSR)进行签名,最终生成证书(CRT)文件。

        (4)服务器证书(server.crt):生成的证书文件就是服务器证书,通常命名为 “server.crt”。

        (5)签名过程:签名是使用私钥(Key)与公钥(CSR)进行证书生成的过程。私钥用于对公钥进行签名,以确保证书的完整性和身份验证。

        二.Nginx中实现自签名SSL证书生成与配置

        1.私钥生成

        #关闭防火墙及安全机制
        systemctl stop firewalld.service 
        setenforce 0
        
        #在root用户的家目录下执行
        cd ~
        #使用ssl生成私钥名为 server.key
        openssl genrsa -des3 -out server.key 1024
        #回车,输入自定义的密码文本,此处设置为12345
        #输入两次
        

        Nginx中实现自签名SSL证书生成与配置 第1张

        #查看生成的私钥
        cat server.key 
        

        Nginx中实现自签名SSL证书生成与配置 第2张

        2.公钥生成

        #基于创建的server.key私钥创建server.csr公钥
        openssl req -new -key server.key -out server.csr
        #查看私钥加密的内容
        openssl req -text -in server.csr -noout
        

        Nginx中实现自签名SSL证书生成与配置 第3张

        Nginx中实现自签名SSL证书生成与配置 第4张

        3.生成解密的私钥key

        #基于server.key私钥生成server.key.unsecure的解密私钥
        openssl rsa -in server.key -out server.key.unsecure
        

        Nginx中实现自签名SSL证书生成与配置 第5张

        4.签名生成证书

        方法1:
        #方法1需要输入密码,私钥密码为12345
        openssl  x509 -req -days 1000 -in server.csr -signkey server.key -out server.crt
        #使用私钥和公钥生成server.crt签名证书,-days为1000天 -in指定公钥,-signkey指定私钥,生成的前面证书为server.crt
        方法2:
        openssl x509 -req -days 1000 -in server.csr -signkey server.key.unsecure -out server1.crt
        #使用解密私钥和公钥生成server.crt签名证书,-days为1000天 -in指定公钥,-signkey指定解密后的私钥,生成的前面证书为server.crt
        

        Nginx中实现自签名SSL证书生成与配置 第6张

        Nginx中实现自签名SSL证书生成与配置 第7张

        #查看证书的内容,server.crt内容
        openssl x509 -text -in server.crt -noout
        

        Nginx中实现自签名SSL证书生成与配置 第8张

        5.配置证书并验证

        #安装额外源 并安装启动nginx
        yum install epel-release -y 
        yum install nginx -y
        systemctl start nginx 
        vim  /etc/nginx/nginx.conf
        #编辑nginx主配置文件文件末尾添加内容如下
        server {
            listen       443 ssl ;    
            server_name localhost ;
            ssl_certificate "/root/server.key";
            ssl_certificate_key "/root/server.key.unsecure";
        }
        #创建一个新的server模块,注意要在http模块里面,listen表示监听端口,server_name写主机地址或localhost都可以,ssl_certificate是签名证书的路径,ssl_certificate_key是私钥的路径,本文私钥路径写了解密后的私钥,写加密时的私钥有报错
        
        #重启nginx到浏览器上访问验证
        systemctl start nginx 
        

        报错信息:

        [root@test5 ~]# systemctl restart nginx

        Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.

        [root@test5 ~]# systemctl status nginx.service

        ● nginx.service - The nginx HTTP and reverse proxy server

        Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)

        Active: failed (Result: exit-code) since 四 2023-09-07 17:47:46 CST; 15s ago

        Process: 54283 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)

        Process: 55399 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)

        Process: 55397 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

        Main PID: 54285 (code=exited, status=0/SUCCESS)

        9月 07 17:47:46 test5 systemd[1]: Starting The nginx HTTP and reverse proxy se…

        9月 07 17:47:46 test5 nginx[55399]: nginx: [emerg] cannot load certificate key…e)

        9月 07 17:47:46 test5 nginx[55399]: nginx: configuration file /etc/nginx/nginx…ed

        9月 07 17:47:46 test5 systemd[1]: nginx.service: control process exited, code=…=1

        9月 07 17:47:46 test5 systemd[1]: Failed to start The nginx HTTP and reverse p…r.

        9月 07 17:47:46 test5 systemd[1]: Unit nginx.service entered failed state.

        9月 07 17:47:46 test5 systemd[1]: nginx.service failed.

        Hint: Some lines were ellipsized, use -l to show in full.

        解决方案:里面使用解密后的私钥文件路径

        vim /etc/nginx/nginx.conf

        ssl_certificate “/root/server.crt”;

        systemctl start nginx 
        

        6.登录

        https://192.168.198.15/

        Nginx中实现自签名SSL证书生成与配置 第9张

        Nginx中实现自签名SSL证书生成与配置 第10张

        Nginx中实现自签名SSL证书生成与配置 第11张

        Nginx中实现自签名SSL证书生成与配置 第12张


    免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

    目录[+]