跳到主要内容
ssl证书按装

SSL 证书安装配置实战:Nginx/Apache/IIS/Tomcat 部署 HTTPS 与绿锁排错全指南

SSL 证书安装配置实战:Nginx/Apache/IIS/Tomcat 部署 HTTPS 与绿锁排错全指南 — SSL 证书安装配置实战:Nginx/Apache/IIS/Tomcat 部署 HTTPS 与绿锁排错全指南

问题背景与根因:证书装上了,浏览器为什么还是不给绿锁

绝大多数“HTTPS 部署失败”都不是证书买错了,而是链路里某一环没闭合。先把症状归类,能省掉一半排查时间:

  • 地址栏显示“不安全”或锁图标带红斜杠:页面里仍有 http:// 资源(混合内容),或证书链不完整。
  • NET::ERR_CERT_AUTHORITY_INVALID:只部署了服务器证书,没带中间证书(Intermediate CA)。安卓 App、Java 客户端和部分旧浏览器最先报错,而桌面 Chrome 因为能通过 AIA 自动补链反而看起来正常。
  • NET::ERR_CERT_COMMON_NAME_INVALID 或 SSL_ERROR_BAD_CERT_DOMAIN:证书 SAN 里没有当前访问的域名,例如只签了 example.com 却用 www.example.com 访问。
  • ERR_SSL_PROTOCOL_ERROR:443 端口被纯 HTTP 服务占用,或客户端只支持 TLS 1.0/1.1 而服务端已禁用。
  • 多站点串证:同一 IP 上多个 HTTPS 站点没开 SNI,浏览器拿到的是第一个站点的证书。

另一类根因是“有效期规则变了”。自 2020-09-01 起,公有 CA 签发的 TLS 证书最长有效期被限制为 398 天;Let’s Encrypt 免费证书固定 90 天。CA/Browser Forum 已通过分阶段缩短方案:2026-03-15 起降为 200 天,2027-03-15 起 100 天,2029-03-15 起 47 天。“装一次管几年”的做法已经彻底失效,必须上自动化续期。

动手前先用两条命令确认现状:

curl -sI https://example.com | head -n 5
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null 
  | openssl x509 -noout -subject -issuer -dates

看三处即可定位方向:subject 是否包含你的域名、issuer 是否为中间 CA(而不是根 CA)、notAfter 是否临近。如果 issuer 直接显示根证书机构名,说明只发出去服务器证书,链没拼。

证书准备:CSR 生成、证书链拼接与 PFX/JKS 格式转换

四种 Web 服务器对证书文件的要求不同,但源头只有一套:私钥 + 服务器证书 + 中间证书。建议在一台 Linux 机器上统一处理后分发。

1)生成私钥与 CSR,SAN 必须写全

openssl req -new -newkey rsa:2048 -nodes 
  -keyout example.com.key -out example.com.csr 
  -subj '/C=CN/ST=Beijing/L=Beijing/O=Example Ltd/CN=example.com' 
  -addext 'subjectAltName=DNS:example.com,DNS:www.example.com'

私钥泄露等于证书作废,务必 chmod 600 example.com.key。想降低握手开销可换 ECDSA:把 rsa:2048 换成 ecparam:prime256v1,但 Android 4.x、Windows XP 等老客户端会不兼容。

2)校验 CSR 内容与私钥是否匹配

openssl req -in example.com.csr -noout -text | grep -A1 'Subject Alternative Name'
openssl x509 -noout -modulus -in example.com.crt | openssl md5
openssl rsa  -noout -modulus -in example.com.key | openssl md5

两条 md5 必须一致,不一致说明拿错了私钥——这是“证书装了但不生效”的高频原因。

3)拼接完整证书链

cat example.com.crt intermediate.crt > fullchain.pem   # 顺序不能反
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt fullchain.pem

顺序是“服务器证书 → 中间证书 → 根证书(可省)”。顺序反了 Nginx 照样能启动,但部分客户端握手失败。

4)转成 IIS / Tomcat 需要的格式

openssl pkcs12 -export -out example.com.pfx 
  -inkey example.com.key -in fullchain.pem -passout pass:YourPass123
