配置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

相关推荐
zzu123zsw34 分钟前
从 proxy_pass 末尾斜杠到 WebAssembly 预压缩:深挖 Nginx 生产环境四大高频踩坑点
运维·nginx·wasm·webassembly·devops·运维排错
郝亚军1 小时前
RK3562 中有 mosquitto、nginx 了,还需要 http,ftp,tftp 等协议吗?
运维·nginx·http
Lsetea6 小时前
OpenSSL报certificate revoked:error 23与CRL吊销层级排查
linux·https·ssl证书·openssl·crl
HelloWorld工程师6 小时前
不改动任何历史配置,也能让 SSL 证书自动续期
linux·服务器·网络协议·https·ssl
Lsetea7 小时前
curl走HTTPS代理报SSL证书错误:proxy-cacert与cacert双层信任排查
linux·https·ssl证书·curl·代理服务器
2501_9378609410 小时前
HTTP/HTTPS 学习笔记(中):报头、Cookie、状态码与请求构造
学习·http·https
Peter-Code11 小时前
Nginx 常用操作整理(前端)
服务器·前端·nginx
Lsetea1 天前
OpenSSL报certificate chain too long:error 22与verify_depth深度排查
linux·https·ssl证书·openssl·证书链
Sayai1 天前
Hadoop 集群安全加固实战:YARN 未授权访问漏洞修复,firewalld 白名单 + Nginx 反向代理 Basic Auth 全流程
运维·hadoop·nginx·安全·网络安全