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 systemd1: Starting The nginx HTTP and reverse proxy se...

9月 07 17:47:46 test5 nginx55399: nginx: emerg cannot load certificate key...e)

9月 07 17:47:46 test5 nginx55399: nginx: configuration file /etc/nginx/nginx...ed

9月 07 17:47:46 test5 systemd1: nginx.service: control process exited, code=...=1

9月 07 17:47:46 test5 systemd1: Failed to start The nginx HTTP and reverse p...r.

9月 07 17:47:46 test5 systemd1: Unit nginx.service entered failed state.

9月 07 17:47:46 test5 systemd1: 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/



相关推荐
xixingzhe25 小时前
Nginx 看懂请求耗时瓶颈
服务器·前端·nginx
北风toto8 小时前
Windows 下用 OpenSSL 生成 SSL 密钥并配置 Nginx 教程
windows·nginx·ssl
卜锦元10 小时前
Docker Compose 部署 Nginx 1.30.6 完整指南
nginx·docker·容器
小小龙学IT13 小时前
Nginx 开源 Web 服务器与反向代理深度解
前端·nginx·开源
青瓦梦滋13 小时前
【在线五子棋对战】游戏匹配模块
网络·c++·网络协议·http·https
zxanz113 小时前
一张 SSL 证书,如何降低业务流量劫持与数据泄露风险
网络·网络协议·ssl
Coisinier1 天前
docker命令管理镜像及容器
linux·运维·nginx·docker·容器·操作系统
2301_32241428041 天前
基于web的校园失物招领管理系统59695-计算机毕设 原创(免费领源码+部署教程+开发环境)
mysql·nginx·php·apache·thinkphp·phpstudy·wampserver
梦帮科技1 天前
Web 漏洞攻防深度:WAF 规则逃逸技术(畸形分块传输、双重编码)与对抗加固
linux·前端·网络安全·ssl·web
flytalei1 天前
从YAML到Nginx服务访问:动手理解 Kubernetes Pod、Deployment 与 Service
运维·nginx·kubernetes