网站域名配置开发记录
项目概述
本文档记录了为 WaitingToDo 项目配置域名 waitingtodo.cn
的完整过程,包括前端、后端、存储服务和反向代理的配置。
项目信息:
- 项目名称:WaitingToDo
- 域名:waitingtodo.cn
- 服务器IP:101.34.246.32
- 项目类型:前后端分离的 Web 应用
技术架构
1 2 3 4 5 6 7 8
| 用户浏览器 ↓ HTTPS (443) Nginx 反向代理 ↓ ┌─────────────────┬─────────────────┬─────────────────┐ │ Vue.js 前端 │ Go API 后端 │ MinIO 存储服务 │ │ (端口: 7070) │ (端口: 8080) │ (端口: 9000) │ └─────────────────┴─────────────────┴─────────────────┘
|
技术栈:
- 前端: Vue.js + Vite
- 后端: Go + Gin 框架
- 存储: MinIO 对象存储
- 反向代理: Nginx
- SSL: Let’s Encrypt 证书
域名配置流程
1. 域名申请与 DNS 配置
1.1 域名注册
- 在域名注册商处注册
waitingtodo.cn
- 配置域名解析,将 A 记录指向服务器 IP
101.34.246.32
1.2 DNS 记录配置
1 2 3
| 类型 主机记录 记录值 A @ 101.34.246.32 A www 101.34.246.32
|
2. SSL 证书申请与配置
2.1 使用 Certbot 申请免费证书
1 2 3 4 5
| sudo yum install certbot python3-certbot-nginx
sudo certbot --nginx -d waitingtodo.cn -d www.waitingtodo.cn
|
2.2 证书自动续期
1 2 3 4
| sudo crontab -e
0 12 * * * /usr/bin/certbot renew --quiet
|
2.3 申请腾讯云的免费 SSL 证书
- 登录腾讯云控制台,进入 SSL 证书服务
- 申请免费 DV 证书,填写域名
waitingtodo.cn
- 下载证书文件,包含
.pem
和 .key
文件
- 将证书文件上传到服务器
/etc/nginx/ssl/
目录 或/etc/ssl/certs/
目录
- 修改 Nginx 配置,使用腾讯云证书,指明证书路径
3. Nginx 反向代理配置
3.1 主配置文件结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include /etc/nginx/conf.d/*.conf; }
|
3.2 HTTPS 服务器配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| server { listen 443 ssl http2; server_name waitingtodo.cn www.waitingtodo.cn; ssl_certificate /etc/nginx/ssl/waitingtodo.cn.pem; ssl_certificate_key /etc/nginx/ssl/waitingtodo.cn.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; location / { root /var/www/waitingtodo/dist; try_files $uri $uri/ /index.html; index index.html; } location /api/ { proxy_pass http://127.0.0.1:7070; 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; proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; } location /images/ { proxy_pass http://101.34.246.32:9000/images/; 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; proxy_set_header X-Amz-Content-Sha256 $http_x_amz_content_sha256; proxy_set_header X-Amz-Date $http_x_amz_date; proxy_set_header Authorization $http_authorization; expires 30d; add_header Cache-Control "public, immutable"; } location /files/ { proxy_pass http://101.34.246.32:9000/files/; 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; add_header Content-Disposition 'attachment'; add_header X-Content-Type-Options nosniff; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; }
|
3.3 HTTP 到 HTTPS 重定向
1 2 3 4 5
| server { listen 80; server_name waitingtodo.cn www.waitingtodo.cn; return 301 https://$server_name$request_uri; }
|
4. 前端配置
4.1 环境变量配置
开发环境 - front/vue/.env.development
:
1 2 3
| # 开发环境直接访问 MinIO VITE_API_BASE_URL=http://localhost:7070/api VITE_PIC_BASE_URL=http://101.34.246.32:9000
|
生产环境 - front/vue/.env.production
:
1 2 3
| # 生产环境通过域名访问 VITE_API_BASE_URL=https://waitingtodo.cn/api VITE_PIC_BASE_URL=https://waitingtodo.cn
|
遇到的问题及解决方案
问题1: 配置域名后 MinIO 存储服务无法访问
现象:
- 配置域名后,图片等静态资源无法正常加载
- 浏览器控制台出现跨域错误
- 图片链接返回 403 或 404 错误
原因分析:
- 前端直接访问 MinIO 服务会遇到跨域问题
- HTTPS 页面无法加载 HTTP 资源(混合内容问题)
- MinIO 服务没有配置正确的 CORS 策略
- 防火墙可能阻止了对 MinIO 端口的直接访问
解决方案:
通过 Nginx 反向代理统一处理所有请求
1 2 3 4 5 6 7 8
| location /images/ { proxy_pass http://127.0.0.1:9000/images/; 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; }
|
优势:
- 解决跨域问题
- 统一 HTTPS 访问
- 隐藏内部服务架构
- 便于缓存和安全控制
问题2: 图片路径中出现 “undefined”
现象:
访问路径变成 waitingtodo.cn/undefined/images/xxx
原因分析:
生产环境中 VITE_PIC_BASE_URL
环境变量被注释或未正确设置,导致前端获取到 undefined
解决方案:
- 检查
.env.production
文件
- 确保环境变量正确设置
- 重新构建前端项目
1 2
| # .env.production VITE_PIC_BASE_URL=https://waitingtodo.cn
|
问题3: SSL 证书配置问题
现象:
解决方案:
- 确保证书文件路径正确
- 检查证书是否过期
- 验证域名是否匹配
1 2 3 4 5
| openssl x509 -in /etc/nginx/ssl/waitingtodo.cn.pem -text -noout
nginx -t
|
问题4: API 请求 404 错误
现象:
前端 API 请求返回 404
原因:
Nginx 代理配置中路径匹配问题
解决方案:
确保 API 路径正确配置
1 2 3 4 5 6
| location /api/ { proxy_pass http://127.0.0.1:7070/; proxy_pass http://127.0.0.1:7070/api/; }
|
监控与维护
1. 日志监控
1 2 3 4 5 6 7 8
| tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
journalctl -u waitingtodo-api -f
|
2. 健康检查
1 2 3 4 5 6 7
| sudo systemctl status nginx sudo systemctl status waitingtodo-api
netstat -tlnp | grep :443 netstat -tlnp | grep :7070
|
3. 证书续期监控
1 2 3 4 5
| certbot certificates
sudo certbot renew --dry-run
|
安全配置
1. 防火墙设置
1 2 3 4 5
| sudo ufw allow 22 sudo ufw allow 80 sudo ufw allow 443 sudo ufw enable
|
2. Nginx 安全头部
1 2 3 4 5 6
| add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header Referrer-Policy "strict-origin-when-cross-origin";
|
3. 限流配置
1 2 3 4 5 6 7 8 9 10 11
| http { limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s; server { location /api/ { limit_req zone=api burst=20 nodelay; proxy_pass http://127.0.0.1:7070; } } }
|
总结
通过本次域名配置实践,我们成功实现了:
- 统一的访问入口: 所有服务通过
waitingtodo.cn
域名访问
- 安全的 HTTPS 连接: 使用 SSL 证书加密传输
- 高效的反向代理: Nginx 统一处理请求分发
- 灵活的环境配置: 开发和生产环境分离
- 完善的监控体系: 日志、健康检查、性能监控
关键经验总结
- 架构设计: 采用反向代理模式,统一入口,便于管理和扩展
- 安全优先: HTTPS、安全头部、防火墙、限流等多层防护
- 环境分离: 开发、测试、生产环境配置分离,避免混乱
- 监控完善: 日志、监控、告警体系,及时发现和解决问题
- 文档记录: 详细记录配置过程,便于后续维护和问题排查
后续优化方向
- CDN 接入: 提升静态资源加载速度
- 负载均衡: 支持多实例部署
- 自动化运维: CI/CD 流水线,自动化测试和部署
- 性能监控: APM 工具,深入了解应用性能
通过这次完整的域名配置实践,不仅解决了当前的技术需求,也为项目的后续发展奠定了坚实的基础。