Skip to content

AWS 离岸部署方案(new-api token 中转站)

背景

参考 aihubmix.com 的部署方式:服务部署在 AWS 日本东京(Azure 同理),国内用户通过中国移动国际出口(CMI)直连访问,无需翻墙,延迟约 100–150ms。

路由路径(实测):

本地 → 中国移动骨干网 → CMI 国际出口 → Azure/AWS 东京 → 服务器

架构

国内用户 → DNS(A 记录,灰色云) → AWS EC2(香港/东京) → new-api → 上游 API

区域选择

AWS 区域位置对国内延迟最优运营商
ap-east-1香港~30–60ms联通/电信/移动
ap-northeast-1东京~100ms移动(CMI)
ap-southeast-1新加坡~80–120ms移动/联通

推荐首选香港(ap-east-1),三网延迟最低。


EC2 规格

  • 实例:t3.small(2核2G)起,稳定用 t3.medium(2核4G)
  • 存储:30GB gp3 SSD
  • 系统:Ubuntu 22.04 LTS
  • 安全组:开放 80、443、22(SSH 限自己 IP),不暴露 3000

部署步骤

1. 初始化服务器

bash
sudo apt update && sudo apt upgrade -y
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker ubuntu
newgrp docker

2. docker-compose.yml

yaml
version: '3.8'

services:
  new-api:
    image: calciumion/new-api:latest
    restart: always
    ports:
      - "3000:3000"
    environment:
      - SQL_DSN=root:yourpassword@tcp(db:3306)/newapi
      - REDIS_CONN_STRING=redis://redis:6379
      - SESSION_SECRET=your_random_secret_here
      - TZ=Asia/Shanghai
    depends_on:
      - db
      - redis
    volumes:
      - ./data:/data

  db:
    image: mysql:8.0
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=yourpassword
      - MYSQL_DATABASE=newapi
    volumes:
      - ./mysql:/var/lib/mysql

  redis:
    image: redis:alpine
    restart: always
bash
mkdir -p ~/newapi && cd ~/newapi
docker compose up -d

3. Nginx 反向代理

bash
sudo apt install -y nginx certbot python3-certbot-nginx

/etc/nginx/sites-available/newapi

nginx
server {
    listen 80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1: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;

        # AI 流式响应必须关闭 buffering
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 300s;
        chunked_transfer_encoding on;
    }
}
bash
sudo ln -s /etc/nginx/sites-available/newapi /etc/nginx/sites-enabled/
sudo certbot --nginx -d api.yourdomain.com
sudo systemctl reload nginx

4. DNS 配置

在域名商或 Cloudflare 添加 A 记录:

api.yourdomain.com  →  EC2 公网 IP

重要:Cloudflare 必须用灰色云(DNS only),不要开橙色代理,否则 SSE 流式响应会卡顿。


注意事项

  1. 出站 IP 封锁:OpenAI 等上游 API 可能封锁 AWS 香港/东京 IP,建议先测试,或搭配住宅代理出口。
  2. 流式响应:nginx proxy_buffering off 是必须项,否则 Claude/GPT 流式输出会积压后一次性返回。
  3. 数据备份:MySQL 数据定期备份到 S3,避免实例异常丢失数据。
  4. 弹性 IP:绑定 Elastic IP,防止 EC2 重启后公网 IP 变化导致域名失效。

验证

部署完成后执行:

bash
traceroute api.yourdomain.com
curl -o /dev/null -s -w "Total time: %{time_total}s\n" https://api.yourdomain.com/

© 2026 Amber Tech. All rights reserved.