Docker Nginx多项目部署:二级域名配置与容器化实践指南
Docker Nginx部署二级域名访问多个Web项目
一、背景与需求分析
在微服务架构和DevOps实践中,开发者常需在同一服务器上部署多个Web项目(如前端应用、API服务、后台管理系统等)。传统方式需为每个项目配置独立端口,导致访问路径复杂(如http://ip:8080、http://ip:8081)。通过二级域名(如api.example.com、admin.example.com)访问可显著提升用户体验和系统可维护性。
核心需求:
- 同一服务器运行多个Web项目
- 通过二级域名区分访问路径
- 保持高可用性和可扩展性
- 简化部署与运维流程
二、技术方案选型
2.1 Docker容器化优势
- 隔离性:每个项目运行在独立容器中,避免依赖冲突
- 可移植性:镜像打包环境配置,实现”Build Once, Run Anywhere”
- 资源控制:通过CPU/内存限制保障关键服务性能
- 快速扩展:结合Docker Compose或Kubernetes实现横向扩展
2.2 Nginx反向代理核心作用
- 统一入口:将80/443端口请求分发至不同容器
- 负载均衡:支持多容器实例的流量分配
- SSL终止:集中管理HTTPS证书
- 路径重写:实现优雅的URL路由
三、实施步骤详解
3.1 环境准备
# 安装必要工具sudo apt updatesudo apt install -y docker.io docker-compose nginx# 验证安装docker --versionnginx -v
3.2 项目容器化
以两个Node.js项目为例:
项目1(api.example.com):
# api-service/DockerfileFROM node:16-alpineWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .EXPOSE 3000CMD ["npm", "start"]
项目2(admin.example.com):
# admin-panel/DockerfileFROM node:16-alpineWORKDIR /appCOPY package*.json ./RUN npm install --productionCOPY . .EXPOSE 3001CMD ["node", "server.js"]
构建镜像:
docker build -t api-service ./api-servicedocker build -t admin-panel ./admin-panel
3.3 Nginx配置优化
创建/etc/nginx/conf.d/multi-project.conf:
# 主服务器配置server {listen 80;server_name example.com;return 301 https://$host$request_uri;}# API服务代理server {listen 443 ssl;server_name api.example.com;ssl_certificate /etc/nginx/ssl/fullchain.pem;ssl_certificate_key /etc/nginx/ssl/privkey.pem;location / {proxy_pass http://api-service:3000;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}# 管理后台代理server {listen 443 ssl;server_name admin.example.com;ssl_certificate /etc/nginx/ssl/fullchain.pem;ssl_certificate_key /etc/nginx/ssl/privkey.pem;location / {proxy_pass http://admin-panel:3001;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_pass:指向Docker容器内部服务ssl_certificate:需提前准备Let’s Encrypt或其他证书proxy_set_header:确保真实IP和Host头信息传递
3.4 Docker Compose编排
创建docker-compose.yml:
version: '3.8'services:api-service:image: api-service:latestrestart: unless-stoppednetworks:- webnetadmin-panel:image: admin-panel:latestrestart: unless-stoppednetworks:- webnetnginx-proxy:image: nginx:alpineports:- "80:80"- "443:443"volumes:- ./nginx.conf:/etc/nginx/conf.d/multi-project.conf- /etc/letsencrypt:/etc/nginx/ssldepends_on:- api-service- admin-panelnetworks:- webnetnetworks:webnet:driver: bridge
3.5 域名解析配置
在DNS管理界面添加:
- A记录
api→ 指向服务器IP - A记录
admin→ 指向服务器IP - 泛域名解析(可选)
*.example.com
四、高级优化技巧
4.1 健康检查与自动重启
# docker-compose.yml 增强示例services:api-service:healthcheck:test: ["CMD", "curl", "-f", "http://localhost:3000/health"]interval: 30stimeout: 10sretries: 3
4.2 性能调优参数
# Nginx性能优化配置worker_processes auto;worker_rlimit_nofile 100000;events {worker_connections 4000;use epoll;multi_accept on;}http {sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;client_max_body_size 20m;}
4.3 安全加固措施
- 禁用Nginx版本显示:
server_tokens off;
- 限制访问频率:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;server {location / {limit_req zone=one burst=5;}}
- 启用HSTS:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
五、常见问题解决方案
5.1 容器间通信失败
现象:Nginx无法代理到容器服务
排查步骤:
- 检查Docker网络:
docker network inspect webnet - 验证容器端口暴露:
docker ps - 测试容器内访问:
docker exec -it nginx-proxy sh→curl http://api-service:3000
5.2 SSL证书问题
解决方案:
- 使用Certbot自动获取证书:
sudo certbot --nginx -d api.example.com -d admin.example.com
- 手动证书配置时确保路径正确
- 设置自动续期:
sudo certbot renew --dry-run
5.3 性能瓶颈分析
工具推荐:
- Nginx状态监控:
location /nginx_status {stub_status on;access_log off;allow 127.0.0.1;deny all;}
- Docker统计:
docker stats - 负载测试:
ab -n 1000 -c 100 http://api.example.com/
六、扩展架构建议
6.1 自动化部署流水线
# .gitlab-ci.yml 示例stages:- build- deploybuild_api:stage: buildscript:- docker build -t api-service:$CI_COMMIT_SHA ./api-service- docker push api-service:$CI_COMMIT_SHAdeploy_production:stage: deployscript:- docker stack deploy -c docker-compose.prod.yml my_stackenvironment:name: productionurl: https://api.example.com
6.2 多环境管理策略
建议采用以下目录结构:
.├── environments/│ ├── prod/│ │ ├── docker-compose.yml│ │ └── nginx.conf│ ├── staging/│ │ └── ...│ └── dev/│ └── ...└── projects/├── api-service/└── admin-panel/
七、总结与最佳实践
- 容器命名规范:使用
<项目>-<环境>格式(如api-prod) - 配置管理:将Nginx配置和Docker Compose文件纳入版本控制
- 监控告警:集成Prometheus+Grafana监控容器指标
- 日志集中:使用ELK或Loki+Grafana收集分析日志
- 渐进式更新:采用蓝绿部署或金丝雀发布策略
通过本方案实现的二级域名访问架构,可支持横向扩展至数十个Web项目,同时保持99.9%以上的可用性。实际生产环境中,建议结合Kubernetes实现更高级的编排能力,但对于中小型项目,本文介绍的Docker+Nginx组合已能提供卓越的性价比。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权请联系我们,一经查实立即删除!