keytool -importkeystore -srckeystore example.com.pfx -srcstoretype PKCS12 
  -destkeystore example.com.jks -deststoretype JKS -deststorepass changeit

IIS 只认 .pfx;Tomcat 8.5 之后推荐 .p12(PKCS12),JKS 仅用于兼容老版本。转换时 -in 一定要用 fullchain,否则导进 Windows 后依旧缺中间证书。

Linux + Nginx:从零到 443 可访问

以 RHEL/CentOS 系为例,Debian/Ubuntu 把 yum 换成 apt、路径换成 /etc/nginx 即可。

sudo yum install -y nginx
sudo mkdir -p /etc/nginx/ssl
sudo cp example.com.key fullchain.pem chain.pem /etc/nginx/ssl/
sudo chmod 600 /etc/nginx/ssl/example.com.key
sudo chown -R root:root /etc/nginx/ssl

写入 /etc/nginx/conf.d/example.conf:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    http2 on;                 # nginx 1.25.1+;旧版本写 listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate     /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers off;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    resolver 223.5.5.5 8.8.8.8 valid=300s;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/nginx/ssl/chain.pem;

    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;

    root  /var/www/html;
    index index.html;
}

关键参数说明:ssl_certificate 必须指向 fullchain(含中间证书);ssl_trusted_certificate 供 OCSP Stapling 校验,指向纯中间证书 chain.pem,配合 resolver 才能实时拉取 OCSP 响应;ssl_session_tickets off 避免会话票据无法轮换带来的前向安全问题;HSTS 的 max-age 建议先用 300 秒验证,确认全站 HTTPS 无遗漏后再调到 31536000。

生效与放行:

sudo nginx -t
sudo systemctl reload nginx
sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload
# 云服务器还需在安全组放行 443/TCP

reload 不中断连接,只有改 listen 或替换 nginx 二进制才需要 restart。若系统开着 SELinux,证书放在非标准目录会读不到,执行 restorecon -Rv /etc/nginx/ssl 修正上下文。

Apache 与 Tomcat:配置文件写法与生效命令

Apache httpd 2.4:Debian 系先 sudo a2enmod ssl headers rewrite,RHEL 系 sudo yum install -y mod_ssl。

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html
    SSLEngine on
    SSLCertificateFile      /etc/httpd/ssl/fullchain.pem
    SSLCertificateKeyFile   /etc/httpd/ssl/example.com.key
    # 2.4.8 之前必须单独指定中间证书;新版本 fullchain 已含,可省略
    SSLCertificateChainFile /etc/httpd/ssl/chain.pem
    SSLProtocol             -all +TLSv1.2 +TLSv1.3
    SSLCipherSuite          ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
    SSLHonorCipherOrder     off
    Header always set Strict-Transport-Security 'max-age=31536000'
</VirtualHost>

校验与重载:sudo apachectl configtest 输出 Syntax OK 后执行 sudo systemctl reload httpd(Debian 为 apache2)。若 SSLCertificateFile 只写服务器证书又漏配 ChainFile,桌面浏览器可能正常,但微信内置浏览器、安卓 WebView 会直接白屏。

Tomcat 8.5/9/10:把 example.com.p12 放进 conf/,编辑 conf/server.xml:

<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="200" SSLEnabled="true" scheme="https" secure="true">
  <SSLHostConfig>
    <Certificate certificateKeystoreFile="conf/example.com.p12"
                 certificateKeystorePassword="YourPass123"
                 certificateKeystoreType="PKCS12"
                 type="RSA"/>
  </SSLHostConfig>
</Connector>

同时把 8080 连接器改成 redirectPort="443",并在 conf/web.xml 末尾加 <security-constraint> 强制跳转,或直接在 Tomcat 前挂 Nginx 做 TLS 终止(更常见)。改完必须重启:sudo systemctl restart tomcat,Tomcat 不会热加载 server.xml。

Java 环境常踩的坑是 certificateKeystoreFile 用相对路径时基准目录为 $CATALINA_BASE,不是当前 shell 目录,写绝对路径最稳。

Windows IIS:PFX 导入、SNI 绑定与 0x80070520 报错

