通过HTTPS访问子域名

【目标】

使用HTTPS方式访问子域名, 例如 https://fmt.infuq.com

【说明】

1.一台阿里云ECS服务器CentOS7部署了Nginx

2.使用腾讯云 DNSPod 进行域名解析配置

【实现方式】

使用 certbot 工具

【操作流程】

1.主账号登录腾讯云, 通过主账号创建一个子用户

2.给该子用户授权(4个权限)

3.创建密钥, 记住 SecretId 和 SecretKey

4.临时关闭Nginx服务(即关闭占用80和443端口的服务)

5.需要部署Python3.10环境, 这里选择使用 Docker Python3.10, 直接使用即可

bash 复制代码
docker pull registry.cn-hangzhou.aliyuncs.com/infuq/python:3.10

6.进入容器, 依次执行如下命令(关键操作)

bash 复制代码
6.1
sh-4.2#  yum install -y epel-release

6.2
sh-4.2#  pip3 install --upgrade pip

6.3
sh-4.2#  pip3 install setuptools_rust

6.4
sh-4.2#  pip3 install certbot

6.5
sh-4.2#  certbot --version    // 验证 certbot 是否安装成功
certbot 5.2.2

6.6
sh-4.2#  pip3 install certbot-dns-tencentcloud

6.7
sh-4.2#  certbot plugins      // 验证 dns-tencentcloud 是否安装成功

6.8
sh-4.2#  mkdir -p /etc/letsencrypt/tencentcloud

6.9
sh-4.2#  tee /etc/letsencrypt/tencentcloud/credentials.ini <<EOF
> dns_tencentcloud_secret_id = <腾讯云自己的SECRET_ID>
> dns_tencentcloud_secret_key = <腾讯云自己的SECRET_KEY>
> EOF

6.10
sh-4.2# chmod 600 /etc/letsencrypt/tencentcloud/credentials.ini 

6.11
sh-4.2# pip3 install --upgrade certifi

6.12
sh-4.2# export SSL_CERT_FILE=$(python -c "import certifi; print(certifi.where())")

6.13
sh-4.2# export REQUESTS_CA_BUNDLE=$SSL_CERT_FILE

6.14
sh-4.2# which certbot     // 查看certbot命令绝对位置
/usr/local/bin/python3.10/bin/certbot


6.15 生成密钥
sh-4.2# /usr/local/bin/python3.10/bin/certbot certonly \
-d "infuq.com" -d "*.infuq.com" \
-a dns-tencentcloud \
--dns-tencentcloud-credentials /etc/letsencrypt/tencentcloud/credentials.ini \
--dns-tencentcloud-propagation-seconds 60 \
--server https://acme-v02.api.letsencrypt.org/directory \
--agree-tos --non-interactive --preferred-challenges dns-01

输出内容
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for infuq.com and *.infuq.com
Waiting 60 seconds for DNS changes to propagate
Encountered exception during recovery: certbot_dns_tencentcloud.certbot_tencentcloud_plugins.APIException: {'Code': 'InvalidParameter.RecordIdInvalid', 'Message': '记录编号错误。'}

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/infuq.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/infuq.com/privkey.pem
This certificate expires on 2026-03-29.
These files will be updated when the certificate renews.

7.将生成的 fullchain.pem 和 privkey.pem 文件拷贝到Nginx所在的ECS服务器, 在nginx的conf文件里配置 ssl_certificate 和 ssl_certificate_key

powershell 复制代码
server {
    charset      utf-8;
    listen       443 ssl;
    server_name  fmt.infuq.com;

    ssl_certificate      /root/letsencrypt/fullchain.pem;
    ssl_certificate_key  /root/letsencrypt/privkey.pem;
    ssl_protocols        TLSv1.2 TLSv1.3;


    # 其他内容

}

server {
    charset      utf-8;
    listen       80;
    server_name  fmt.infuq.com;

    # http://fmt.infuq.com/ -> https://fmt.infuq.com/
    return 301 https://$host$request_uri;

}

8.重启Nginx

9.访问正常

相关推荐
riyue6667 小时前
封装 WebSocket 工具类
网络·vue.js·websocket·网络协议·v
成空的梦想9 小时前
ZLibrary反爬机制实战分析的技术文章大纲
网络协议·https·ssl
2501_915909069 小时前
不用越狱就看不到 iOS App 内部文件?使用 Keymob 查看和导出应用数据目录
android·ios·小程序·https·uni-app·iphone·webview
不做菜鸟的网工11 小时前
H3C IPv6 over IPv4隧道实验
网络协议
23.11 小时前
【网络】TCP与HTTP:网络通信的核心机制解析
网络·tcp/ip·http
独自破碎E11 小时前
【面试真题拆解】5秒内限10次HTTP接口访问,结合数据结构和算法说说你的思路
数据结构·http·面试
袁小皮皮不皮11 小时前
【HCIA】第三章TCP/IP协议栈中其他主要协议
运维·服务器·网络·网络协议·tcp/ip
小庄梦蝶12 小时前
Mixed Content: The page at ‘https://域名/‘ was loaded over HTTPS
网络协议·http·https
SVIP1115912 小时前
Vue3 WebSocket 封装通关指南:心跳 + 重连 + 全局状态管理,复制即用!
网络·websocket·网络协议
带娃的IT创业者12 小时前
Weclaw 混合通讯架构实战:HTTP+SSE+WebSocket的三元融合如何支撑起整个 AI 助手的实时对话?
websocket·http·fastapi·sse·流式响应·实时通讯·混合架构