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/



相关推荐
せいしゅん青春之我4 小时前
[JavaEE初阶]HTTPS-SSL传输过程中的加密
https·java-ee·ssl
任性不起来了4 小时前
宝塔面板点击ssl证书报错:出错了,面板运行时发生错误!ModuleNotFoundError: No module named ‘OpenSSL‘
网络·网络协议·ssl
Andya_net4 小时前
网络安全 | SSL/TLS 证书文件格式详解:PEM、CRT、CER、DER、PKI、PKCS12
安全·web安全·ssl
fruge12 小时前
Ubuntu服务器已下载Nginx安装包的安装指南
服务器·nginx·ubuntu
siriuuus13 小时前
Linux Tomcat 简单使用及 Nginx 反向代理
linux·nginx·tomcat
2501_9160074715 小时前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
li37149089016 小时前
nginx报400bad request 请求头过大异常处理
java·运维·nginx
2501_9159090619 小时前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview
Crazy________20 小时前
34部署LNMP架构详细解析
linux·运维·服务器·nginx
2501_915918411 天前
掌握 iOS 26 App 运行状况,多工具协作下的监控策略
android·ios·小程序·https·uni-app·iphone·webview