SSL 证书安装指南

覆盖 Nginx、Apache、IIS、Tomcat、Node.js/Express 等主流环境,含 CSR 生成、证书链安装、HTTPS 重定向与常见问题排查。

CSR 生成 中间证书链 强制 HTTPS 故障排查
提示:安装前请确保您已获得以下文件或内容:
  • 私钥(private key,生成 CSR 时产生,必须妥善保管)
  • 域名证书(.crt 或 .pem)
  • 中间证书/根证书链(CA Bundle/chain)
1 生成 CSR 与私钥
OpenSSL(Linux/macOS/Windows with OpenSSL)
openssl req -new -newkey rsa:2048 -nodes -keyout your_domain.key -out your_domain.csr -subj "/C=CN/ST=Fujian/L=Xiamen/O=Your Company/OU=IT/CN=yourdomain.com"

若使用 ECC,可将 rsa:2048 更换为 ecparam -genkey -name prime256v1 流程;注意 CSR 的 CN 与购买域名一致。

IIS(Windows)

在 IIS 管理器 → 服务器 → 服务器证书 → 创建证书申请,填写组织与域名信息后保存 .csr。

2 Nginx 安装
合并证书链(如需要)
cat your_domain.crt intermediate_ca.crt > full_chain.crt
Nginx 配置示例
server { listen 443 ssl http2; server_name yourdomain.com; ssl_certificate /path/to/full_chain.crt; # 证书 + 中间证书链 ssl_certificate_key /path/to/your_domain.key; # 私钥 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; root /var/www/html; index index.html index.php; } server { listen 80; server_name yourdomain.com; return 301 https://$host$request_uri; # 强制 HTTPS }
3 Apache 安装
虚拟主机配置
<VirtualHost *:443> ServerName yourdomain.com DocumentRoot "/var/www/html" SSLEngine on SSLCertificateFile "/path/to/your_domain.crt" SSLCertificateKeyFile "/path/to/your_domain.key" SSLCertificateChainFile "/path/to/intermediate_ca.crt" Protocols h2 http/1.1 </VirtualHost>
HTTP → HTTPS 重定向
<VirtualHost *:80> ServerName yourdomain.com Redirect permanent / https://yourdomain.com/ </VirtualHost>
4 IIS 安装
导入证书

IIS 管理器 → 服务器 → 服务器证书 → 导入(选择 .pfx 如有;若为 .crt/.key,需先合并生成 .pfx)。

站点绑定

站点 → 绑定 → 添加:https,选择导入的证书,勾选 SNI(多站点同 IP)。

5 Tomcat 安装
生成 keystore(示例)
keytool -genkey -alias tomcat -keyalg RSA -keysize 2048 -keystore keystore.jks -validity 3650
server.xml 配置
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" keystoreFile="/path/to/keystore.jks" keystorePass="your_password" clientAuth="false" sslProtocol="TLS" />
6 Node.js / Express
HTTPS 服务示例
const fs = require('fs'); const https = require('https'); const express = require('express'); const app = express(); const options = { key: fs.readFileSync('/path/to/your_domain.key'), cert: fs.readFileSync('/path/to/your_domain.crt'), ca: fs.readFileSync('/path/to/intermediate_ca.crt'), }; app.get('/', (req, res) => res.send('Hello HTTPS!')); https.createServer(options, app).listen(443, () => { console.log('HTTPS server running on 443'); });
7 强制 HTTPS / 重定向
Nginx
server { listen 80; server_name yourdomain.com; return 301 https://$host$request_uri; }
Apache .htaccess
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
IIS(URL 重写)

安装 URL Rewrite,创建规则:若 {HTTPS} 不等于 on,则 301 重定向到 https://{HTTP_HOST}{REQUEST_URI}。

8 验证与测试
浏览器与命令行
curl -I https://yourdomain.com openssl s_client -connect yourdomain.com:443 -servername yourdomain.com -showcerts

检查是否存在"证书链不完整"、"域名不匹配"、"证书过期"等问题。

9 常见问题排查
浏览器提示"不安全"或没有小锁
通常由证书链不完整、域名不匹配、证书过期、混合内容(HTTPS 页面内引用 HTTP 资源)导致。请确认证书链完整、域名与证书一致、资源全部改为 HTTPS。
SNI 场景下某些用户无法访问
确认服务器与客户端支持 SNI。对极旧系统/浏览器,可采用独立 IP 或使用更兼容的证书与协议集合(如启用 RSA 与完整链)。
启用了 HTTPS 但部分接口仍走 HTTP
检查反向代理或前端代码是否硬编码 http://,在服务端增加强制跳转与 HSTS(谨慎配置,以防调试受限)。