nginx升级https实战(certbot工具)

nginx升级https实战(certbot工具)

经验文章nimo972025-06-29 8:39:454A+A-

在当今高度重视网络安全的时代,网站的加密传输已然变得举足轻重。随着HTTPS协议的广泛普及,SSL/TLS证书俨然成为网站不可或缺的标配。而Certbot与Let’s Encrypt于这一领域中扮演着至关重要的角色,二者之间存在着紧密且别具一格的关联。


Let’s Encrypt是一个免费、开放且自动化的证书颁发机构(CA)。Certbot则是Let’s Encrypt官方推荐的客户端工具。它是一个开源的软件,旨在简化从Let’s Encrypt获取和安装SSL/TLS证书的过程。Certbot的出现,让即使没有专业技术背景的用户也能够轻松地为自己的网站配置HTTPS。使用Certbot,用户可以通过简单的命令行操作来完成一系列复杂的任务。


下面我们一起来实操一下如何使用certbot,对基于nginx的服务器端进行https的升级:

进入服务器shell界面

# 安装 EPEL 仓库(若未安装)
sudo yum install epel-release -y

# 安装 Certbot 及 Nginx 插件(CentOS 7/8 通用)
sudo yum install certbot certbot-nginx -y 

使用certbot申请证书

sudo certbot --nginx -d yourdomain.com // yourdomain.com替换为自己的域名地址

此时,在服务器的
/etc/letsencrypt/live/yourdomain.com/ 目录下已生成相关的证书文件!!

修改nginx.conf配置,一般在/etc/nginx/目录下

server {
    listen 80;
    server_name yourdomain.com;
    return 301 https://$host$request_uri;  # 强制跳转 HTTPS
}

server {
    listen 443 ssl;
    server_name yourdomain.com;
    
    # Certbot 生成的证书路径
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    
    # 其他配置(如 Vue/Gin 代理)
    location / {
        root /var/www/vue-dist;
        try_files $uri $uri/ /index.html;
    }
}

检查nginx配置文件是否正确

sudo nginx -t
sudo nginx -s reload

测试域名:

浏览器访问:
<https://yourdomain.com>

大功告成!!!

点击这里复制本文地址 以上内容由nimo97整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

尼墨宝库 © All Rights Reserved.  蜀ICP备2024111239号-7