免费 SSL 配置实战
90% 的独立站和 SaaS 用 Let's Encrypt 或 Cloudflare 免费 SSL 就够了。这一节给你两种方案的完整操作流程。
方案一:Cloudflare SSL(最简单)
如果你的 DNS 已经托管在 Cloudflare,开启 SSL 只需要 3 步。
步骤
Step 1:登录 Cloudflare → 选择你的域名 → 点击 SSL/TLS
Step 2:选择加密模式
| 模式 | 说明 | 推荐? |
|---|---|---|
| Off | 无 SSL | ❌ 不要用 |
| Flexible | 浏览器→Cloudflare 加密,Cloudflare→服务器不加密 | ⚠️ 不安全 |
| Full | 全程加密,服务器可以用自签名证书 | ✅ 可用 |
| Full (Strict) | 全程加密,服务器必须有有效证书 | ✅✅ 推荐 |
Step 3:开启 Always Use HTTPS(Edge Certificates → Always Use HTTPS → On)
Cloudflare 会自动签发、续期证书,你什么都不用做。
方案二:Let's Encrypt + Certbot(自己的服务器)
如果你的服务器是 VPS(Ubuntu/Debian),用 Certbot 自动申请和续期。
安装 Certbot
# Ubuntu/Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx
# 如果用 Apache
sudo apt install certbot python3-certbot-apache
申请证书
# Nginx 服务器
sudo certbot --nginx -d example.com -d www.example.com
# Apache 服务器
sudo certbot --apache -d example.com -d www.example.com
# 仅申请证书,不自动配置(手动模式)
sudo certbot certonly --standalone -d example.com
Certbot 会自动: 1. 验证域名所有权(需要域名 A 记录指向这台服务器) 2. 申请证书 3. 修改 Nginx/Apache 配置 4. 设置 cron 自动续期
验证自动续期
# 测试续期(不真正续期)
sudo certbot renew --dry-run
# 查看证书有效期
sudo certbot certificates
自动续期:Certbot 安装后会自动创建 cron job,每天检查证书有效期,到期前 30 天自动续期。
方案三:Shopify / Webflow / Ghost 托管平台
如果你用的是托管平台,SSL 是自动配置的:
| 平台 | SSL 设置方法 |
|---|---|
| Shopify | 后台 → 在线商店 → 域名 → 已连接域名自动获得 SSL |
| Webflow | 项目设置 → 自定义域名 → 自动 SSL |
| Ghost | 配置时自动 Let's Encrypt |
| GitHub Pages | 仓库设置 → Pages → 开启 Enforce HTTPS |
| Cloudflare Pages | 自动,无需配置 |
强制 HTTPS 跳转
申请了证书还不够,还要确保 HTTP 流量自动跳转到 HTTPS。
Nginx 配置
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 其他配置...
}
HSTS(HTTP Strict Transport Security)
HSTS 告诉浏览器:这个域名永远只用 HTTPS,不要尝试 HTTP。
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
注意:开启 HSTS 前确保 HTTPS 完全正常,因为 HSTS 很难撤销(max-age 期间浏览器都会强制 HTTPS)。
证书有效期检查
# 用 openssl 检查证书
echo | openssl s_client -servername example.com -connect example.com:443 2>/dev/null | openssl x509 -noout -dates
# 输出示例:
# notBefore=Mar 1 00:00:00 2026 GMT
# notAfter=May 30 00:00:00 2026 GMT
下一节:HTTPS 常见问题排查——混合内容错误、证书不信任、HSTS 陷阱怎么处理。