跳转到内容
搜索 Rocky Linux 文档

    浏览当前文档目录

    Rocky 9:nginx 配置 TLS 与自签证书验证

    在 Rocky Linux 9.8 特权容器(nginx 1.20.1,OpenSSL)中实际执行:openssl req -x509 生成自签证书、写 conf.d 的 8443 server、nginx -t、nginx 启动、curl -k、openssl s_client 看协议版本。日志:plans/content-expansion/evidence/logs/rocky-nginx-tls.log。

    Let’s Encrypt / certbot 未在本环境实测(需要公网域名与 80/443 端口),该部分按官方流程说明。

    Terminal window
    sudo dnf install -y nginx openssl
    sudo openssl req -x509 -nodes -newkey rsa:2048 -days 30 \
    -subj "/CN=lab.local" \
    -keyout /etc/pki/tls/private/lab.key \
    -out /etc/pki/tls/certs/lab.crt
    -rw-r--r-- 1 root root 1115 ... /etc/pki/tls/certs/lab.crt

    自签证书只适合测试:浏览器会报不受信任。生产用 ACME 签发的证书。

    server {
    listen 8443 ssl;
    server_name lab.local;
    ssl_certificate /etc/pki/tls/certs/lab.crt;
    ssl_certificate_key /etc/pki/tls/private/lab.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    location / { return 200 "ok\n"; }
    }

    放在 /etc/nginx/conf.d/lab.conf。证书链要拼在 ssl_certificate 里:EL9 的 nginx 1.20 仍接受 ssl_certificate 单文件,签发机构给的 fullchain 直接放这里。

    Terminal window
    sudo nginx -t
    sudo nginx
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

    nginx -t 会检查证书路径是否存在、私钥是否匹配;先测再 reload,否则一次 reload 就可能把线上打挂。

    Terminal window
    curl -sk https://127.0.0.1:8443/
    echo | openssl s_client -connect 127.0.0.1:8443 -servername lab.local 2>/dev/null | grep -E "Protocol|Cipher"
    ok
    New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
    Protocol : TLSv1.3

    curl -k 跳过证书校验(自签必然不通过),s_client 用来确认协商到的协议版本与套件。

    Terminal window
    sudo dnf install -y epel-release
    sudo dnf install -y certbot python3-certbot-nginx
    sudo certbot --nginx -d example.com
    sudo certbot renew --dry-run

    要点:certbot --nginx 需要 nginx 在跑且域名解析到本机、80 端口可达;renew --dry-run 用于验证续期链路。certbot 来自 EPEL,必须先装 epel-release。

    • 私钥权限:lab.key 不应全局可读,建议 600 且属主 root;
    • 证书链不全:只放站点证书会导致部分客户端校验失败,要放 fullchain;
    • 忘了 nginx -t:配置错误直接 reload 会中断服务。
    Terminal window
    sudo dnf install -y nginx openssl
    sudo openssl req -x509 -nodes -newkey rsa:2048 -days 7 -subj "/CN=lab.local" \
    -keyout /etc/pki/tls/private/lab.key -out /etc/pki/tls/certs/lab.crt
    sudo tee /etc/nginx/conf.d/lab.conf >/dev/null <<'EOF'
    server {
    listen 8443 ssl;
    server_name lab.local;
    ssl_certificate /etc/pki/tls/certs/lab.crt;
    ssl_certificate_key /etc/pki/tls/private/lab.key;
    location / { return 200 "ok\n"; }
    }
    EOF
    sudo nginx -t && sudo nginx
    curl -sk https://127.0.0.1:8443/

    检查:

    1. nginx -t 输出 successful
    2. curl -sk 返回 ok
    3. openssl s_client 显示 TLSv1.3
    4. 把证书路径改错后 nginx -t 报错(验证失败能被拦住)

    常见问题

    自签证书为什么 curl 要加 -k?

    自签证书不在系统信任库里,curl 会报证书验证失败。-k 跳过校验只用于测试,生产必须用受信任 CA 签发的证书。

    certbot --nginx 报错找不到 server 块?

    certbot 需要能从 nginx 配置里找到匹配域名的 server_name;确认域名解析、80 端口可达,且 nginx 正在运行。

    ssl_certificate 要放几个文件?

    EL9 的 nginx 用单个文件即可:把站点证书与中间证书按顺序拼接(fullchain)放进 ssl_certificate,私钥放 ssl_certificate_key。