1)导入证书:Win+R 输入 certlm.msc → “个人” → 右键“所有任务 → 导入”,选择 example.com.pfx,勾选“包括所有扩展属性”和“标记此密钥为可导出”(便于日后迁移)。中间证书要单独导入到“中间证书颁发机构 → 证书”存储区,否则部分客户端仍报链不完整。

PowerShell 一行导入:

Import-PfxCertificate -FilePath C:sslexample.com.pfx -CertStoreLocation Cert:LocalMachineMy `
  -Password (ConvertTo-SecureString -String 'YourPass123' -AsPlainText -Force)

2)绑定 443:IIS 管理器 → 站点 → 右侧“绑定” → 添加 → 类型 https、端口 443、SSL 证书选刚导入的那张、勾选“需要服务器名称指示(SNI)”。多站点共用 443 必须勾 SNI,否则保存时会提示“至少有一个其他站点使用同一 IP 和端口”。

命令行等价操作:

appcmd set site /site.name:"Default Web Site" /+bindings.[protocol='https',bindingInformation='*:443:example.com',sslFlags='1']
netsh http show sslcert

sslFlags=1 即启用 SNI;netsh http show sslcert 可确认 443 上实际挂的是哪个证书哈希。

3)HTTP 强制跳转:安装 URL Rewrite 模块后,在站点根目录 web.config 加规则:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Force HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions><add input="{HTTPS}" pattern="off" /></conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

常见报错:绑定时报 0x80070520(“尝试创建目录时出错”或“找不到指定对象”),九成是私钥权限问题——导入时未勾选可导出,或 IIS_IUSRS / NETWORK SERVICE 没有私钥读取权限。用 certlm.msc 找到证书 → 所有任务 → 管理私钥 → 添加 IIS_IUSRS 读取权限即可。提示“该证书不存在”则多半选错了存储区(当前用户存储 vs 本地计算机存储)。

四类服务器与证书格式对比表

同一张证书在不同服务器上的落地方式差异很大,下表可直接对照操作:

对比项 Nginx Apache httpd IIS Tomcat
证书文件 fullchain.pem + .key fullchain.pem + .key(+chain) .pfx .p12 / .jks
配置位置 /etc/nginx/conf.d/*.conf /etc/httpd/conf.d/ssl.conf IIS 管理器 → 绑定 conf/server.xml
语法检查 nginx -t apachectl configtest appcmd list site 看 catalina.out
生效方式 systemctl reload nginx systemctl reload httpd 保存绑定即生效 必须 restart
典型坑 缺中间证书、私钥权限 2.4.8 前漏配 ChainFile 私钥权限、未勾 SNI Alias 不是 key entry

证书文件格式对照:

扩展名 内容 典型用途
.key PEM 格式私钥 Nginx / Apache
.crt / .pem 服务器证书(Base64 文本) Nginx / Apache
chain / ca-bundle 中间证书 拼 fullchain、OCSP Stapling
.pfx / .p12 证书+私钥+链,带密码 IIS / Tomcat / Java
.jks Java KeyStore Tomcat 7 及更早版本
.csr 证书签名请求 向 CA 申请签发

选型建议:公网入口优先 Nginx,性能与配置灵活度最好;Windows/.NET 站点用 IIS;Tomcat 建议前面挂 Nginx 做 TLS 终止,把证书管理收敛到一处,避免每年续期要改四个地方。无论选哪种,都要同步放行安全组的 443/TCP,并在负载均衡层与源站两侧都部署证书,否则回源链路仍是明文。

常见错误排查:真实报错与对应处置

下面这些都是部署现场的原样输出,按关键字搜索即可定位:

  1. nginx: [emerg] cannot load certificate “/etc/nginx/ssl/fullchain.pem”: BIO_new_file() failed (SSL: error:80000002:system library::No such file or directory):路径写错或 SELinux 拦截。先 ls -l 确认文件存在,再 restorecon -Rv /etc/nginx/ssl,最后 getenforce 看是否 Enforcing。
  2. nginx: [emerg] PEM_read_bio_X509_AUX() failed (SSL: error:0480006C:PEM routines::no start line):文件内容不是 PEM,常见于把 .pfx 二进制直接改名成 .pem,或复制时混入 BOM。用 head -n 1 fullchain.pem 应看到 -----BEGIN CERTIFICATE-----。
  3. nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use):Apache 或另一个 nginx 实例占着 443。用 ss -lntp | grep :443 找进程,先 systemctl stop,不要直接 kill -9。
  4. curl: (60) SSL certificate problem: unable to get local issuer certificate:服务端链不完整。补齐 fullchain,或用 curl --cacert chain.pem 反向验证判断。
  5. AH02572: Failed to configure at least one certificate and key for example.com:443:Apache 的证书与私钥不匹配,回到第二节用 modulus 比对。
  6. java.io.IOException: Alias name [tomcat] does not identify a key entry:JKS 里只导入了证书没导入私钥,用 keytool -list -v 查看 Entry type 必须是 PrivateKeyEntry。
  7. Mixed Content: The page at ‘https://…’ was loaded over HTTPS, but requested an insecure resource ‘http://…’:页面硬编码了 http 的图片/JS/CSS。全站搜 http:// 改成 https 或协议相对路径,或加 Content-Security-Policy: upgrade-insecure-requests。
  8. 0x80070520:IIS 私钥权限问题,见上一节处理方式。

排查顺序建议:先过语法关(nginx -t / configtest),再用 openssl s_client 看服务端实际吐出的证书链,最后才怀疑浏览器缓存——大量“改了没生效”其实是 reload 没执行,或 CDN 节点未刷新。

效果验证:五步确认绿锁真正生效

1)验证证书链完整

openssl s_client -connect example.com:443 -servername example.com 
  -CAfile /etc/ssl/certs/ca-certificates.crt </dev/null 2>/dev/null | grep 'Verify return code'

输出 Verify return code: 0 (ok) 才算链完整;出现 unable to get local issuer certificate 说明中间证书没发出去。

2)验证域名与有效期

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName

3)验证协议与跳转

curl -sI http://example.com | grep -iE 'HTTP/|location'        # 期望 301 指向 https
openssl s_client -connect example.com:443 -tls1_1 </dev/null   # 期望握手失败
nmap --script ssl-enum-ciphers -p 443 example.com             # 只应出现 TLS1.2/1.3

4)到期监控(放进 crontab 每天跑并接告警)

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -checkend 2592000 || echo "证书将在30天内过期"

5)浏览器与在线评分:Chrome 无痕模式访问,地址栏应为闭合锁图标,点开证书显示“证书有效”;F12 控制台搜 Mixed Content 应为空;再用 SSL Labs(ssllabs.com/ssltest)跑一次,目标 A 及以上。拿到 A+ 通常需要:启用 TLS 1.3、HSTS 头存在且 max-age ≥ 31536000、无 RC4/3DES、OCSP Stapling 正常。

最后检查 CDN:站点走了 CDN 时,证书要在 CDN 控制台上传或开启其免费证书,源站证书只影响回源链路,两层都要覆盖,否则用户侧仍会看到旧证书或提示不安全。

常见问题

先点开地址栏图标看证书是否“有效”,再按 F12 查控制台有无 Mixed Content 报错。多数情况是页面残留 http:// 图片或接口,或证书链缺中间证书。补齐 fullchain 并把全站资源改为 https 后刷新即可恢复锁图标。

泛域名 *.example.com 覆盖 www.example.com、api.example.com 等一级子域,但不覆盖 example.com 本身,也不覆盖 a.b.example.com。裸域需单独加入 SAN,或改用多域名证书。签 CSR 时把两个都写进 subjectAltName 最省事。

公有 CA 单张 TLS 证书最长 398 天,Let’s Encrypt 固定 90 天;按 CA/B Forum 方案,2026-03-15 起降至 200 天,2027 年起 100 天,2029 年起 47 天。建议到期前 30 天自动续期并自动 reload 服务。

Nginx 执行 systemctl reload nginx、Apache 执行 systemctl reload httpd 即可热加载,不断连接;IIS 保存绑定后立即生效;Tomcat 修改 server.xml 必须重启进程。生产环境建议用双证书平滑切换,避免 reload 失败导致 443 不可用。

可以,靠 SNI 实现。Nginx 写多个 server 块各自 listen 443 ssl;IIS 绑定时勾选“需要服务器名称指示”;Apache 用多个 VirtualHost。注意不支持 SNI 的老客户端(XP、Android 2.x)只会拿到默认证书。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。