方案 A — Docker Compose(推荐)
使用 docker/docker-compose.yml 可一键启动完整的生产部署,包含以下服务:
| 服务 | 主机端口 |
| ----------------------- | -------- |
| server(Bun 后端) | 3888 |
| api(Rust 代理) | 5060 |
| web-tenant(用户端) | 5090 |
| web-admin(管理后台) | 5091 |
| postgres | 内部网络 |
1. 准备环境文件
cd docker
./prepare.sh
该脚本会基于 docker/.env.example 自动生成 docker/.env,并补齐 BETTER_AUTH_SECRET、CRON_SECRET、RSA_PRIVATE_KEY、RSA_PUBLIC_KEY 等必要值。
如果你希望手动配置,也可以执行:
cp docker/.env.example docker/.env
随后编辑 docker/.env,至少确认以下变量:
DATABASE_URL=postgresql://postgres:postgres@postgresql:5432/openproxy
BETTER_AUTH_SECRET=<随机 32 字节 base64>
BETTER_AUTH_URL=https://your-tenant-domain/api
# APP_DOMAIN 是顶级域名(如 example.com,默认 aiproxy.shop),控制:
# - 为 *.APP_DOMAIN 生成 better-auth 受信任来源
# - 将 better-auth 会话 Cookie 的 Domain 设为 .APP_DOMAIN,让 *.APP_DOMAIN 子域
# 共享同一份登录态(仅在生产环境启用)
APP_DOMAIN=
# CLIENT_ORIGIN 是租户前端的对外 origin(协议 + 主机,无尾部斜杠,
# 例如 https://app.example.com),用于拼接支付回调/跳转地址与邮件验证
# 链接。默认生产为 https://app.${APP_DOMAIN}。
CLIENT_ORIGIN=
ADMIN_EMAILS=owner@example.com
RSA_PRIVATE_KEY=<通过 bun scripts/generateRSAKey.ts 生成>
RSA_PUBLIC_KEY=<同上生成>
REDIS_URL=redis://redis:6379
根据实际需要补充邮件(RESEND/SMTP)、OAuth、短信/验证码、支付变量。自部署建议同时设置 ADMIN_EMAILS,这样对应邮箱注册后会自动获得后台管理员权限。
2. 启动
cd docker
docker compose up -d
3. 验证
curl http://localhost:5060/health # Rust 代理
curl http://localhost:3888/api/health # Bun 服务端
4. 域名与反向代理
使用 Nginx(或其他反向代理)绑定域名:
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:5060;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:5090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
如需 HTTPS,添加 ssl_certificate / ssl_certificate_key 配置或使用 Certbot 自动申请。
方案 B — 手动部署
PostgreSQL
部署 PostgreSQL 15+,创建数据库后运行迁移:
cd apps/server
DATABASE_URL=postgres://... bun run migrate
apps/server(Bun)
cd apps/server
bun install
bun run start # 或使用 pm2 / systemd 管理
apps/api(Rust)
cd apps/api
cargo build --release
./target/release/api
推荐使用 systemd 进行进程管理:
# /etc/systemd/system/openproxy-api.service
[Unit]
Description=OpenProxy Rust API
After=network.target
[Service]
EnvironmentFile=/etc/openproxy/api.env
ExecStart=/usr/local/bin/openproxy-api
Restart=always
[Install]
WantedBy=multi-user.target
apps/web(React)
cd apps/web
bun run build:tenant # 输出到 dist/tenant
bun run build:admin # 输出到 dist/admin
将生成的 dist/ 目录通过 Nginx 或静态文件托管服务发布。
定时任务配置与调用方式见 定时任务。
生产环境检查清单
- [ ] 所有公开域名均启用 HTTPS 并使用有效证书
- [ ]
BETTER_AUTH_URL已设置为对外前端域名加/api,APP_DOMAIN已正确配置为部署使用的主域名,以匹配内置 trusted origins 和跨子域 cookie - [ ] 开启 PostgreSQL 自动备份(如定时 pg_dump 或托管数据库快照)
- [ ] 已配置
cleanupOrder和archiveMonthlyUsage的定时触发,并确保请求携带正确的CRON_SECRET - [ ] 禁止记录
RSA_PRIVATE_KEY或明文 API 密钥 - [ ] 生产环境使用密钥管理工具(Docker secrets、Vault 等),而非明文
.env文件 - [ ] 为 Rust 代理容器配置内存和 CPU 资源限制