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),按官方流程说明。
三个最容易错的地方
Section titled “三个最容易错的地方”- 路径:
ssl_certificate指向的文件不存在 →nginx -t直接失败; - 私钥匹配:证书与私钥不是一对 →
nginx -t报key values mismatch; - 证书链:只放叶子证书,部分客户端校验失败 → 要把中间证书拼进同一文件。
这三条都能被 nginx -t 拦住,所以改完配置第一件事永远是 nginx -t。
自签证书最小验证
Section titled “自签证书最小验证”sudo dnf install -y nginx opensslsudo 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"; }}sudo nginx -t && sudo nginxnginx: configuration file /etc/nginx/nginx.conf test is successfulcurl -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"okNew, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384Protocol : TLSv1.3s_client 的输出确认协商到 TLSv1.3 与具体套件;如果这里是 TLSv1.2,说明配置里限制了协议版本。
certbot:先装 EPEL,再签,再验续期
Section titled “certbot:先装 EPEL,再签,再验续期”sudo dnf install -y epel-releasesudo dnf install -y certbot python3-certbot-nginxsudo certbot --nginx -d example.comsudo certbot renew --dry-runcertbot 来自 EPEL;--nginx 会自动改配置并 reload,前提是 nginx 在跑、域名解析到本机、80 端口可达。renew --dry-run 验证的续期链路,才是长期不翻车的关键。
- 改了证书没 reload:nginx 仍用旧证书;
- 私钥 644:应 600,避免其他用户读取;
- 只测 443 不测链路:
curl -k通不代表证书链正确,用openssl s_client -showcerts看完整链。
练习:自签 8443 + 失败用例
Section titled “练习:自签 8443 + 失败用例”sudo dnf install -y nginx opensslsudo 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.crtsudo 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"; }}EOFsudo nginx -t && sudo nginxcurl -sk https://127.0.0.1:8443/sudo sed -i 's#lab.crt#nope.crt#' /etc/nginx/conf.d/lab.confsudo nginx -t # 预期失败检查:
- 第一次
nginx -t成功、curl -sk返回ok s_client显示TLSv1.3- 改坏路径后
nginx -t明确报错 - 恢复路径后再次
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。
- 放行 443:firewalld 放行端口
- 容器服务:Quadlet 管理容器
- 安装后核对:新装系统 7 项核对