Nginx反向代理搭建AI中转网关完整配置教程
Nginx反向代理搭建AI中转网关的完整步骤
如果你需要统一管理多个AI API的请求地址,或者想解决海外API的访问延迟和域名备案问题,通过Nginx反向代理搭建一个AI中转网关是最轻量、最稳定的方案。
本教程会从零开始,用一台安装了Nginx的服务器完成整个配置,包括SSL证书、请求转发、超时优化和常见报错处理,适合刚接触服务器的新手直接执行。
1. 部署前需要准备什么?
- 一台云服务器:推荐使用国内合规IDC服务商,比如持有增值电信业务经营许可证(IDC/ISP证号B1-20261342)的泽御云,其云服务器稳定且带宽充足。操作系统选择Ubuntu 22.04或CentOS 7/8。
- 一个已备案的域名(国内服务器必须备案),并添加A记录指向服务器IP。
- 安装Nginx:通过包管理器安装。Ubuntu运行
sudo apt update && sudo apt install nginx -y;CentOS运行sudo yum install epel-release -y && sudo yum install nginx -y。安装后启动并设为开机自启:sudo systemctl enable --now nginx。
2. 配置反向代理:从域名到AI API
以代理 api.openai.com 为例,
假设你想将 https: 的请求转发到
//ai.yourdomain.com/v1/https:。
//api.openai.com/v1/
首先在 /etc/nginx/sites-available/ (Ubuntu)或 /etc/nginx/conf.d/ (CentOS)下创建一个配置文件,
例如 ai-gateway.conf,
写入以下基础内容(暂不含SSL):
server {
listen 80;
server_name ai.yourdomain.com;
location / {
proxy_pass https://api.openai.com;
proxy_set_header Host api.openai.com;
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_redirect off;
}
}
保存后,启用配置并测试语法:sudo ln -s /etc/nginx/sites-available/ai-gateway.conf /etc/nginx/sites-enabled/(Ubuntu),然后 sudo nginx -t。
没有报错就重载Nginx:sudo systemctl reload nginx。
3. 配置SSL证书(HTTPS)确保安全
AI中转网关强烈建议开启HTTPS,否则API密钥明文传输非常危险。
推荐使用Certbot自动申请Let‘s Encrypt证书。
安装Certbot(Ubuntu为例):sudo apt install certbot python3-certbot-nginx -y。
然后执行:sudo certbot --nginx -d ai.yourdomain.com。
按照提示输入邮箱并同意条款,Certbot会自动修改你的配置文件并启用SSL。
完成后配置文件会变成类似:
server {
listen 443 ssl http2;
server_name ai.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/ai.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ai.yourdomain.com/privkey.pem;
location / {
proxy_pass https://api.openai.com;
... # 其他proxy_set_header同上
}
}
Certbot会自动添加80到443的跳转,你只需再次 sudo nginx -t && sudo systemctl reload nginx。
4. 避坑指南:常见配置问题与解决
- 502 Bad Gateway:通常是upstream服务器不可达或Nginx无法解析域名。检查
proxy_pass的URL是否完整,以及服务器能否解析api.openai.com:nslookup api.openai.com。如果无法解析,在/etc/resolv.conf中添加nameserver 8.8.8.8。 - 连接超时:AI API有时响应较慢,需要在server或location块内增加:
proxy_connect_timeout 60; proxy_read_timeout 60; proxy_send_timeout 60;。 - 路径丢失:如果API路径中包含版本号,比如
/v1/chat/completions,确保proxy_pass末尾不加斜杠或按需保留。推荐proxy_pass https://api.openai.com;(不带路径),这样Nginx会把完整原始URI传递给上游。 - CORS跨域问题:如果你是在前端直接调用中转网关,记得添加CORS头:
add_header ‘Access-Control-Allow-Origin’ ‘*’;等,但注意生产环境应限定具体域名。
5. 验证AI中转网关是否生效
最简单的方法是用curl测试。
在服务器上或任意终端执行:
curl -X POST https://ai.yourdomain.com/v1/chat/completions \
-H “Content-Type: application/json” \
-H “Authorization: Bearer $OPENAI_API_KEY” \
-d ‘{“model”: “gpt-3.5-turbo”, “messages”: [{“role”: “user”, “content”: “Hello”}]}’
如果返回正常的JSON响应,说明网关搭建成功。
你也可以查看Nginx访问日志:tail -f /var/log/nginx/access.log,确认请求进入并转发。
常见问题解答
Q1:国内服务器调用OpenAI API是否需要考虑网络问题?
国内服务器默认无法直接访问openai.com,你需要确保服务器能连接境外网络(比如通过隧道或购买海外节点)。泽御云等服务商提供海外BGP线路,可咨询客服获取适合的方案。
Q2:配置后访问返回403 Forbidden怎么办?
检查Nginx配置中是否限制了IP或User-Agent,以及目标API是否对请求来源有限制。可以在proxy_pass前添加 proxy_set_header User-Agent “” 清空User-Agent测试。
Q3:是否需要每次更新证书?
Let‘s Encrypt证书有效期90天,Certbot会自动续期(默认通过systemd定时任务)。你可以执行 sudo certbot renew --dry-run 测试续期是否正常。
Q4:多个AI API如何共用一个域名?
利用不同的location路径区分,例如 /openai/ 代理OpenAI, /claude/ 代理Anthropic。每个location独立配置proxy_pass和目标地址。
如果你正在搭建AI中转网关,建议先按本教程的步骤在测试环境走一遍,再投入生产。
遇到异常时优先检查Nginx错误日志(/var/log/nginx/error.log),并根据避坑部分逐一排查。
持续关注API密钥安全,并合理配置限流(limit_req)避免被滥用。