阿里云 ECS 双域名 Nginx + HTTPS 配置备忘
建站
0 字 / 约 0 分钟
阿里云 ECS 双域名 Nginx + HTTPS 配置备忘
适用场景:
- 阿里云 ECS 单实例
- 已备案双域名(示例 chiplayout.net & chiplayout.top)
- 同一 Web 目录
- 自动续期 HTTPS 证书
1. 前提检查
| 项目 | 要求 |
|---|---|
| 安全组 | 入方向 TCP 80 & 443 已放行 |
| 域名 | A 记录已解析到 ECS 公网 IP |
| 系统 | Ubuntu 22.04/20.04(CentOS 同理) |
2. 安装 Nginx
bash
sudo apt update && sudo apt install nginx -y
sudo systemctl enable --now nginx3. 一键申请 Let’s Encrypt 证书(双域名)
bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d chiplayout.net -d www.chiplayout.net \
-d chiplayout.top -d www.chiplayout.top按提示填写邮箱 → 同意协议 → 自动续期已内置 systemd timer
4. Nginx 虚拟主机配置(双域名 → 同一目录)
创建 /etc/nginx/sites-available/chiplayout:
nginx
# HTTP 统一 301
server {
listen 80;
server_name chiplayout.net www.chiplayout.net
chiplayout.top www.chiplayout.top;
return 301 https://$host$request_uri;
}
# HTTPS 共用
server {
listen 443 ssl http2;
server_name chiplayout.net www.chiplayout.net
chiplayout.top www.chiplayout.top;
ssl_certificate /etc/letsencrypt/live/chiplayout.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/chiplayout.net/privkey.pem;
include /etc/nginx/snippets/ssl-params.conf;
root /web/vitepress/dist; # ← 两个域名指向同一目录
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
# Twikoo 评论后端(可选)
location /twikoo/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}启用并加载:
bash
sudo ln -s /etc/nginx/sites-available/chiplayout /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx5. 通用 SSL 优化片段(可选)
bash
sudo tee /etc/nginx/snippets/ssl-params.conf <<EOF
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
EOF6. 自动续期测试
bash
sudo certbot renew --dry-run如无报错,系统将在证书剩余 30 天时自动续期。
7. 快速更新站点内容(后续)
bash
# 本地
npm run docs:build
tar -zcvf dist.tar.gz docs/.vitepress/dist
scp dist.tar.gz root@<ECS>:/tmp
# 服务器
cd /web/vitepress
tar -zxvf /tmp/dist.tar.gz --strip-components=28. 常见问题速查
| 现象 | 解决 |
|---|---|
| 403 Forbidden | chown -R www-data:www-data /web/vitepress/dist |
| 证书冲突警告 | 保留一份 80 跳转,其余删除 |
| 443 不通 | 安全组 + 本地防火墙确认放行 |
至此,双域名 HTTPS 统一目录部署完成,可直接访问: