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

这里博主提供一个通用办法,首先我们必须有一个域名:【这是我的域名】,然后服务对应某个提供服务的端口【端口】

bash 复制代码
# 更新系统包列表
sudo apt update

# 安装 Nginx
sudo apt install nginx -y

# 安装 Certbot 及其 Nginx 插件
sudo apt install certbot python3-certbot-nginx -y

安装完成后查看nginx的状态:

bash 复制代码
sudo systemctl start nginx
sudo systemctl enable nginx

配置nginx

bash 复制代码
cd /etc/nginx/conf.d/
sudo vim 【这是我的域名】.conf

然后放置配置文件:

bash 复制代码
# HTTP 服务器 ------ 强制跳转 HTTPS
server {
    listen 80;
    server_name 【这是我的域名】;
    return 301 https://$host$request_uri;
}

# HTTPS 服务器 ------ 处理加密并转发到本地服务
server {
    listen 443 ssl 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的路径写进来了,后面就不用在大调整了

bash 复制代码
sudo certbot certonly --nginx -d 【这是我的域名】

然后回到刚才的文件里/etc/nginx/conf.d/【这是我的域名】.conf,把下面这两行注释取消:

bash 复制代码
    ssl_certificate /etc/letsencrypt/live/【这是我的域名】/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/【这是我的域名】/privkey.pem;

然后更新nginx即可:

bash 复制代码
sudo nginx -t
sudo systemctl reload nginx

然后可以在其他机器上,测试访问https://【这是我的域名】,同样,启动服务的时候只需要暴露本地的【端口】就行,一切请求都由nginx在443端口做转发

相关推荐
CC城子1 小时前
EtherNet/IP I/O 丢包排查(linux系统)
linux·单片机·tcp/ip·ethernetip
新时代牛马1 小时前
Linux 信号处理完整篇:从sigaction、掩码到内核发送与可重入排障
linux·运维·信号处理
程序员梅雨2 小时前
Linux & Shell 实用干货
linux·运维·服务器·后端·面试·php
吴声子夜歌2 小时前
编写Shell脚本——自顶向下设计
linux·运维·shell
M78佐菲2 小时前
HTML学习笔记
linux·笔记·学习·tcp/ip·html
新时代牛马2 小时前
嵌入式 Linux 蓝牙框架完整篇:从 HCI、L2CAP 到BlueZ 用户态
linux·运维·服务器
滨哥GPT2 小时前
Codex新增文件上传后为什么小文件正常,大文件总失败?Multipart、Body Limit与Nginx限制排查
nginx·ai编程·文件上传·codex·multipart
Brilliantwxx3 小时前
【STM32】 初认识USART串口
linux·stm32·单片机·嵌入式硬件·架构
aningx3 小时前
使用自定义bash脚本预防oom卡死
linux
ONLYOFFICE3 小时前
ONLYOFFICE成为Linux操作系统 – Winux的预装办公套件
linux·运维·服务器