域名配置指南
本文档介绍如何为 Vibany 项目配置自定义域名和 SSL 证书。
Vercel 域名配置
1. 添加域名到 Vercel
- 登录 Vercel Dashboard
- 选择你的项目
- 进入 Settings → Domains
- 点击 "Add Domain"
- 输入域名(如
yourdomain.com或www.yourdomain.com)
2. 配置 DNS 记录
Vercel 会提示你配置以下 DNS 记录之一:
方式一:A 记录(根域名)
Type: A
Name: @
Value: 76.76.21.21
TTL: 600
方式二:CNAME 记录(子域名)
Type: CNAME
Name: www
Value: cname.vercel-dns.com
TTL: 600
3. 等待 DNS 传播
DNS 传播通常需要:
- 几分钟到几小时
- 最长 48 小时
Vercel 会自动:
- 验证域名所有权
- 申请和配置 SSL 证书
- 启用 HTTPS
4. 重定向配置
建议配置 www 到非 www 的重定向(或反之):
- 在 Domains 页面添加两个域名
- Vercel 会自动处理重定向
- 或者手动在项目中配置
next.config.js
VPS 域名配置
1. 购买域名
推荐使用以下域名注册商:
| 注册商 | 特点 | 官网 |
|---|---|---|
| Cloudflare Registrar | 成本价续费 | cloudflare.com |
| Namecheap | 性价比高 | namecheap.com |
| Google Domains | 简洁易用 | domains.google |
| 阿里云 | 中文支持 | wanwang.aliyun.com |
| 腾讯云 | 中文支持 | dnspod.cn |
2. 配置 DNS 记录
指向 VPS IP
Type: A
Name: @
Value: 你的服务器IP地址
TTL: 600
配置 www 子域名
Type: A
Name: www
Value: 你的服务器IP地址
TTL: 600
3. 配置 Nginx
创建 Nginx 配置文件:
sudo nano /etc/nginx/sites-available/vibany
添加配置:
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
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;
}
# 上传文件大小限制
client_max_body_size 50M;
}
启用配置:
sudo ln -s /etc/nginx/sites-available/vibany /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
4. 配置 SSL 证书(Let's Encrypt)
使用 Certbot 免费申请 SSL 证书:
# 安装 Certbot
sudo apt update
sudo apt install certbot python3-certbot-nginx
# 申请证书
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
# 自动续期测试
sudo certbot renew --dry-run
Certbot 会自动:
- 修改 Nginx 配置添加 HTTPS
- 设置证书自动续期
- 配置 HTTP 到 HTTPS 重定向
Cloudflare 代理配置
1. 添加域名到 Cloudflare
- 登录 Cloudflare Dashboard
- 点击 "Add Site"
- 输入域名
- 选择套餐(免费套餐即可)
- 按提示修改域名 NS 服务器
2. 配置 DNS 记录
Type: A
Name: @
Value: 服务器IP或76.76.21.21(Vercel)
Proxy status: Proxied(橙色云)
TTL: Auto
3. 开启 HTTPS
在 Cloudflare → SSL/TLS:
- SSL/TLS 加密模式:Full (strict)
- 始终使用 HTTPS:开启
- 自动 HTTPS 重写:开启
4. 性能优化
在 Cloudflare → Speed:
- Auto Minify:开启 HTML/CSS/JS
- Brotli:开启
- Rocket Loader:可选
在 Cloudflare → Caching:
- Caching Level:Standard
- Browser Cache TTL:1 天
多域名部署
主域名 + 子域名
示例配置:
主域名:vibany.com → 主站
子域名:
- vibany.com → 中文站
- admin.vibany.com → 管理后台
- api.vibany.com → API 接口
配置步骤
- 在域名管理后台添加 DNS 记录:
Type: A
Name: cn
Value: 服务器IP
Type: A
Name: admin
Value: 服务器IP
- 在项目中配置多域名支持:
// middleware.ts
export default clerkMiddleware((auth, req) => {
const host = req.headers.get('host');
// 不同子域名不同处理
if (host?.startsWith('admin.')) {
// 管理后台逻辑
}
});
- 在 Clerk 配置多域名:
在 Clerk Dashboard → Domains 中添加所有子域名。
故障排除
域名无法访问
检查清单:
- DNS 记录已配置并传播
- 服务器防火墙允许 80/443 端口
- Nginx/应用正在运行
- 域名已正确解析到服务器 IP
诊断命令:
# 检查 DNS 解析
dig yourdomain.com
nslookup yourdomain.com
# 检查端口连通性
curl -I http://yourdomain.com
curl -I https://yourdomain.com
# 检查服务器监听端口
sudo netstat -tlnp | grep :3000
sudo lsof -i :3000
SSL 证书问题
证书过期:
# 手动续期
sudo certbot renew
# 检查续期定时任务
sudo systemctl status certbot.timer
证书不匹配:
- 确保证书包含所有域名(主域名 + www)
- 重新申请证书包含所有子域名
HTTPS 重定向循环
原因:
- Cloudflare SSL 模式设置错误
- 服务器重定向配置冲突
解决方案:
- Cloudflare SSL/TLS 设置为 "Full (strict)"
- 禁用服务器的 HTTP 到 HTTPS 重定向(让 Cloudflare 处理)
最佳实践
1. 使用 www 还是非 www
推荐:使用非 www(裸域名)作为主域名,www 重定向到主域名。
原因:
- 更简洁
- 节省 4 个字符
- 现代浏览器隐藏 www
2. 强制 HTTPS
确保所有流量都通过 HTTPS:
# Nginx 配置
server {
listen 80;
server_name yourdomain.com;
return 301 https://$server_name$request_uri;
}
3. HSTS 头部
增强安全性:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
4. 域名备案(中国大陆)
如果使用中国大陆服务器:
- 必须完成 ICP 备案
- 在网站底部显示备案号
- 备案号链接到工信部网站
费用说明
| 项目 | 费用 | 说明 |
|---|---|---|
| 域名注册 | ¥10-100/年 | 根据后缀不同 |
| SSL 证书 | 免费 | Let's Encrypt |
| Cloudflare | 免费 | 免费套餐足够 |
| Vercel SSL | 免费 | 自动配置 |