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

文章目录

一.相关介绍

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
#输入两次
#查看生成的私钥
cat server.key 

2.公钥生成

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

3.生成解密的私钥key

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

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
#查看证书的内容,server.crt内容
openssl x509 -text -in server.crt -noout

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/



相关推荐
乐辞3 小时前
localhost 自动被 redirect 到 https 地址的问题
网络协议·http·https
CSH05610 小时前
istio 网关开启https访问链接重置
https·istio
乐观的lishan11 小时前
python快速搭建https服务器
服务器·网络协议·https
智通全网络工作室12 小时前
HTTP 和 HTTPS 协议的区别?
网络协议·http·https
栗子~~13 小时前
Charles(青花瓷)抓取https请求
网络协议·http·https
小黑爱编程13 小时前
【Linux】图解详谈HTTPS的安全传输
linux·安全·https
活老鬼13 小时前
Nginx+Tomcat负载均衡、反向代理
运维·网络·数据库·nginx·http·tomcat·负载均衡
一颗去去20 小时前
【HTTP 和 HTTPS详解】1
网络协议·http·https
A864451 天前
解决在Nignx下Thinkphp路由不生效问题
开发语言·nginx·php·thinkphp
Sundayday471 天前
tomcat的安装,管理与配置
java·运维·服务器·nginx·tomcat·云计算