diff --git a/modify.md b/modify.md index 5903655c..cbd1a2ab 100644 --- a/modify.md +++ b/modify.md @@ -1,3 +1,55 @@ +## 2025-11-21 - HTTPS 部署配置:创建 Docker Compose HTTPS 部署结构 + +### 修改内容 +- **创建目录结构**: + - 创建 `nginx/` 目录用于存放 Nginx 配置文件 + - 创建 `ssl/` 目录用于存放 SSL 证书文件 + - 在 `ssl/` 目录添加 `.gitignore` 防止证书文件被提交到版本控制 + +- **Nginx 配置**: + - 新增 `nginx/nginx.conf` - Nginx 反向代理配置文件 + - 配置 HTTP 到 HTTPS 自动重定向 + - 配置 SSL/TLS 安全设置(TLSv1.2, TLSv1.3) + - 配置安全响应头(HSTS, X-Frame-Options 等) + - 配置反向代理到 marketing-site 服务 + - 配置静态文件缓存优化 + +- **Docker Compose 配置**: + - **保留原有 HTTP 配置**:`docker-compose.yml` 恢复为原始 HTTP 配置 + - marketing-site 服务直接映射端口 3000:3000 + - 适用于开发环境或不需要 HTTPS 的场景 + - **新增 HTTPS 配置**:`docker-compose.https.yml` - 独立的 HTTPS 配置文件 + - marketing-site 服务改为仅暴露内部端口(不直接映射到主机) + - 新增 nginx 服务,监听 80 和 443 端口 + - 配置 SSL 证书挂载(`./ssl:/etc/nginx/ssl:ro`) + - 配置 Nginx 配置文件挂载 + - 添加 Docker 网络配置,服务间通过内部网络通信 + - 容器名称改为 `marketing-site-https` 避免与 HTTP 模式冲突 + +- **文档和说明**: + - 新增 `ssl/README.md` - SSL 证书获取和配置说明 + - 说明如何获取 Let's Encrypt 证书 + - 说明如何生成自签名证书(测试用) + - 说明如何配置商业证书 + - 新增 `HTTPS_DEPLOY.md` - HTTPS 部署完整指南 + - 目录结构说明 + - HTTP 和 HTTPS 模式对比 + - 快速开始步骤(使用 `-f docker-compose.https.yml` 参数) + - 服务说明 + - 常用命令(区分 HTTP 和 HTTPS 模式) + - 配置说明 + - 故障排查 + +### 修改的文件 +- `site/docker-compose.yml` - 恢复为原始 HTTP 配置(直接访问 3000 端口) +- `site/docker-compose.https.yml` - 新增 HTTPS 配置文件(通过 Nginx 反向代理)(新建) +- `site/nginx/nginx.conf` - Nginx 反向代理和 SSL 配置(新建) +- `site/ssl/README.md` - SSL 证书配置说明(新建) +- `site/ssl/.gitignore` - 防止证书文件被提交(新建) +- `site/HTTPS_DEPLOY.md` - HTTPS 部署指南(新建,已更新说明两种模式) + +变更原因:用户需要在 site 目录创建文件目录结构,采用 docker compose 进行 HTTPS 部署。同时保留原有的 HTTP 配置,创建独立的 HTTPS 配置文件。 + ## 2025-01-XX - 首页轮播优化:添加 Home_1.jpg 并优化文字内容 ### 修改内容 diff --git a/site/HTTPS_DEPLOY.md b/site/HTTPS_DEPLOY.md new file mode 100644 index 00000000..f26ac447 --- /dev/null +++ b/site/HTTPS_DEPLOY.md @@ -0,0 +1,231 @@ +# HTTPS 部署指南 + +本文档说明如何使用 Docker Compose 进行 HTTPS 部署。 + +## 目录结构 + +``` +site/ +├── docker-compose.yml # Docker Compose HTTP 配置文件(直接访问 3000 端口) +├── docker-compose.https.yml # Docker Compose HTTPS 配置文件(通过 Nginx 反向代理) +├── nginx/ +│ └── nginx.conf # Nginx 反向代理配置 +├── ssl/ +│ ├── cert.pem # SSL 证书文件(需要自行添加) +│ ├── key.pem # SSL 私钥文件(需要自行添加) +│ └── README.md # SSL 证书说明 +└── Dockerfile # 应用镜像构建文件 +``` + +## 配置文件说明 + +- **docker-compose.yml** - HTTP 配置(默认) + - 直接暴露 3000 端口 + - 适用于开发环境或不需要 HTTPS 的场景 + +- **docker-compose.https.yml** - HTTPS 配置 + - 使用 Nginx 反向代理 + - 支持 HTTP 自动重定向到 HTTPS + - 适用于生产环境 + +## 前置要求 + +1. Docker 和 Docker Compose 已安装 +2. SSL 证书文件已准备好(见 `ssl/README.md`) + +## 快速开始 + +### 1. 准备 SSL 证书 + +将 SSL 证书文件放置到 `ssl/` 目录: + +```bash +# 证书文件 +ssl/cert.pem + +# 私钥文件 +ssl/key.pem +``` + +详细说明请参考 `ssl/README.md`。 + +### 2. 启动服务 + +使用 HTTPS 配置文件启动: + +```bash +cd site +docker-compose -f docker-compose.https.yml up -d +``` + +**注意**:使用 `-f` 参数指定 HTTPS 配置文件。如果不指定,默认使用 `docker-compose.yml`(HTTP 模式)。 + +### 3. 访问服务 + +- HTTP: http://localhost (自动重定向到 HTTPS) +- HTTPS: https://localhost + +## 服务说明 + +### marketing-site + +- 应用服务,运行在容器内部端口 3000 +- 不直接暴露端口,通过 nginx 反向代理访问 + +### nginx + +- 反向代理服务 +- 监听端口 80 (HTTP) 和 443 (HTTPS) +- 自动将 HTTP 请求重定向到 HTTPS +- 提供 SSL/TLS 终止 + +## 常用命令 + +### 启动服务 +```bash +# HTTPS 模式 +docker-compose -f docker-compose.https.yml up -d + +# HTTP 模式(默认) +docker-compose up -d +``` + +### 停止服务 +```bash +# HTTPS 模式 +docker-compose -f docker-compose.https.yml down + +# HTTP 模式 +docker-compose down +``` + +### 查看日志 +```bash +# HTTPS 模式 - 查看所有服务日志 +docker-compose -f docker-compose.https.yml logs -f + +# HTTPS 模式 - 查看特定服务日志 +docker-compose -f docker-compose.https.yml logs -f nginx +docker-compose -f docker-compose.https.yml logs -f marketing-site + +# HTTP 模式 +docker-compose logs -f +``` + +### 重启服务 +```bash +# HTTPS 模式 +docker-compose -f docker-compose.https.yml restart + +# HTTP 模式 +docker-compose restart +``` + +### 重新构建应用 +```bash +# HTTPS 模式 +docker-compose -f docker-compose.https.yml build marketing-site +docker-compose -f docker-compose.https.yml up -d + +# HTTP 模式 +docker-compose build marketing-site +docker-compose up -d +``` + +## 配置说明 + +### 修改端口 + +如需修改 HTTPS 端口,编辑 `docker-compose.https.yml`: + +```yaml +nginx: + ports: + - "80:80" + - "8443:443" # 修改为 8443 +``` + +### 修改域名 + +编辑 `nginx/nginx.conf`,将 `server_name _;` 替换为您的域名: + +```nginx +server_name your-domain.com www.your-domain.com; +``` + +### 更新 SSL 证书 + +1. 将新证书文件放置到 `ssl/` 目录 +2. 重启 nginx 服务: +```bash +# HTTPS 模式 +docker-compose -f docker-compose.https.yml restart nginx + +# HTTP 模式不需要此操作 +``` + +## 故障排查 + +### 检查容器状态 +```bash +# HTTPS 模式 +docker-compose -f docker-compose.https.yml ps + +# HTTP 模式 +docker-compose ps +``` + +### 检查 SSL 证书 +```bash +# 验证证书文件是否存在 +ls -la ssl/ + +# 验证证书内容 +openssl x509 -in ssl/cert.pem -text -noout +``` + +### 检查 Nginx 配置 +```bash +# 进入 nginx 容器 +docker exec -it nginx-https sh + +# 测试配置 +nginx -t +``` + +### 查看错误日志 +```bash +# HTTPS 模式 +docker-compose -f docker-compose.https.yml logs nginx + +# HTTP 模式(无 nginx 服务) +docker-compose logs marketing-site +``` + +## 安全建议 + +1. ✅ 使用有效的 SSL 证书(生产环境) +2. ✅ 定期更新 SSL 证书 +3. ✅ 使用强密码保护私钥文件 +4. ✅ 不要将私钥文件提交到版本控制系统 +5. ✅ 定期更新 Docker 镜像和依赖 + +## HTTP vs HTTPS 模式对比 + +| 特性 | HTTP 模式 | HTTPS 模式 | +|------|-----------|------------| +| 配置文件 | `docker-compose.yml` | `docker-compose.https.yml` | +| 访问端口 | 3000 | 80 (HTTP), 443 (HTTPS) | +| 访问地址 | http://localhost:3000 | http://localhost, https://localhost | +| 反向代理 | 无 | Nginx | +| SSL 证书 | 不需要 | 需要 | +| 适用场景 | 开发环境 | 生产环境 | + +## 注意事项 + +- 首次启动 HTTPS 模式前必须准备好 SSL 证书文件 +- 如果证书文件不存在,nginx 容器将无法启动 +- 自签名证书仅用于开发测试,生产环境请使用有效证书 +- 确保防火墙允许 80 和 443 端口访问(HTTPS 模式) +- HTTP 模式和 HTTPS 模式可以同时运行,但需要确保端口不冲突 + diff --git a/site/docker-compose.https.yml b/site/docker-compose.https.yml new file mode 100644 index 00000000..46ed363d --- /dev/null +++ b/site/docker-compose.https.yml @@ -0,0 +1,35 @@ +version: '3.8' + +services: + marketing-site: + build: + context: . + dockerfile: Dockerfile + expose: + - "3000" + environment: + - NODE_ENV=production + restart: unless-stopped + container_name: marketing-site-https + networks: + - app-network + + nginx: + image: nginx:alpine + container_name: nginx-https + ports: + - "80:80" + - "443:443" + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro + - ./ssl:/etc/nginx/ssl:ro + depends_on: + - marketing-site + restart: unless-stopped + networks: + - app-network + +networks: + app-network: + driver: bridge + diff --git a/site/nginx/nginx.conf b/site/nginx/nginx.conf new file mode 100644 index 00000000..614a7e0c --- /dev/null +++ b/site/nginx/nginx.conf @@ -0,0 +1,76 @@ +events { + worker_connections 1024; +} + +http { + # 上游服务器配置 + upstream marketing-site { + server marketing-site:3000; + } + + # HTTP 重定向到 HTTPS + server { + listen 80; + server_name _; + + # 允许 Let's Encrypt 验证 + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + + # 其他所有请求重定向到 HTTPS + location / { + return 301 https://$host$request_uri; + } + } + + # HTTPS 服务器配置 + server { + listen 443 ssl http2; + server_name _; + + # SSL 证书配置 + ssl_certificate /etc/nginx/ssl/cert.pem; + ssl_certificate_key /etc/nginx/ssl/key.pem; + + # SSL 安全配置 + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers HIGH:!aNULL:!MD5; + ssl_prefer_server_ciphers on; + ssl_session_cache shared:SSL:10m; + ssl_session_timeout 10m; + + # 安全头 + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-XSS-Protection "1; mode=block" always; + + # 代理配置 + location / { + proxy_pass http://marketing-site; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + 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_cache_bypass $http_upgrade; + + # 超时设置 + proxy_connect_timeout 60s; + proxy_send_timeout 60s; + proxy_read_timeout 60s; + } + + # 静态文件缓存 + location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ { + proxy_pass http://marketing-site; + proxy_cache_valid 200 30d; + expires 30d; + add_header Cache-Control "public, immutable"; + } + } +} + diff --git a/site/ssl/.gitignore b/site/ssl/.gitignore new file mode 100644 index 00000000..7d251e6d --- /dev/null +++ b/site/ssl/.gitignore @@ -0,0 +1,9 @@ +*.pem +*.key +*.crt +*.cert +*.p12 +*.pfx +!README.md +!.gitignore + diff --git a/site/ssl/README.md b/site/ssl/README.md new file mode 100644 index 00000000..b92e71c9 --- /dev/null +++ b/site/ssl/README.md @@ -0,0 +1,62 @@ +# SSL 证书目录 + +此目录用于存放 SSL 证书文件。 + +## 文件要求 + +请将以下文件放置在此目录中: + +- `cert.pem` - SSL 证书文件(或 `fullchain.pem`) +- `key.pem` - SSL 私钥文件 + +## 获取 SSL 证书 + +### 方式 1: 使用 Let's Encrypt (免费) + +1. 安装 certbot: +```bash +# Ubuntu/Debian +sudo apt-get update +sudo apt-get install certbot + +# 或使用 Docker +docker run -it --rm -v $(pwd)/ssl:/etc/letsencrypt certbot/certbot certonly --standalone +``` + +2. 获取证书后,将证书文件复制到此目录: +```bash +# Let's Encrypt 证书通常在 /etc/letsencrypt/live/your-domain.com/ +cp /etc/letsencrypt/live/your-domain.com/fullchain.pem ./ssl/cert.pem +cp /etc/letsencrypt/live/your-domain.com/privkey.pem ./ssl/key.pem +``` + +### 方式 2: 使用自签名证书 (仅用于测试) + +```bash +# 生成自签名证书(仅用于开发测试) +openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ + -keyout ssl/key.pem \ + -out ssl/cert.pem \ + -subj "/C=CN/ST=State/L=City/O=Organization/CN=localhost" +``` + +### 方式 3: 使用商业证书 + +将您购买的 SSL 证书文件重命名为: +- 证书文件 → `cert.pem` +- 私钥文件 → `key.pem` + +## 文件权限 + +确保私钥文件权限正确(仅所有者可读): +```bash +chmod 600 ssl/key.pem +chmod 644 ssl/cert.pem +``` + +## 注意事项 + +- ⚠️ **不要将私钥文件提交到版本控制系统** +- 生产环境请使用有效的 SSL 证书 +- 自签名证书会导致浏览器显示安全警告 +