Nginx SSL 部署封面 — TLS 1.3、AES-256、SHA-256 加密协议示意图

Nginx 快速部署 SSL 证书实现 HTTPS 访问

文章目录

给网站加 HTTPS 这事儿,说难不难,说简单也不简单。之前我帮朋友部署了好几台服务器,每次都要翻文档,索性今天把完整的流程整理一下,以后直接复制粘贴。

为什么要用 HTTPS?#

2026 年了,HTTP 网站已经被浏览器标记为「不安全」,很多功能都会受限:

  • 🚫 浏览器地址栏会显示灰色感叹号
  • 🚫 无法使用 Service Worker、PWA 等现代 Web 功能
  • 🚫 微信、支付宝等第三方平台要求必须 HTTPS
  • 📈 SEO 加分,Google/百度都明确表示 HTTPS 是排名因素
  • 🔒 加密传输,保护用户隐私数据

而且现在证书都是免费的,不用白不用。

证书类型选择#

常见的证书类型#

表格:类型、价格、验证方式 对比表
类型 价格 验证方式 适用场景
DV SSL 免费 域名验证 个人博客、小网站
OV SSL ¥500+/年 企业验证 企业官网
EV SSL ¥2000+/年 严格企业验证 金融、电商

Let’s Encrypt 是免费的 DV SSL 证书,由 Mozilla、Cisco、Chrome 等大厂联合发起,值得信赖。

Let’s Encrypt 的限制#

  • 证书有效期 90 天(需要自动续期)
  • 单域名证书最多一次申请 100 个子域名
  • 有速率限制(每小时 50 个域名)

Let’s Encrypt + Certbot 自动续期#

我推荐用 Let’s Encrypt 的免费证书,配合 Certbot 工具,可以实现自动续期,不用担心证书过期的问题。

环境要求#

  • Ubuntu 20.04 / 22.04 或 Debian 11+
  • 已安装 Nginx
  • 拥有域名且 DNS 已解析到服务器 IP
  • SSH 访问服务器

第一步:安装 Certbot#

1
2
3
4
5
6
7
# Ubuntu / Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx

# 验证安装
certbot --version
# 输出:certbot 2.6.0

💡 如果你的系统版本较老,可能需要先添加 Certbot 仓库:

1
2
3
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install certbot python3-certbot-nginx

第二步:确认 Nginx 配置#

在申请证书之前,确保你的域名已经正确解析,并且 Nginx 已经配置好了站点:

1
2
3
4
5
6
7
8
# 检查 Nginx 配置语法
sudo nginx -t
# 输出:nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
#       nginx: configuration file /etc/nginx/nginx.conf test is successful

# 确保站点已启用
ls -la /etc/nginx/sites-enabled/
# 应该看到你的站点配置文件

第三步:申请证书#

1
2
3
4
5
# 单域名
sudo certbot --nginx -d example.com -d www.example.com

# 多域名(一次申请)
sudo certbot --nginx -d example.com -d www.example.com -d blog.example.com

执行后会提示输入邮箱(用于过期通知)和同意协议:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for emergency renewal and security notices)
 (Enter 'c' to cancel): your@email.com

Please read the Terms of Service at...
(A)gree/(C)ancel: A

Would you be willing to share your email address with the Electronic
Frontier Foundation? (Y)es/(N)o: N

Requesting a certificate for example.com and www.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem

第四步:Certbot 自动修改的 Nginx 配置#

