欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Debian 12.0 上为 Nginx 配置 SSL/TLS 证书

Debian 12.0 上为 Nginx 配置 SSL/TLS 证书

2025/4/22 7:11:31 来源:https://blog.csdn.net/qq_43329216/article/details/144679993  浏览:    关键词:Debian 12.0 上为 Nginx 配置 SSL/TLS 证书

在 Debian 12.0 上为 Nginx 配置 SSL/TLS 证书,可以使用免费的 Let’s Encrypt 证书,通过工具 Certbot 自动完成证书获取和配置。以下是具体步骤:

  1. 安装 Certbot 和 Nginx 插件
    Certbot 是一个工具,可以从 Let’s Encrypt 获取证书并自动配置到 Nginx。
    运行以下命令安装 Certbot 和其 Nginx 插件:
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
  1. 确保域名指向服务器

在你的域名注册商控制面板中,将域名的 A 记录 和 CNAME 记录 指向你的服务器 IP 地址。
例如:

类型主机名
A@你的服务器 IP 地址

然后等待 DNS 解析生效。可以通过以下命令验证 DNS 解析是否正确:

ping your-domain.com

如果返回了你的服务器 IP,说明解析已经生效。

  1. 获取 SSL/TLS 证书

Certbot 会自动为你的域名获取 Let’s Encrypt 的免费证书并配置 Nginx。

执行以下命令:

sudo certbot --nginx -d your-domain.com -d www.your-domain.com
•	将 your-domain.com 替换为你的实际域名。
•	如果你有多个域名,可以用 -d 参数添加更多域名。

交互过程

Certbot 会提示你完成以下操作:
1. 输入邮箱地址,用于接收证书过期提醒。
2. 同意 Let’s Encrypt 服务条款。
3. 选择是否强制将 HTTP 流量重定向到 HTTPS(建议选择 2 强制重定向)。

Certbot 完成后,会看到如下提示:

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your-domain.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/your-domain.com/privkey.pem

这表示证书已经成功获取,并自动配置到了 Nginx。

  1. 检查 Nginx 配置

Certbot 会自动修改 Nginx 的站点配置文件,例如 /etc/nginx/sites-available/your-domain.com,并添加如下内容:

server {listen 443 ssl;listen [::]:443 ssl;server_name your-domain.com www.your-domain.com;ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers HIGH:!aNULL:!MD5;root /var/www/your-domain.com;index index.html index.htm;location / {try_files $uri $uri/ =404;}
}server {listen 80;listen [::]:80;server_name your-domain.com www.your-domain.com;return 301 https://$host$request_uri;
}
  1. 测试和重新加载 Nginx

测试 Nginx 配置是否正确:

sudo nginx -t

如果没有错误,重新加载 Nginx:

sudo systemctl reload nginx
  1. 验证 HTTPS 配置

在浏览器中访问 https://your-domain.com 和 https://www.your-domain.com,确认网站使用 HTTPS 并显示有效的锁标志。

你也可以通过以下命令验证证书:

openssl s_client -connect your-domain.com:443 -servername your-domain.com
  1. 自动续期证书

Let’s Encrypt 的证书有效期为 90 天,Certbot 会自动安排续期。

可以手动测试续期命令,确保没有问题:

sudo certbot renew --dry-run

如果没有错误,说明自动续期功能已配置好。

  1. 可选优化(提高安全性)

你可以在 Nginx 配置中添加一些额外设置来提高安全性:

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;# 禁用不安全的协议
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";

保存后,重新加载 Nginx:

sudo systemctl reload nginx

总结

1.	安装 Certbot 和插件。
2.	配置域名 DNS 解析。
3.	使用 Certbot 获取和配置证书:certbot --nginx -d your-domain.com -d www.your-domain.com
4.	测试配置并重新加载 Nginx。
5.	验证 HTTPS 是否正常工作。
6.	确保自动续期功能正常:certbot renew --dry-run。

这样,你的 Nginx 网站就成功绑定了 HTTPS 证书,并且会自动续期,保持安全!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词