问题背景
在完成 Digital Garden 容器部署后,发现两个问题:
- 访问 https://notes.biyo.site 没有 HTTPS 证书
- 网站重定向到其他项目(baby后台管理)
问题分析
问题 1:缺少 Nginx 反向代理配置
现象:
- 容器正常运行在 8080 端口
- 域名 notes.biyo.site 无法访问
- 没有配置 SSL 证书
根本原因:
- 服务器上运行着系统级 Nginx,管理所有域名的反向代理
- 仅部署 Docker 容器不够,还需要配置 Nginx 反向代理
- 没有为 notes.biyo.site 创建站点配置文件
问题 2:端口冲突
现象:
- 访问 notes.biyo.site 重定向到 vault.biyo.site 的内容
根本原因:
- digital-garden 使用 8080 端口
- vault.biyo.site 也配置了反向代理到 127.0.0.1:8080
- 导致域名解析冲突
服务器端口分配情况:
# 查看端口占用
docker ps --format 'table {{.Names}}\t{{.Ports}}'
# 输出:
8081 - couple-insight-api
8082 - devflow-backend-prod
8083 - baby-log-api
8080 - vault.biyo.site (冲突!)解决方案
步骤 1:检查服务器环境
# SSH 登录服务器
ssh root@$SERVER_IP
# 查看所有容器端口
docker ps --format 'table {{.Names}}\t{{.Ports}}'
# 查看端口占用情况
netstat -tulpn | grep :80
# 查看 Nginx 配置目录
ls -la /etc/nginx/sites-enabled/
# 查看现有站点配置(参考)
cat /etc/nginx/sites-available/vault.biyo.site步骤 2:修改 Docker 端口
修改 docker-compose.yml:
services:
app:
build: .
container_name: digital-garden
ports:
- "8084:80" # 从 8080 改为 8084
restart: always提交并部署:
# 本地提交
git add docker-compose.yml
git commit -m "fix: change port to 8084 to avoid conflict"
git push
# 服务器部署
ssh root@$SERVER_IP "cd /root/digital-garden && git pull && docker compose down && docker compose up -d"步骤 3:创建 Nginx 配置
# 创建站点配置文件
ssh root@$SERVER_IP "cat > /etc/nginx/sites-available/notes.biyo.site << 'EOF'
server {
listen 80;
server_name notes.biyo.site;
location / {
proxy_pass http://127.0.0.1:8084;
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;
}
}
EOF"
# 验证配置文件创建成功
ssh root@$SERVER_IP "cat /etc/nginx/sites-available/notes.biyo.site"步骤 4:启用站点配置
# 创建软链接到 sites-enabled
ssh root@$SERVER_IP "ln -sf /etc/nginx/sites-available/notes.biyo.site /etc/nginx/sites-enabled/"
# 测试 Nginx 配置
ssh root@$SERVER_IP "nginx -t"
# 重新加载 Nginx
ssh root@$SERVER_IP "systemctl reload nginx"步骤 5:配置 HTTPS 证书
使用 Certbot 自动申请和配置 Let’s Encrypt 证书:
# 自动申请并配置 SSL 证书
ssh root@$SERVER_IP "certbot --nginx -d notes.biyo.site --non-interactive --agree-tos --email hubertbiyo@gmail.com"
# 预期输出:
# Successfully deployed certificate for notes.biyo.site
# Congratulations! You have successfully enabled HTTPS on https://notes.biyo.site步骤 6:验证部署
# 查看容器状态
ssh root@$SERVER_IP "docker ps | grep digital-garden"
# 验证端口监听
ssh root@$SERVER_IP "netstat -tulpn | grep 8084"
# 查看最终 Nginx 配置
ssh root@$SERVER_IP "cat /etc/nginx/sites-enabled/notes.biyo.site"
# 测试 HTTPS 访问
curl -I https://notes.biyo.site
# 测试 HTTP 自动重定向到 HTTPS
curl -I http://notes.biyo.siteCertbot 自动配置详解
Certbot 会自动修改 Nginx 配置文件,添加:
server {
server_name notes.biyo.site;
location / {
proxy_pass http://127.0.0.1:8084;
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;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/notes.biyo.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/notes.biyo.site/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
# HTTP 自动重定向到 HTTPS
server {
if ($host = notes.biyo.site) {
return 301 https://$host$request_uri;
}
listen 80;
server_name notes.biyo.site;
return 404;
}常用命令速查
Nginx 管理
# 测试配置文件语法
nginx -t
# 重新加载配置(不中断服务)
systemctl reload nginx
# 重启 Nginx
systemctl restart nginx
# 查看 Nginx 状态
systemctl status nginx
# 查看 Nginx 错误日志
tail -f /var/log/nginx/error.log
# 查看站点访问日志
tail -f /var/log/nginx/access.log证书管理
# 查看所有证书
certbot certificates
# 手动续期证书
certbot renew
# 测试续期(不实际执行)
certbot renew --dry-run
# 删除证书
certbot delete --cert-name notes.biyo.site
# 为新域名添加证书
certbot --nginx -d newdomain.com --non-interactive --agree-tos --email your@email.com端口检查
# 查看所有监听端口
netstat -tulpn | grep LISTEN
# 查看特定端口
netstat -tulpn | grep :8084
# 查看进程占用端口
lsof -i :8084
# 查看所有 Docker 容器端口映射
docker ps --format 'table {{.Names}}\t{{.Ports}}'Docker 容器管理
# 查看容器日志
docker logs digital-garden
# 实时查看日志
docker logs -f digital-garden
# 查看容器详细信息
docker inspect digital-garden
# 进入容器
docker exec -it digital-garden sh
# 查看容器资源使用
docker stats digital-garden
# 重启容器
docker restart digital-garden完整部署流程总结
新域名 HTTPS 部署清单
-
修改 docker-compose.yml 端口
ports: - "8084:80" # 选择未使用的端口 -
部署容器
git add docker-compose.yml git commit -m "fix: change port to 8084" git push ssh root@server "cd /root/project && git pull && docker compose up -d" -
创建 Nginx 配置
cat > /etc/nginx/sites-available/domain.com << 'EOF' server { listen 80; server_name domain.com; location / { proxy_pass http://127.0.0.1:PORT; 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; } } EOF -
启用站点
ln -sf /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/ nginx -t systemctl reload nginx -
配置 HTTPS
certbot --nginx -d domain.com --non-interactive --agree-tos --email your@email.com -
验证
curl -I https://domain.com docker ps | grep container-name
端口分配规范
为避免端口冲突,建议统一管理端口分配:
| 端口范围 | 用途 | 已使用 |
|---|---|---|
| 8080-8089 | Web 应用 | 8081, 8082, 8083, 8084 |
| 9000-9009 | MinIO 存储 | 9002, 9003, 9004, 9005 |
| 5430-5439 | PostgreSQL | 5433 |
| 3306-3316 | MySQL | - |
| 6379-6389 | Redis | - |
当前端口分配表
# Web 应用
8081 - couple-insight-api
8082 - devflow-backend-prod
8083 - baby-log-api
8084 - digital-garden
# MinIO 对象存储
9002 - devflow-minio (API)
9003 - devflow-minio (Console)
9004 - baby-log-minio (API)
9005 - baby-log-minio (Console)
# 数据库
5433 - couple-insight-db (PostgreSQL)故障排查
问题:域名无法访问
# 1. 检查容器是否运行
docker ps | grep container-name
# 2. 检查端口是否监听
netstat -tulpn | grep :PORT
# 3. 检查 Nginx 配置
nginx -t
cat /etc/nginx/sites-enabled/domain.com
# 4. 检查 Nginx 日志
tail -f /var/log/nginx/error.log
# 5. 测试本地访问
curl -I http://localhost:PORT问题:HTTPS 证书错误
# 1. 查看证书状态
certbot certificates
# 2. 检查证书文件
ls -la /etc/letsencrypt/live/domain.com/
# 3. 重新申请证书
certbot delete --cert-name domain.com
certbot --nginx -d domain.com
# 4. 测试证书续期
certbot renew --dry-run问题:端口冲突
# 1. 查看端口占用
netstat -tulpn | grep :PORT
lsof -i :PORT
# 2. 查找使用该端口的容器
docker ps --format 'table {{.Names}}\t{{.Ports}}' | grep PORT
# 3. 修改 docker-compose.yml 使用新端口
# 4. 更新 Nginx 配置中的 proxy_pass
# 5. 重启服务问题:HTTP 没有重定向到 HTTPS
# 检查 Nginx 配置中是否有重定向规则
cat /etc/nginx/sites-enabled/domain.com | grep "return 301"
# 如果没有,手动添加:
server {
listen 80;
server_name domain.com;
return 301 https://$host$request_uri;
}安全建议
Nginx 安全配置
server {
# ... 其他配置 ...
# 隐藏 Nginx 版本号
server_tokens off;
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN" always;
# XSS 防护
add_header X-XSS-Protection "1; mode=block" always;
# 防止 MIME 类型嗅探
add_header X-Content-Type-Options "nosniff" always;
# HSTS(强制 HTTPS)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}SSL 安全配置
# 测试 SSL 配置强度
# 访问:https://www.ssllabs.com/ssltest/analyze.html?d=notes.biyo.site
# Certbot 默认配置已经很安全,包含:
# - TLS 1.2 和 1.3
# - 强加密套件
# - OCSP Stapling
# - 2048位 DH 参数自动化脚本
一键部署新站点
创建 deploy-new-site.sh:
#!/bin/bash
DOMAIN=$1
PORT=$2
PROJECT_PATH=$3
EMAIL="hubertbiyo@gmail.com"
if [ -z "$DOMAIN" ] || [ -z "$PORT" ] || [ -z "$PROJECT_PATH" ]; then
echo "Usage: $0 <domain> <port> <project_path>"
echo "Example: $0 notes.biyo.site 8084 /root/digital-garden"
exit 1
fi
echo "=== Creating Nginx config for $DOMAIN ==="
cat > /etc/nginx/sites-available/$DOMAIN << EOF
server {
listen 80;
server_name $DOMAIN;
location / {
proxy_pass http://127.0.0.1:$PORT;
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;
}
}
EOF
echo "=== Enabling site ==="
ln -sf /etc/nginx/sites-available/$DOMAIN /etc/nginx/sites-enabled/
echo "=== Testing Nginx config ==="
nginx -t || exit 1
echo "=== Reloading Nginx ==="
systemctl reload nginx
echo "=== Setting up HTTPS ==="
certbot --nginx -d $DOMAIN --non-interactive --agree-tos --email $EMAIL
echo "=== Deployment complete! ==="
echo "Site URL: https://$DOMAIN"使用方法:
chmod +x deploy-new-site.sh
./deploy-new-site.sh notes.biyo.site 8084 /root/digital-garden监控和维护
证书到期监控
# Certbot 自动续期任务(已由 certbot 安装时配置)
# 查看 cron 任务
cat /etc/cron.d/certbot
# 手动测试续期
certbot renew --dry-runNginx 日志分析
# 访问量统计
cat /var/log/nginx/access.log | wc -l
# 查看最近访问
tail -n 100 /var/log/nginx/access.log
# 统计 IP 访问次数
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
# 统计 URL 访问次数
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
# 统计状态码
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn相关文档
更新记录
- 2026-01-02: 首次创建,记录 HTTPS 配置和端口冲突问题解决方案