Certbot 会自动修改 Nginx 配置,添加 HTTPS 相关设置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
server {
    server_name example.com www.example.com;

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

# HTTP 自动跳转到 HTTPS
server {
    server_name example.com www.example.com;
    listen 80;
    return 301 https://$host$request_uri;
}

第五步:验证配置并重启#

1
2
3
4
5
6
7
# 测试 Nginx 配置
sudo nginx -t

# 重载 Nginx
sudo systemctl reload nginx

# 访问 https://example.com 测试

手动配置 SSL(可选)#

如果 Certbot 自动配置不符合你的需求,可以手动配置。

基础 SSL 配置#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server {
    listen 443 ssl http2;
    server_name example.com;

    # SSL 证书
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # SSL 安全设置
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # 现代加密套件(TLS 1.2 和 1.3)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # HSTS(强制 HTTPS)
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    # 其他安全头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # 网站目录
    root /var/www/example.com;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

自动续期配置#

Let’s Encrypt 证书有效期 90 天,Certbot 默认每天检查两次,快过期时会自动续期。

查看定时任务#

1
2
3
4
# 查看 certbot 定时器
sudo systemctl status certbot.timer
# ● certbot.timer - Run certbot twice daily
#   Loaded: loaded (/lib/systemd/system/certbot.timer; enabled)

测试续期流程#

1
2
3
4
5
6
7
# 测试续期流程(不会真正执行)
sudo certbot renew --dry-run

# 输出示例:
# Processing /etc/letsencrypt/renewal/example.com.conf
# The following certs are not due for renewal yet:
#   /etc/letsencrypt/live/example.com/fullchain.pem expires on 2026-04-20

手动续期(如果测试失败)#

1
2
3
4
5
# 强制续期
sudo certbot renew --force-renewal

# 查看详细日志
sudo certbot renew --verbose

续期后重启 Nginx#

Certbot 续期成功后需要 reload Nginx。创建 hook 脚本:

1
2
3
4
5
6
7
# 创建 reload 脚本
sudo tee /etc/letsencrypt/renewal-hooks/post/reload-nginx.sh > /dev/null << 'EOF'
#!/bin/bash
systemctl reload nginx
EOF

sudo chmod +x /etc/letsencrypt/renewal-hooks/post/reload-nginx.sh

SSL 安全等级优化到 A+#

部署完成后,可以用 SSL Labs 测试,得到 A+ 是目标。

我的优化配置#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 完整的 SSL 安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;

# OCSP Stapling(加快验证速度)
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

# HSTS( Strict Transport Security)
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

常见评分问题#

表格:问题、解决方案 对比表
问题 解决方案
不支持 TLS 1.3 更新 OpenSSL 到 1.1.1+
证书链不完整 确保 include 了 fullchain.pem
使用了 RC4 等旧算法 更新 ssl_ciphers
没有启用 OSCP Stapling 添加 ssl_stapling 配置

HTTPS 安全原理#

简单科普一下 HTTPS 的工作流程:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
浏览器                          服务器
  │                               │
  │──── TLS 握手开始 ────────────▶│
  │                               │
  │◀──── 发送证书(公钥)──────────│
  │                               │
  │    验证证书有效性              │
  │    生成随机对称密钥            │
  │                               │
  │──── 用公钥加密发送密钥 ───────▶│
  │                               │
  │◀──── 用私钥解密得到密钥 ───────│
  │                               │
  │    双方都有对称密钥了          │
  │                               │
  │════ 加密通信开始 ══════════════│
  │                               │

关键点:

  1. 证书:证明服务器身份,由可信 CA 签发
  2. 非对称加密:用于握手阶段交换密钥
  3. 对称加密:正式通信使用,效率高

常见问题#

Q: 证书续期失败怎么办?#

检查清单:

  1. 域名是否到期或 DNS 是否生效
  2. Nginx 配置是否正确
  3. 防火墙是否开放了 80 和 443 端口
1
2
3
4
# 排查步骤
ping example.com
curl -I http://example.com
sudo certbot renew --verbose

Q: Nginx 配置测试失败?#

先用 nginx -t 检查语法,配合 2>&1 | grep -i error 定位具体行号:

1
2
3
4
5
6
7
8
# 测试配置语法
sudo nginx -t

# 查看错误信息
sudo nginx -t -c /etc/nginx/nginx.conf

# 如果配置错误,查看具体行号
sudo nginx -t 2>&1 | grep -i error

Q: 多个域名怎么管理?#

certbot 提供了 certificates 列出所有证书,delete 按证书名删除:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 查看所有证书
sudo certbot certificates

# 输出示例:
# Certificate Name: example.com
#   Serial Number: xxx
#   Key Types: RSA 2048
#   Expiry Date: 2026-04-20 12:00:00+00:00 (VALID)
#   Certificate Paths: /etc/letsencrypt/live/example.com/fullchain.pem

# 删除不需要的证书
sudo certbot delete --cert-name example.com

Q: 泛域名证书怎么申请?#

泛域名(*.example.com)必须用 DNS-01 验证,HTTP 验证不通过:

1
2
# 泛域名需要 DNS 验证
sudo certbot --manual --preferred-challenges dns -d "example.com" -d "*.example.com"

Q: HTTP 页面有资源仍使用 HTTP?#

浏览器控制台会报 “Mixed Content” 错误。需要把页面中所有 HTTP 资源改成 HTTPS:

1
2
# 批量替换数据库中的 HTTP 链接(WordPress 示例)
wp search-replace 'http://example.com' 'https://example.com' --all-tables

Q: 想禁用 HTTP 访问?#

如果你的网站必须全 HTTPS,可以完全关闭 HTTP:

1
2
3
4
5
server {
    listen 80 default_server;
    server_name _;
    return 444;  # 直接关闭连接,不返回任何响应
}

证书管理命令速查#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 申请证书
sudo certbot --nginx -d example.com -d www.example.com

# 测试续期
sudo certbot renew --dry-run

# 手动续期所有证书
sudo certbot renew

# 查看证书列表
sudo certbot certificates

# 删除证书
sudo certbot delete --cert-name example.com

# 吊销证书(如果域名被盗)
sudo certbot revoke --cert-path /etc/letsencrypt/live/example.com/fullchain.pem

使用工具推荐#

总结#

整个过程其实就四步:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 1. 安装
apt install certbot python3-certbot-nginx

# 2. 申请(自动配置 Nginx)
certbot --nginx -d example.com

# 3. 验证
nginx -t && systemctl reload nginx

# 4. 测试自动续期
certbot renew --dry-run

免费、简单、安全,何乐而不为?


有问题欢迎留言 👇

加入讨论

当前评论以 Telegram 私信为主渠道 — 点击下方按钮直接给我留言(文章链接与标题已自动填好)。我会在 几分钟到几小时内亲自回复每一条留言,也欢迎在 关于页 查看更多联系方式

在 Telegram 留言 @losefrank

文章链接已自动填到 Telegram 输入框,直接写留言即可