跳转到内容
搜索 AlmaLinux 文档

    浏览当前文档目录

    AlmaLinux 9:nginx 的 TLS 配置与验证

    在 AlmaLinux 9.8 特权容器(nginx 1.20.1 + OpenSSL)中实测:openssl req -x509、写 conf.d/lab.conf、nginx -t、启动 nginx、curl -sk、openssl s_client。日志:plans/content-expansion/evidence/logs/alma-nginx-tls.log。

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

    1. 路径:ssl_certificate 指向的文件不存在 → nginx -t 直接失败;
    2. 私钥匹配:证书与私钥不是一对 → nginx -t 报 key values mismatch;
    3. 证书链:只放叶子证书,部分客户端校验失败 → 要把中间证书拼进同一文件。

    这三条都能被 nginx -t 拦住,所以改完配置第一件事永远是 nginx -t。

    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
    -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;
    location / { return 200 "ok\n"; }
    }
    Terminal window
    sudo nginx -t && sudo nginx
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    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

    s_client 的输出确认协商到 TLSv1.3 与具体套件;如果这里是 TLSv1.2,说明配置里限制了协议版本。

    certbot:先装 EPEL,再签,再验续期

    Section titled “certbot:先装 EPEL,再签,再验续期”
    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 来自 EPEL;--nginx 会自动改配置并 reload,前提是 nginx 在跑、域名解析到本机、80 端口可达。renew --dry-run 验证的续期链路,才是长期不翻车的关键。

    • 改了证书没 reload:nginx 仍用旧证书;
    • 私钥 644:应 600,避免其他用户读取;
    • 只测 443 不测链路:curl -k 通不代表证书链正确,用 openssl s_client -showcerts 看完整链。
    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/
    sudo sed -i 's#lab.crt#nope.crt#' /etc/nginx/conf.d/lab.conf
    sudo nginx -t # 预期失败

    检查:

    1. 第一次 nginx -t 成功、curl -sk 返回 ok
    2. s_client 显示 TLSv1.3
    3. 改坏路径后 nginx -t 明确报错
    4. 恢复路径后再次 nginx -t 通过

    常见问题

    nginx -t 报 key values mismatch 是什么原因?

    证书与私钥不是一对(比如换了证书没换私钥)。用 openssl x509 -noout -modulus 与 openssl rsa -noout -modulus 对比两者模数即可确认。

    证书链不全有什么表现?

    浏览器与 curl 可能正常,但部分客户端或 Android 设备报证书错误。用 openssl s_client -showcerts 看是否包含中间证书。

    certbot 续期后 nginx 需要手动 reload 吗?

    certbot 的 deploy hook 通常会自动 reload;若用 --webroot 等方式没配 hook,需要自己加 systemctl reload nginx 或 cron。