1.Mac编译安装Nginx,根据这个文章即可
https://blog.csdn.net/a1004084857/article/details/128512612
2.此时访问[http://localhost],看到如下界面代表成功

image.png
3.在 /usr/local/nginx里新建文件夹cert. 在cert为根目录的终端上执行一下命令
bash -c "openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout cert/api.openai.com.key -out cert/api.openai.com.crt -subj '/CN=api.openai.com'"
会在 cert/ 目录生成两个文件:
-
api.openai.com.crt(证书) -
api.openai.com.key(私钥)
双击 cert\api.openai.com.crt,按以下步骤安装:
- 点击"安装证书"
-
添加到钥匙串里,选择系统或者用户都可以
image.png
3.选择这个。右键显示简介,修改成始终信任,关闭后,就不报红了
image.png

image.png
3.配置nginx.conf,直接用我的这个,修改key,key是从心流开放平台上整的https://platform.iflow.cn/。我配置的80的端口
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 阿里云 DNS
resolver 223.5.5.5 valid=300s;
resolver_timeout 5s;
server {
listen 80;#这里修改你们要的端口
listen 443 ssl;
server_name api.openai.com;
# 替换为你刚才生成的证书绝对路径
ssl_certificate ../cert/api.openai.com.crt;
ssl_certificate_key ../cert/api.openai.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 跨域(新版 Trae 必须 always)
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS always;
add_header Access-Control-Allow-Headers Content-Type,Authorization always;
if ($request_method = OPTIONS) {
return 204;
}
# 统一转发所有 /v1 请求到心流
location /v1 {
proxy_pass https://apis.iflow.cn/v1;
proxy_set_header Host apis.iflow.cn;
proxy_set_header Authorization "Bearer 你的Key";
proxy_set_header Content-Type application/json;
proxy_ssl_server_name on;
proxy_ssl_verify off;
proxy_buffering off;
proxy_cache off;
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding off;
proxy_read_timeout 3600s;
proxy_connect_timeout 120s;
# 伪装成 OpenAI 请求
proxy_set_header User-Agent "OpenAI/NodeJS/4.0.0";
proxy_set_header Referer "";
}
}
}
//查看端口被占用sudo lsof -i :80
//杀死进程
替换 1234 为实际的 PID
sudo kill -9 1234
4.运行命令测试啦
# 1. 验证配置语法(无报错才继续)
sudo ./nginx -t
# 2. 重启 Nginx(加载新配置)
sudo ./nginx -s stop && sudo ./nginx
#如果提示nginx.pid不存在,先运行这个就可以
sudo nginx
5.nginx启动后,你在浏览器里输入http://127.0.0.1/ 是能访问到

image.png
6.在终端里访问下 ping api.openai.com是可以访问到本地的

image.png
7.现在本地反代理已完成。配置下Trae的模型

image.png
这个上面的模型ID取得是

image.png
下面的密钥随便输入。只要不是空就行。就此。结束了。下面怎么使用都是老司机了。自己研究吧。

