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端口做转发

相关推荐
枕星而眠13 小时前
Linux网络协议三部曲:从UDP/TCP到HTTP,一篇打通任督二核
linux·网络协议·udp
剑神一笑14 小时前
Linux curl 命令深度解析:从 HTTP 请求到网络调试实战
linux·网络·http
热爱Liunx的丘丘人14 小时前
搭建一个 Web + 数据库系统(Nginx+PHP+MySQL)
数据库·nginx·php
zh路西法14 小时前
【ROS一键编译脚本】基于colcon与catkin的辅助一键懒人脚本
linux·windows·bash
a珍爱上了a强14 小时前
__attribute__((constructor))
linux
lightgis14 小时前
使用工作站电脑
linux·电脑
z2023050814 小时前
RDMA之NVIDIA Zero Touch RoCE (ZTR),和RTT的应用(9)
linux·服务器·网络·人工智能·ai
code monkey.14 小时前
【Linux之旅】Linux TCP Socket 编程实战:从单连接到线程池,构建高并发服务端
linux·网络·tcp/ip
H Journey14 小时前
总结Linux下查看IP地址的相关命令
linux·运维·ip address