配置Nginx自签名SSL证书,支持HTTPS

配置Nginx自签名SSL证书的流程

  • 生成一个SSL自签名证书
  • 客户端机器信任这个自签名证书
  • 修改RHEL服务器的Nginx配置
  • 在客户机用curl测试HTTPS

生成一个SSL自签名证书

在RHEL服务器上, 用openssl命令生成一个自签名证书

复制代码
openssl genrsa -out server.key 2048 #生成一个2048位的RSA私钥,保存到server.key
openssl req -x509 -new -nodes -key server.key -sha256 -days 3650 -out server.crt -subj "/C=CN/CN=www.petertest.com/O=MyRootCA" # 使用私钥server.key, 生成一个自签名的根证书server.crt, 有效期10年

注: CN=www.petertest.com需要换成你的服务器域名, 否则curl测试会报错

客户端机器信任这个自签名证书

将server.crt传到客户端Linux机器上。 在RHEL/Centos机器上, 需要把证书传到目录/etc/pki/ca-trust/source/anchors/,再用如下命令更新系统信任的CA证书列表

复制代码
update-ca-trust

修改RHEL服务器的Nginx配置

修改nginx.conf, 如下:

复制代码
    server {
        listen 443 ssl;
        server_name www.peter.com; # 设置域名

        access_log   access.log;
        include /etc/nginx/default.d/*.conf;

        ssl_certificate /server.crt; # 指定证书的绝对路径
        ssl_certificate_key /server.key; # 指定私钥的绝对路径

        location / {
            default_type text/html;
            return 200 '<h1>Welcome to Nginx</h1>';
        }
    }

再把证书文件server.crt和私钥文件server.key拷到服务器的根目录下

测试

在客户机用curl测试, 返回200 OK

复制代码
curl https://www.petertest.com:443 -i
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Wed, 11 Dec 2024 14:02:13 GMT
Content-Type: text/html
Content-Length: 25
Connection: keep-alive

<h1>Welcome to Nginx</h1>

参考

https://cloud.tencent.com/developer/article/1743989

相关推荐
jaysee-sjc12 小时前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
xbzb18 小时前
用 Secret + ConfigMap + PV/PVC 部署 WordPress:一套能跑 https 的最小组合
http·https·wordpress·configmap·secret·pod
Csvn18 小时前
Nginx 日志、监控与排障:全字段日志、条件日志与证据链定位
后端·nginx
杨凯凡1 天前
Spring Boot SSE 上线后不实时、频繁断线?Nginx 缓冲、心跳与连接回收实战
spring boot·后端·nginx
Csvn1 天前
Nginx 缓存体系:三层 key、七态 X-Cache-Status 与锁/stale 策略
后端·nginx
Csvn2 天前
Nginx 负载均衡进阶与健康检查:哈希、keepalive 池与 DNS 自愈
后端·nginx
橘色的喵3 天前
不接 SRS,只用 Nginx 做 WebRTC 播放:架构与踩坑
nginx·webrtc·srs·rtmp
Csvn3 天前
Nginx rewrite 与 server_name:last/break 语义、多域名匹配与伪静态
后端·nginx
亚那迦基3 天前
24小时高危漏洞速报,互联网惊天危机来临!PHP、Java、Node.js、NGINX、GitLab无一幸免,看看你家是否中招
nginx·gitlab·php
tonydf3 天前
没完了,Nginx又报新漏洞!
运维·nginx