nginx-proxy-ai/README.md

177 lines
4.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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 公共信任证书。**
### 证书续期
Let's Encrypt 证书有效期 90 天,需自动续期:
```bash
# 检查续期定时器
systemctl list-timers | grep certbot
# 手动续期
certbot renew --deploy-hook "docker exec nginx-ai nginx -s reload"
```
### 新增站点证书
1. 使用 certbot 申请证书:
```bash
certbot certonly --webroot -w /var/www/html -d new-site.tlyq.ai
```
2.`conf.d/` 添加配置,引用 `www.tlyq.ai` 的证书:
```nginx
ssl_certificate /etc/letsencrypt/live/www.tlyq.ai/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.tlyq.ai/privkey.pem;
```
3. 重启 nginx
```bash
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-95 为平衡点) |
| `gzip_types` | text/css/js/json/xml/svg | 压缩的 MIME 类型 |
### SSL 安全配置
```nginx
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;
```
### 反代配置
```nginx
proxy_connect_timeout 3s; # 连接后端超时
proxy_send_timeout 30s; # 发送请求超时
proxy_read_timeout 30s; # 读取响应超时
proxy_buffer_size 16k; # 响应头缓冲
proxy_buffers 8 16k; # 响应体缓冲
```
## 部署
### 生产部署
```bash
# 上传配置到服务器
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"
```
### 本地测试
```bash
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
```
## 常用命令
```bash
# 测试配置语法
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
```