本視頻是一份全流程「保姆級」教學,手把手教你如何從零開始搭建一個對外 API 中轉站。相關視頻中的內容、命令、腳本、代碼,都在博客文章中 🔗https://869hr.uk
視頻教程
教程核心架構
VPS + CLI Proxy API (CPA) + NEW API + Docker + Nginx + Cloudflare + SSL + 域名配置
時間戳
| 時間 | 步驟 |
|---|
| 00:00 | 前提与 VPS 选择:騰訊雲最新活動頁面選擇與購買 |
| 01:30 | 伺服器連接:使用 FinalShell 連接到伺服器並查看 IP |
| 02:45 | 準備工作:創建資料夾結構 (/opt/proxy/…) |
| 04:00 | 配置 CPA:修改 cpa/config.yaml(⚠️ 請務必修改密碼與秘鑰) |
| 06:15 | 配置 Docker Compose:編寫 docker-compose.yml |
| 08:30 | 認證文件:將認證文件放進指定目錄 |
| 10:00 | 防火牆配置:Linux 防火牆與云伺服器安全組打開 3000 端口 |
| 11:45 | 執行與訪問:執行 docker compose 並訪問中轉站首頁 |
| 13:15 | New API 配置:登錄、渠道管理、添加渠道、測試 |
| 15:00 | 系統管理:設置模型價格、模型廣場 |
| 16:30 | 令牌配置:配置與測試令牌 |
| 18:00 | 本地連接測試:使用 CC Switch 進行本地測試 |
| 19:30 | 域名與 SSL (進階):Cloudflare DNS 代理域名 |
| 21:30 | Nginx 配置:反向代理與安全增強 |
| 23:45 | SSL 證書:Cloudflare Origin Certificate |
| 25:15 | 最終安全性部署:更新防火牆與 Docker Compose |
| 27:30 | 重啟與驗證:最終啟動與域名訪問驗證 |
前提
VPS选择
购买完毕后进入控制台
查看服务器ip
使用FinalShell连接到服务器
对外中转站教程开始
一、准备工作
1、创建一个文件夹用于存文件,并进入该文件夹
1
| mkdir -p /opt/proxy/{cpa/{logs,auths},newapi/{data,logs}} && cd /opt/proxy && touch docker-compose.yml cpa/config.yaml
|
1 2 3 4 5 6 7 8 9
| /opt/proxy/ ├── docker-compose.yml ├── cpa/ │ ├── logs/ │ ├── auths/ │ └── config.yaml └── newapi/ ├── data/ └── logs/
|
2、cpa目录下的 config.yaml 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
host: ""
port: 8317 tls:
enable: false
remote-management:
allow-remote: true
secret-key: "$2a$10$5dHykttqHWAU.WcYFg0qgOXhDoxC1P7wpZ7i2T8Kj9xqwvVRfTUm2"
disable-control-panel: false
auth-dir: "~/.cli-proxy-api"
api-keys: - sk-8SGCShq021BAFgpBE
debug: true
logging-to-file: true
logs-max-total-size-mb: 100
request-retry: 3
max-retry-interval: 30 routing:
strategy: "round-robin"
ws-auth: false
usage-statistics-enabled: false
|
3、proxy目录下的 docker-compose.yml 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
services:
new-api:
image: calciumion/new-api:latest
container_name: new-api
restart: always
command: --log-dir /app/logs
ports: - '3000:3000'
volumes: - ./newapi/data:/data - ./newapi/logs:/app/logs
environment: - SQL_DSN=postgresql://root:123456@postgres:5432/new-api - REDIS_CONN_STRING=redis://redis
- TZ=Asia/Shanghai
- ERROR_LOG_ENABLED=true
- BATCH_UPDATE_ENABLED=true
depends_on: - redis - postgres - cpa
healthcheck:
test: [ 'CMD-SHELL', "wget -q -O - http://localhost:3000/api/status | grep -o '\"success\":\\s*true' || exit 1", ] interval: 30s timeout: 10s retries: 3
redis: image: redis:latest container_name: redis restart: always
postgres: image: postgres:15 container_name: postgres restart: always environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: 123456
POSTGRES_DB: new-api volumes: - pg_data:/var/lib/postgresql/data
cpa: image: eceasy/cli-proxy-api:latest container_name: cpa volumes: - ./cpa/config.yaml:/CLIProxyAPI/config.yaml - ./cpa/auths:/root/.cli-proxy-api - ./cpa/logs:/CLIProxyAPI/logs restart: always
volumes: pg_data:
|
4、把认证文件放进 /opt/proxy/cpa/auths 目录下
5、去Linux的防火墙打开3000端口
先查看下防火墙状态:
1 2 3 4 5 6 7 8 9 10
| systemctl status firewalld
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --list-services
firewall-cmd --list-ports
|
如果防火墙没有开启,则不用执行下面的步骤,或者你要安全可以打开防火墙后执行下面命令:
1
| firewall-cmd --zone=public --add-port=3000/tcp --permanent && firewall-cmd --reload && firewall-cmd --list-ports
|
6、打开云服务器的安全组
进入腾讯云控制台,选择服务器,点击防火墙选项卡,添加规则:来源选择全部,协议端口填写 3000。注意不要动系统默认的 SSH(22端口)规则。
二、执行docker compose文件
默认用户名 root,默认密码 123456,登录后记得修改密码。
四、New API 配置
1、渠道管理
添加渠道 - 类型选择对应选项 - Base URL 填写 http://cpa:8317(Docker 内部网络用容器名访问)- 密钥填写 CPA 配置文件中的 API Keys 值
2、系统管理
设置模型价格(内部计费单位),配置完模型价格后渠道测试才能通过。
3、其他设置
系统设置中可配置可见菜单、签到功能、系统公告、API 信息、支付设置等。
4、配置令牌
创建令牌并复制密钥值,这就是用户调用 API 时需要的密钥。
5、本地连接测试(CC Switch)
在 CC Switch 中填入中转站地址和令牌,发送测试消息验证中转站正常工作。
五、域名与 SSL(进阶)
1、使用 Cloudflare 的 DNS 代理域名
- 登录 Cloudflare,点击添加站点,输入域名
- 选择免费计划
- 到域名注册商(如阿里云)修改 NS 记录指向 Cloudflare 提供的地址
- 等待 DNS 传播生效(通常几小时)
2、添加 Nginx 目录和文件
1 2 3 4
| mkdir -p /opt/proxy/nginx/{ssl,logs,conf.d} && \ cd /opt/proxy && \ touch nginx/nginx.conf nginx/conf.d/api.conf nginx/ssl/api.pem nginx/ssl/api.key && \ chmod 600 ./nginx/ssl/api.key
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| /opt/proxy/ ├── docker-compose.yml ├── newapi/ │ ├── data/ │ └── logs/ ├── cpa/ │ ├── config.yaml │ ├── auths/ │ └── logs/ └── nginx/ ├── nginx.conf ├── conf.d/ │ └── api.conf ├── ssl/ │ ├── api.pem │ └── api.key └── logs/
|
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 1024; use epoll; multi_accept on; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; client_max_body_size 100m;
gzip on; gzip_vary on; gzip_min_length 1024; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;
include /etc/nginx/conf.d/*.conf; }
|
api.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| server { listen 80; server_name api.wudike.online;
return 301 https://$server_name$request_uri; }
server { listen 443 ssl http2; server_name api.wudike.online;
ssl_certificate /etc/nginx/ssl/api.pem; ssl_certificate_key /etc/nginx/ssl/api.key;
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=63072000" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always;
access_log /var/log/nginx/api-access.log main; error_log /var/log/nginx/api-error.log;
client_max_body_size 100m;
location / { proxy_pass http://new-api:3000; proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade';
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_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Port $server_port;
proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s;
proxy_buffering off; proxy_cache off; proxy_cache_bypass $http_upgrade; }
location /nginx-health { access_log off; return 200 "healthy\n"; add_header Content-Type text/plain; } }
|
SSL 证书
在 Cloudflare 的 SSL/TLS 设置页面,选择源服务器选项卡,点击创建证书。将证书内容复制到 api.pem,私钥内容复制到 api.key。
1
| chmod 600 ./nginx/ssl/api.key
|
3、配置 DNS 解析
在 Cloudflare DNS 管理页面添加 A 记录:名称填写子域名(如 api),IPv4 地址填写 VPS 的公网 IP,确保代理状态开启(橙色云朵图标)。
4、打开防火墙
1 2 3
| firewall-cmd --permanent --add-port=443/tcp && firewall-cmd --permanent --add-port=80/tcp && firewall-cmd --reload firewall-cmd --permanent --remove-port=3000/tcp && firewall-cmd --reload
|
在云服务商安全组中同样开放 80/443,移除 3000。
5、修改 Docker Compose 添加 Nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
services: new-api: image: calciumion/new-api:latest container_name: new-api restart: always command: --log-dir /app/logs ports: - '3000' volumes: - ./newapi/data:/data - ./newapi/logs:/app/logs environment: - SQL_DSN=postgresql://root:123456@postgres:5432/new-api - REDIS_CONN_STRING=redis://redis - TZ=Asia/Shanghai - ERROR_LOG_ENABLED=true - BATCH_UPDATE_ENABLED=true depends_on: - redis - postgres - cpa healthcheck: test: [ 'CMD-SHELL', "wget -q -O - http://localhost:3000/api/status | grep -o '\"success\":\\s*true' || exit 1", ] interval: 30s timeout: 10s retries: 3 networks: - newapi-network
redis: image: redis:latest container_name: redis restart: always networks: - newapi-network
postgres: image: postgres:15 container_name: postgres restart: always environment: POSTGRES_USER: root POSTGRES_PASSWORD: 123456 POSTGRES_DB: new-api volumes: - pg_data:/var/lib/postgresql/data networks: - newapi-network
cpa: image: eceasy/cli-proxy-api:latest container_name: cpa volumes: - ./cpa/config.yaml:/CLIProxyAPI/config.yaml - ./cpa/auths:/root/.cli-proxy-api - ./cpa/logs:/CLIProxyAPI/logs restart: always networks: - newapi-network
nginx: image: nginx:alpine container_name: nginx restart: always ports: - '80:80' - '443:443' volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro - ./nginx/conf.d:/etc/nginx/conf.d:ro - ./nginx/ssl:/etc/nginx/ssl:ro - ./nginx/logs:/var/log/nginx depends_on: - new-api networks: - newapi-network
volumes: pg_data:
networks: newapi-network: driver: bridge
|
6、启动 Docker Compose
1 2
| docker compose down docker compose up -d
|
在浏览器中访问 https://你的域名,确认能看到 New API 登录页面且地址栏显示安全锁图标。
相關鏈接
⚠️ 安全提示:教程中涉及的所有密碼、秘鑰、隨機字符串,在實際部署時必須修改為您自己的內容!
日常鏈接
注意⚠️鏈接需複製到瀏覽器中才能打開!
關注我
- 微信討論群:https://qr.869hr.uk/aitech
- 超過100T資料總站:https://doc.869hr.uk
- Telegram 群聊:https://t.me/tgmShareAI
- 微信公眾號:搜「AI前沿的短褲哥」
- 視頻的文字博客:https://869hr.uk
- 推特:https://x.com/gxjdian
- YouTube:https://youtube.com/@gxjdian
VPS 主機推薦
賬號購買 & 充值
eSIM 推薦
- 三家 eSIM 對比及開戶鏈接:https://s.869hr.uk/mcc
- 9eSIM 打 9 折(優惠碼:maq):https://www.9esim.com/?coupon=maq
- ESTK 打 9 折(優惠碼:GXJDIAN):https://store.estk.me/zh?aid=16007
- XeSIM 打 9 折(推薦碼:gxjdian):https://xesim.cc/?DIST=RE5FHg==
- Wise 申請鏈接(推薦碼:lizhiw12):https://wise.com/invite/ihpc/lizhiw12
- N26 申請鏈接(推薦碼:lizhiw02766c):https://youtu.be/HY9OD8rX89s?si=78REb8MyKSJB6cwQ
- Bybit 支付卡申請鏈接:https://www.bybit.com/invite?ref=LGNQRG
相關播放列表
參考文章
如果覺得這期視頻對你有幫助,請點贊、評論、訂閱頻道並打開小鈴鐺 🔔