Linux服务使用Nginx配置域名并使用certbot提供SSL

Linux服务使用Nginx配置域名并使用certbot提供SSL 这里博主提供一个通用办法首先我们必须有一个域名【这是我的域名】然后服务对应某个提供服务的端口【端口】# 更新系统包列表sudoaptupdate# 安装 Nginxsudoaptinstallnginx-y# 安装 Certbot 及其 Nginx 插件sudoaptinstallcertbot python3-certbot-nginx-y安装完成后查看nginx的状态sudosystemctl start nginxsudosystemctlenablenginx配置nginxcd/etc/nginx/conf.d/sudovim【这是我的域名】.conf然后放置配置文件# HTTP 服务器 —— 强制跳转 HTTPSserver{listen80;server_name 【这是我的域名】;return301https://$host$request_uri;}# HTTPS 服务器 —— 处理加密并转发到本地服务server{listen443ssl http2;server_name 【这是我的域名】;# 证书路径# ssl_certificate /etc/letsencrypt/live/【这是我的域名】/fullchain.pem;# ssl_certificate_key /etc/letsencrypt/live/【这是我的域名】/privkey.pem;# 安全性配置ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers HIGH:!aNULL:!MD5;# 反向代理到服务location /{proxy_pass http://127.0.0.1:【端口】;proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto$scheme;}}在这里我们就已经把SSL的路径写进来了后面就不用在大调整了sudocertbot certonly--nginx-d【这是我的域名】然后回到刚才的文件里/etc/nginx/conf.d/【这是我的域名】.conf把下面这两行注释取消ssl_certificate /etc/letsencrypt/live/【这是我的域名】/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/【这是我的域名】/privkey.pem;然后更新nginx即可sudonginx-tsudosystemctl reload nginx然后可以在其他机器上测试访问https://【这是我的域名】同样启动服务的时候只需要暴露本地的【端口】就行一切请求都由nginx在443端口做转发