自建AI中转网关域名SSL配置教程:从零到生效
为什么需要给AI中转网关配域名和SSL
自己搭建的AI中转网关(比如 One-API、New-API)默认跑在 IP+端口 上,直接暴露不安全,而且容易被运营商封或遭遇中间人攻击。
加上域名和 SSL 后,不仅访问更稳定,还能启用 HTTPS 加密传输,保护 API Key 和请求数据。
这步配置对生产环境几乎是必选项。
动手前需要准备好什么
- 一台云服务器(Linux,推荐 Ubuntu 20.04+ 或 CentOS 7+)
- 已部署好的 AI 中转网关(假设运行在
127.0.0.1:3000) - 一个已经备案或能正常解析的域名(如
gateway.example.com) - 服务器上安装好 Nginx(如果还没装,往下看命令)
- 服务器 SSH 客户端(如 Xshell、Termius)
一步步配置域名解析和 SSL 证书
1. 域名解析到服务器 IP
登录你的域名管理后台(如阿里云 DNS、腾讯云 DNSPod),添加一条 A 记录:
- 记录类型:A
- 主机记录:
gateway(如果你要用gateway.example.com) - 记录值:你的服务器公网 IP
等待几分钟到几小时,用 ping gateway.example.com 确认 IP 已生效。
2. 安装 Nginx(如果没装)
# Ubuntu / Debian
sudo apt update && sudo apt install nginx -y
# CentOS / RHEL
sudo yum install epel-release -y && sudo yum install nginx -y
启动并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
3. 申请免费 SSL 证书(使用 acme.sh)
推荐用 acme.sh,支持自动续期。
安装:
curl https://get.acme.sh | sh
source ~/.bashrc
设置默认证书服务为 Let's Encrypt:
acme.sh --set-default-ca --server letsencrypt
申请证书(需要先确保域名解析生效,并且 80 端口开放):
acme.sh --issue -d gateway.example.com --standalone -k ec-256
如果使用 Nginx 模式(更推荐,不占用 80 端口):
acme.sh --issue -d gateway.example.com --nginx
证书会保存在 ~/.acme.sh/gateway.example.com_ecc/ 目录下。
4. 配置 Nginx 反向代理 + SSL
编辑 Nginx 配置文件(假设在 /etc/nginx/conf.d/gateway.conf,
或 /etc/nginx/sites-available/gateway,
然后创建软链到 sites-enabled):
server {
listen 80;
server_name gateway.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name gateway.example.com;
ssl_certificate /root/.acme.sh/gateway.example.com_ecc/fullchain.cer;
ssl_certificate_key /root/.acme.sh/gateway.example.com_ecc/gateway.example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
重要:如果你的网关监听的是 127.0.0.1:3000,这样写没问题。
如果网关监听 0.0.0.0:3000,建议改为 127.0.0.1 或限制内网访问,避免绕过 Nginx 直接暴露。
测试配置并重载 Nginx:
sudo nginx -t
sudo systemctl reload nginx
5. 设置防火墙放行端口
# 使用 iptables / firewalld / ufw
# Ubuntu
sudo ufw allow 80,443/tcp
# CentOS
sudo firewall-cmd --permanent --add-port=80/tcp --add-port=443/tcp
sudo firewall-cmd --reload
新手最容易踩的坑
- 域名没解析生效:申请证书或访问时报错,先用
dig gateway.example.com确认。 - Nginx 配置里证书路径不对:如果你用 root 执行 acme.sh,证书就在
/root/.acme.sh/下;如果用普通用户,注意路径权限。 - 网关监听地址错误:如果 AI 网关仅监听
127.0.0.1,Nginx 的proxy_pass必须写http://127.0.0.1:3000;如果监听0.0.0.0,建议改成127.0.0.1提高安全性。 - SSL 证书未自动续期:acme.sh 默认添加了定时任务,执行
crontab -l检查;如果没看到,手动加一条。 - 502 Bad Gateway:检查网关进程是否启动,以及 Nginx 能否连通网关端口(
curl http://127.0.0.1:3000)。
如何验证配置是否生效
- 打开浏览器,访问
https://gateway.example.com,应该看到 AI 中转网关的登录页,地址栏有绿色锁图标。 - 使用 curl 测试:
curl -I https://gateway.example.com
返回 HTTP/2 200 且包含 Strict-Transport-Security 头即成功。
- 检查 Nginx 错误日志:
sudo tail -f /var/log/nginx/error.log
高频问题解答
Q:申请证书时提示 80 端口被占用?
A:关掉占用 80 的服务(如 systemctl stop nginx),或者改用 --nginx 模式。申请完成后重启 Nginx。
Q:配置完成后访问显示“您的连接不是私密连接”?
A:可能是证书链不全,检查 ssl_certificate 是否指向 fullchain.cer,而不是 cert.cer。
Q:如何免费续期?
A:acme.sh 会自动续期,你只需确保定时任务存在(默认添加在 root 用户的 crontab 中),同时证书路径不变。
总结
自建 AI 中转网关配上域名和 SSL,既安全又方便管理。
核心就是三步:域名解析、申请证书、Nginx 反代。
按照本文步骤操作,半小时内就能跑通。
如果你在中途遇到其他报错,先回看“新手坑”部分,多数都能解决。