5.3 KiB
5.3 KiB
nginx-proxy-ai — tlyq.ai 反向代理
tlyq.ai 各站点的 nginx 反向代理配置,统一管理 SSL 证书、路由规则和性能优化。
架构
客户端 → nginx:80/443 → 后端容器(Docker 内网)
↓
SSL 终止 + Gzip + 缓存 + 限流
- 生产环境:
docker-compose.yml,端口 80/443,挂载/etc/letsencrypt证书 - 本地测试:
docker-compose.local.yml,端口 9443-9445,自签名证书
代理站点
| 子域名 | 后端容器 | 端口 | 说明 |
|---|---|---|---|
| www.tlyq.ai | www-ai | 3000 | 图灵引擎官网 |
| cloud.tlyq.ai | cloud-ai | 80 | 智算系统云平台(纯静态) |
| token.tlyq.ai | token-ai | 80 | Token 工厂(纯静态) |
| issue.tlyq.ai | issue-ai | 3000 | 工单系统 |
| assets.tlyq.ai | assets-ai | 3000 | 资产管理系统 |
| git.tlyq.ai | gitea-ai | 3000 | Gitea 代码托管 |
| oa.tlyq.ai | oa-ai | 3000 | OA 统一门户 |
| sso.tlyq.ai | authelia | 9091 | Authelia SSO 认证中心 |
| monitor.tlyq.ai | monitor-ai | 3000 | 告警监控中心 |
SSL 证书
证书来源
| 域名 | 证书类型 | 有效期 | 备注 |
|---|---|---|---|
| www.tlyq.ai | Let's Encrypt | 2026-07-01 ~ 2026-09-29 | 主证书,所有站点共用 |
| www.tlyq.ai-0001 | Let's Encrypt | — | SSO 站点专用 |
⚠️ 重要:禁止使用 CloudFlare Origin 证书
CloudFlare Origin 证书是自签名证书,浏览器不信任,会导致 SSL 不安全提示。
所有站点必须使用 Let's Encrypt 公共信任证书。
证书续期
证书续期通过 systemd timer 自动管理,每天检查两次(01:30 和 22:32),距离到期 < 30 天时自动续期。
# 查看定时器状态
systemctl status snap.certbot.renew.timer
# 查看下次运行时间
systemctl list-timers snap.certbot.renew.timer
# 手动触发续期
systemctl start snap.certbot.renew.service
# 查看续期日志
journalctl -u snap.certbot.renew.service --since "1 hour ago"
# 测试续期(不实际续期)
certbot renew --dry-run
续期成功后自动执行 deploy hook 重载 nginx,无需人工干预。
新增站点证书
-
使用 certbot 申请证书:
certbot certonly --webroot -w /var/www/html -d new-site.tlyq.ai -
在
conf.d/添加配置,引用www.tlyq.ai的证书:ssl_certificate /etc/letsencrypt/live/www.tlyq.ai/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.tlyq.ai/privkey.pem; -
重启 nginx:
docker exec nginx-ai nginx -s reload
性能优化配置
全局配置(nginx.conf)
| 配置项 | 值 | 说明 |
|---|---|---|
sendfile |
on | 零拷贝文件传输 |
tcp_nopush |
on | 聚合响应头和 body |
tcp_nodelay |
on | keepalive 连接不延迟发送 |
keepalive_timeout |
65s | 客户端 keep-alive 超时 |
keepalive_requests |
1000 | 单连接最大请求数 |
gzip |
on | 启用 Gzip 压缩 |
gzip_comp_level |
5 | 压缩级别(1-9,5 为平衡点) |
gzip_types |
text/css/js/json/xml/svg | 压缩的 MIME 类型 |
SSL 安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:...;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets on;
反代配置
proxy_connect_timeout 3s; # 连接后端超时
proxy_send_timeout 30s; # 发送请求超时
proxy_read_timeout 30s; # 读取响应超时
proxy_buffer_size 16k; # 响应头缓冲
proxy_buffers 8 16k; # 响应体缓冲
部署
生产部署
# 上传配置到服务器
rsync -avz --exclude='node_modules' --exclude='.git' \
--exclude='nginx-local.conf' --exclude='docker-compose.local.yml' \
--exclude='certs/' \
./ txjp:/root/docker/nginx-proxy-ai/
# 重启 nginx
ssh txjp "docker exec nginx-ai nginx -t && docker exec nginx-ai nginx -s reload"
本地测试
docker compose -f docker-compose.local.yml up -d
访问:https://localhost:9444(assets)、https://localhost:9445(issue)
文件结构
nginx-proxy-ai/
├── nginx.conf # 生产环境主配置
├── nginx-local.conf # 本地测试主配置
├── docker-compose.yml # 生产环境 Docker 配置
├── docker-compose.local.yml # 本地测试 Docker 配置
├── conf.d/ # 各站点反向代理配置
│ ├── www-ai.conf
│ ├── cloud-ai.conf
│ ├── token-ai.conf
│ ├── issue-ai.conf
│ ├── assets-ai.conf
│ ├── git-ai.conf
│ ├── oa-ai.conf
│ ├── sso-ai.conf
│ ├── monitor-ai.conf
│ ├── root-domain.conf # 根域名跳转
│ └── letsencrypt.conf # ACME 验证
├── certs/ # 本地测试自签名证书
├── static/ # 静态文件
└── README.md
常用命令
# 测试配置语法
docker exec nginx-ai nginx -t
# 平滑重载配置
docker exec nginx-ai nginx -s reload
# 查看完整合并配置
docker exec nginx-ai nginx -T
# 查看 nginx 版本
docker exec nginx-ai nginx -v
# 查看当前生效的 SSL 配置
docker exec nginx-ai nginx -T | grep ssl