ef260ff27754330f2e16d507c1aa37e5a372178e
ALiYunManager - 阿里云多服务 Docker 部署方案
架构概览
┌─────────────────────────────────────────────────────────────┐
│ 阿里云服务器 (8.136.137.59) │
│ 主 Nginx (Docker 容器) │
│ ┌──────────┬──────────┬──────────────┬──────────────┐ │
│ │ me. │ www. │ api-resume. │api-miniapp. │ │
│ │dxz99wyr. │dxz99wyr. │ dxz99wyr.cn │dxz99wyr.cn │ │
│ │cn │cn │ │ │ │
│ └────┬─────┴────┬─────┴──────┬───────┴──────┬───────┘ │
│ │ │ │ │ │
│ ┌────▼─────┐ ┌──▼─────┐ ┌───▼──────┐ ┌────▼──────┐ │
│ │ Docker 1 │ │Docker 2│ │ Docker 3 │ │ Docker 4 │ │
│ │个人简历网站│ │小程序网站│ │个人简历后台│ │小程序后台 │ │
│ │内部Nginx │ │内部Nginx│ │内部Nginx │ │内部Nginx │ │
│ └──────────┘ └────────┘ └──────────┘ └───────────┘ │
└─────────────────────────────────────────────────────────────┘
域名规划
| 服务 | 域名 | 类型 |
|---|---|---|
| 个人简历网站 | me.dxz99wyr.cn |
前端 |
| 小程序网站 | www.dxz99wyr.cn |
前端 |
| 个人简历后台 | api-resume.dxz99wyr.cn |
API |
| 小程序后台 | api-miniapp.dxz99wyr.cn |
API |
目录结构
ALiYunManager/
├── docker-compose.yml # 主 Docker Compose 文件
├── deploy.sh # 部署脚本
├── README.md # 本文件
├── nginx/
│ ├── nginx.conf # 主 Nginx 核心配置(**不要修改**)
│ ├── conf.d/ # 各个服务的反向代理配置(由服务自行管理)
│ │ ├── resume-web.conf
│ │ ├── miniapp-web.conf
│ │ ├── resume-api.conf
│ │ └── miniapp-api.conf
│ ├── ssl/ # SSL 证书目录
│ └── logs/ # 日志目录
└── services/ # 各个服务的配置(每个服务自行管理)
├── resume-web/
│ ├── nginx.conf # 服务内部 Nginx 配置
│ └── html/ # 静态文件
├── miniapp-web/
│ ├── nginx.conf
│ └── html/
├── resume-api/
│ ├── nginx.conf # 内部 Nginx 配置或反向代理
│ └── Dockerfile # 后端 Dockerfile 模板
└── miniapp-api/
├── nginx.conf
└── Dockerfile
核心设计原则
- 主 Nginx 只负责反向代理:将外部请求按域名分发到各个 Docker 容器
- 每个服务独立管理自己的配置:
- 每个服务在
nginx/conf.d/下放一个.conf文件(主 Nginx 加载) - 每个服务在
services/服务名/下放自己的内部 Nginx 配置
- 每个服务在
- 所有服务都在 Docker 容器中运行:便于管理和隔离
- 使用 Docker 内部网络通信:主 Nginx 通过容器名直接访问服务
快速开始
1. 上传到服务器
# 在项目目录打包
zip -r aliyun-manager.zip ALiYunManager/
# 上传到服务器(使用你的密钥)
scp -i "D:\003_Project\小程序连接.pem" aliyun-manager.zip root@8.136.137.59:/opt/
2. 在服务器上部署
ssh -i "D:\003_Project\小程序连接.pem" root@8.136.137.59
cd /opt
unzip aliyun-manager.zip
cd ALiYunManager
chmod +x deploy.sh
./deploy.sh
3. 配置 DNS
在域名管理后台添加以下解析记录:
| 记录类型 | 主机记录 | 记录值 |
|---|---|---|
| A | me | 8.136.137.59 |
| A | miniapp | 8.136.137.59 |
| A | api-resume | 8.136.137.59 |
| A | api-miniapp | 8.136.137.59 |
常用命令
# 启动所有服务
docker-compose up -d
# 停止所有服务
docker-compose down
# 查看主 Nginx 日志
docker-compose logs -f nginx
# 重启主 Nginx
docker-compose restart nginx
# 查看所有容器状态
docker-compose ps
# 重载 Nginx 配置(不重启容器)
docker-compose exec nginx nginx -s reload
配置 HTTPS (SSL)
方法一:使用 Certbot (推荐)
# 进入容器安装 certbot
docker-compose exec nginx sh
apk add certbot
# 申请证书(需要先确保 DNS 已解析)
certbot certonly --standalone -d me.dxz99wyr.cn -d www.dxz99wyr.cn -d api-resume.dxz99wyr.cn -d api-miniapp.dxz99wyr.cn
方法二:手动配置
将证书文件放入 nginx/ssl/ 目录:
nginx/ssl/
├── me.dxz99wyr.cn.crt
├── me.dxz99wyr.cn.key
└── ...
然后修改对应服务的 .conf 文件,添加 443 监听和 SSL 配置。
各服务如何接入
前端网站接入
- 将你的构建产物放入
services/resume-web/html/目录 - 如需修改内部 Nginx 配置,编辑
services/resume-web/nginx.conf - 如需修改反向代理规则,编辑
nginx/conf.d/resume-web.conf - 重启对应容器
后端 API 接入
- 替换
services/resume-api/Dockerfile为你的后端 Dockerfile - 修改
services/resume-api/nginx.conf中的upstream指向你的应用端口 - 如需修改反向代理规则,编辑
nginx/conf.d/resume-api.conf - 重新构建并启动容器
注意事项
- 不要修改
nginx/nginx.conf主配置文件,除非你需要调整全局参数 - 各个服务的
.conf文件命名不要冲突 - 后端服务如果不需要内部 Nginx,可以直接暴露应用端口,主 Nginx 直接代理到应用端口
- 生产环境建议开启防火墙,只开放 80 和 443 端口
Description
Languages
Shell
89%
Makefile
11%